I have created this program using TestNG and i'm getting this error java.lan*g.NullPointerException I have initialized the Webelement .But still i'm getting error
POM.java
package TestPOM;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class POM {
public WebDriver driver;
@FindBy(how=How.XPATH,using ="//a[@href='/tasks/tasklist.do']")
public WebElement lnk_MyAccount;
@BeforeClass
public void openBrowser() {
System.setProperty("webdriver.chrome.driver","E:\\Users\\codeRefactor\\CodeRefactor\\Jar\\chromedriver_win32\\Chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get(" url");
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@BeforeMethod
public void Login()
{
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");
driver.findElement(By.id("loginButton")).click();
}
@Test
public void createcustomr(){
PageFactory.initElements(driver, POM.class);
lnk_MyAccount.click();
}
@AfterMethod
public void Logout() throws InterruptedException
{
Thread.sleep(20000);
driver.findElement(By.xpath("//a[text()='Logout']")).click();
}
@AfterClass
public void closeBrowser()
{
driver.close();
}
}
can someone help me on this?
1 Answer 1
When you construct the page using PageFactory.initElements(driver, POM.class);
the new instance of the page is created so that the field that you are referring is still null
.
What you are doing though is pretty tricky. I would rework your design actually. However changing the mentioned line to PageFactory.initElements(driver, this);
might help you..
P.S. - The latter injects the fields into existing object rather than creates a new one of the specified type.
Explore related questions
See similar questions with these tags.