preetam88
preetam88

Reputation: 11

Passing a variable from one class to sendkeys function of another class

I am reading data from excel in one class and trying to pass the value in the sendkeys function of another class but it is not working.It is not typing the text read from excel into the textbox. I have verified that the data is read correctly from the excel.

I am using the below codes.

DataLibrary.java

public class DataLibrary {
    public static WebDriver driver;
    protected static String username;
    protected String password;

    @Test
    public void readData() throws IOException, Exception, Throwable {

        FileInputStream fis = new FileInputStream("./TestData/Data.xlsx");
        Workbook wb = WorkbookFactory.create(fis);
        Sheet sh = wb.getSheet("Data");

        username = sh.getRow(1).getCell(1).getStringCellValue();
        password = sh.getRow(2).getCell(1).getStringCellValue();

    }

}

Login.java

public class Login extends DataLibrary {

    public static WebDriver driver;

    @BeforeMethod

    public void LoginProcess() throws InterruptedException {

        driver = new FirefoxDriver();
        driver.get("URL");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        // Username in Login Page
        driver.findElement(By.id("_58_loginText")).sendKeys(username);

        // Password in Login Page
        driver.findElement(By.id("_58_password")).sendKeys(password);

        // Click on Sign In Button
        driver.findElement(By.xpath("//button")).click();
        Thread.sleep(2000);

        // Click on Apply Button in Home Page
        driver.findElement(By.id("applyButton")).click();
    }
}

When I am passing username and password in the sendkeys in the above code, it is not working. Shows no error but it is not entering the text in UI. What am I doing wrong?

Upvotes: 1

Views: 1624

Answers (1)

Gaurang Shah
Gaurang Shah

Reputation: 12960

it's a very silly mistake, you have put @BeforeMethod where you should have put @Test and visa versa, and so what is happening is LoginProcess method runs before readData method and so you get null values.

change it like mentioned below and everything will work. I have removed to code related to excel and webdriver but it's easy to understand.

public class DataLibrary {
    protected static String username;
    protected String password;

    @BeforeMethod
    public void readData() {
        username = "gaurang";
        password = "shah";
    }
}

and then your test case

public class Login extends DataLibrary {

    @Test
    public void LoginProcess() throws InterruptedException {
        System.out.println(username);
        System.out.println(password);
    }
}

Upvotes: 1

Related Questions