Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null
when I try to run below code in testNg
null pointer exception
shown in Eclipse
public class ImgDDChkbxRadio {
WebDriver driver;
@BeforeTest
public void LaunchBrowser()
{
System.setProperty("webdriver.chrome.driver","F:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.leafground.com/");
}
@Test
public void Img()
{
driver.findElement(By.xpath("//img[@src='images/image.png']")).click();
driver.findElement(By.xpath("//*[@src=\"../images/home.png\"]")).click();
driver.navigate().back();
driver.findElement(By.xpath("//*[@src=\"../images/abcd.jpg\"]")).click();
}
}
It could be the the site is not being loaded - are you able to launch the site manually?
If yes try this:
- Make WebDriver object static (write static before the variable name)
- Try to add Thread.Sleep(5000) to LaunchBrowser method, after you open the site using the browser
Just remove the WebDriver from LaunchBrowser() and I hope this will work for you ;)
public class ImgDDChkbxRadio {
WebDriver driver;
@BeforeTest
public void LaunchBrowser()
{
System.setProperty("webdriver.chrome.driver","/Users/chrome/chromedriver");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://www.leafground.com/");
}
@Test
public void Img()
{
driver.findElement(By.xpath("//img[@src='images/image.png']")).click();
driver.findElement(By.xpath("//*[@src=\"../images/home.png\"]")).click();
driver.navigate().back();
driver.findElement(By.xpath("//*[@src=\"../images/abcd.jpg\"]")).click();
}
}