I have created two class files 'LoginPage.java' and 'orangeHRM.java'
Now as part of POM, I have initialized the 'LoginPage' object in 'OrangeHRM'
LoginPage LP = new LoginPage(driver);
I have declared constructor in LoginPage file
WebDriver driver;
public LoginPage(WebDriver driver)
{
this.driver=driver;
}
Now in the LoginPage.Java file if I use driver as a statement it is throwing NullpointerException. see the below code:
WebElement username = **driver**.findElement(By.xpath("//input[@name='txtUsername' and @id='txtUsername']"));
WebElement password = **driver**.findElement(By.xpath("//input[@name='txtPassword' and @id='txtPassword']"));
WebElement loginbtn = **driver**.findElement(By.xpath("//input[@type='submit' and @id='btnLogin']"));
public void Login(String UName,String Pwd)
{
username.sendKeys(UName);
password.sendKeys(Pwd);
loginbtn.click();
}
Where as if I use driver inside a method in LoginPage.java it is working fine.see the below code:
By username = By.xpath("//input[@name='txtUsername' and @id='txtUsername']");
By password = By.xpath("//input[@name='txtPassword' and @id='txtPassword']");
By loginbtn = By.xpath("//input[@type='submit' and @id='btnLogin']");
public void Login(String UName,String Pwd)
{
**driver**.findElement(username).sendKeys(UName);
**driver**.findElement(password).sendKeys(Pwd);
**driver**.findElement(loginbtn).click();
}
Can someone please explain me the reason why I should not use WebElement declaration using driver in POM model.
LoginPage with WebElement decalarion:
package common;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
public class LoginPage
{
WebDriver driver;
public LoginPage(WebDriver driver)
{
this.driver=driver;
}
WebElement username = driver.findElement(By.xpath("//input[@name='txtUsername' and @id='txtUsername']"));
WebElement password = driver.findElement(By.xpath("//input[@name='txtPassword' and @id='txtPassword']"));
WebElement loginbtn = driver.findElement(By.xpath("//input[@type='submit' and @id='btnLogin']"));
public void Login(String UName,String Pwd)
{
username.sendKeys(UName);
password.sendKeys(Pwd);
loginbtn.click();
}
public void VerifyTitle(String actualtitle, String expectedtitle)
{
Assert.assertEquals(actualtitle, expectedtitle);
}
}
OrangeHRM File:
package frameWork;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import common.LoginPage;
public class OrangeHRM
{
LoginPage LP;
public static WebDriver driver;
@Test
public void Login()
{
System.setProperty("webdriver.chrome.driver", "D://Pavani//Automation Testing//Drivers//chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
LP = new LoginPage(driver);
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
LP.Login("Admin", "admin123");
String actual = "OrangeHRM";
String expected = driver.getTitle();
LP.VerifyTitle(actual, expected);
}
}