I'm getting a java.lang.NullPointerException
when running a test case via Selenium webDriver in Chrome (Page factory model)
Testcase:
package Google;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;
import Google.OpenGoogle;
public class TestNew {
@Test
public void f() throws InterruptedException {
WebDriver driver = BrowserHandler.BrowserFactory("Chrome");
OpenGoogle OpenGoogleobj = PageFactory.initElements(driver, OpenGoogle.class);
OpenGoogleobj.Googletext();
}
}
Pages:
package Google;
import java.security.PublicKey;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class OpenGoogle {
@FindBy(how = How.XPATH, using = ".//*[@id='lst-ib']")
WebElement search ;
static WebDriver driver;
public OpenGoogle (WebDriver driver)
{
this.driver = driver ;
}
public void Googletext() throws InterruptedException
{
Thread.sleep(4000);
search.sendKeys("Ankit");
}
BrowserHandler:
package Google;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserHandler {
public static WebDriver driver;
public static WebDriver BrowserFactory(String BrowserName) {
if(BrowserName.equals("Chrome"))
{
System.setProperty("webdriver.chrome.driver", "/Users/apple/eclipse-workspace/Selenium/src/Google/chromedriver");
ChromeDriver driver = new ChromeDriver() ;
driver.manage().window().maximize();
driver.get("https://google.com");
}
return driver;
}
}
TestNG message:
java.lang.NullPointerException
Note: After debugging i am getting problem at " search.sendkeys" m its takine me to some other inbuild class
[ testResult.setThrowable(ite.getCause());]
2 Answers 2
Check your BrowserHandler
class. You define driver as a static field of the class. Then in BrowserFactory
you declare another "local" driver
. You instantiate that local object but you return the driver
that is a class field which is still null. What you need to do first of all to make your code work is refactor your BrowserHandler
so that it looks like:
public class BrowserHandler {
public static WebDriver BrowserFactory(String BrowserName) {
System.setProperty("webdriver.chrome.driver", "/Users/apple/eclipse-workspace/Selenium/src/Google/chromedriver");
ChromeDriver driver = new ChromeDriver() ;
if(BrowserName.equals("Chrome"))
{
driver.manage().window().maximize();
driver.get("https://google.com");
}
return driver;
}
}
Now your code should work well..
Apart from this particular issue I would strongly recommend you to:
- Learn the Java language conventions about how to name methods and variables.
- Remove
Thread.sleep
fromGoogletext
method unless you just need to make sure you have the text entered to the search box. Even if you need to see the text entered you still need to moveTread.sleep
so that it is after thesendKeys
method method (not before one). - Remove
driver
field fromOpenGoogle
class - Remove constructor from
OpenGoogle
class
Now you have more-or-less well-designed "Hello world" for POM.
-
I am trying one more framework, can you please help me with that sqa.stackexchange.com/questions/32413/…Batu-QA– Batu-QA2018年03月08日 16:45:38 +00:00Commented Mar 8, 2018 at 16:45
You make two webdriver instances in OpenGoogle class and BrowserHandler class, Because of that, webdriver cannot understand which webdriver instance to use for sendkeys action, that's why it gives null point exception.
Create only one webdriver instance to the whole project and use it everywhere you want, the best place is create webdriver instance is beforemethod or beforetest under your TestBase class
Explore related questions
See similar questions with these tags.
search
initialized? I see it declared in your code, but it isn't initialized, so any attempt to use it is going to throw a Null Pointer Exception.