Home > Back-end >  Unable to call the parameters from one class-method to another class-method
Unable to call the parameters from one class-method to another class-method

Time:12-20

I have tried with the below code having two classes once is Class1-TestData and Method1-excel(), And another call to access the parameters Class2-AdminLoginAction and Method2-Admin_Login().

Here is the problem with I need to call the string parameters like UID and PWD as I marked in the Screenshot attached. But the script showed some error and was unable to access it. So, How can I solve this problem, Am I going to the right approach or any other way? Please do need full as soon as possible.

Script image for two classes

import java.io.IOException; 
import java.util.Date;

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
import org.testng.annotations.*;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;

import com.aventstack.extentreports.reporter.ExtentSparkReporter;

public class AdminLoginAction extends TestData{

WebDriver d;
Date currentdate = new Date();
String Screenshotdate = currentdate.toString().replace(" ", "-").replace(":", "-");
ExtentSparkReporter spark = new ExtentSparkReporter("ExtentReport.html");
ExtentReports extent = new ExtentReports();
@Test()
public void Admin_Login() throws InterruptedException, IOException {
    TestData excel = new TestData();
    
    extent.attachReporter(spark);
    ExtentTest test = extent.createTest("Launch browswer and access the WeClean Login page");
    test.log(Status.PASS, "Launch browser success...!!!");
    test.pass("Verified launching browser");
    System.setProperty("webdriver.chrome.driver", "D:\\backup\\selenium\\chromedriver.exe");
    d = new ChromeDriver();
    d.manage().window().maximize();
    d.get(URL);
    Utils.CaptureScreenshot(d, Screenshotdate   "_Login.png");
    d.findElement(By.id("loginUser")).sendKeys(UID);
    d.findElement(By.id("password")).sendKeys(PWD);
    d.findElement(By.id("loginButton")).click();
    Thread.sleep(5000);
    Utils.CaptureScreenshot(d, Screenshotdate   "_HomePage.png");
    test.log(Status.PASS, "Admin Logged in Successful");
    test.pass("Verified Admin logged in");
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.Test;

public class TestData{
    public void excel() throws IOException {
     String filePath = System.getProperty("user.dir") "\\Inputfiles";
     File file =    new File(filePath   "\\TestData.xlsx");
     FileInputStream inputStream = new FileInputStream(file);
     XSSFWorkbook wb=new XSSFWorkbook(inputStream);
     XSSFSheet sheet=wb.getSheet("Admin_inputs");
     XSSFRow row2=sheet.getRow(1);
     XSSFCell cell=row2.getCell(0);
     double UID= cell.getNumericCellValue();
     XSSFCell cell2 = row2.getCell(1);
     String PWD = cell2.getStringCellValue();
    }

CodePudding user response:

You can try like this:

public class TestData {
    double UID;
    String PWD;
    
    public void excel() throws IOException {
        String filePath = System.getProperty("user.dir")   "\\Inputfiles";
        File file = new File(filePath   "\\TestData.xlsx");
        FileInputStream inputStream = new FileInputStream(file);
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = wb.getSheet("Admin_inputs");
        XSSFRow row2 = sheet.getRow(1);
        XSSFCell cell = row2.getCell(0);
        UID = cell.getNumericCellValue();
        XSSFCell cell2 = row2.getCell(1);
        PWD = cell2.getStringCellValue();
    }
}

In Admin_Login() method, am just printing UID and PWD, change the code according to your requirement:

public class AdminLoginAction extends TestData {
    @Test()
    public void Admin_Login() throws InterruptedException, IOException {
        // TestData excel = new TestData();  // you don't need to create this object because you are already inheriting `TestData` class.

        excel();  // before using UID and PWD you have to call excel() method.
        System.out.println(UID);
        System.out.println(PWD);
    }
}
  • Related