I am getting java.lang.NullPointerException while executing my test case, I am using Page Object Modle with cucumber framework in my maven project, following are the code snippets BoxJunitScript.java
package com.stepDefinition;
public class BoxJunitScipt {
WebDriver driver;
WebDriverWait wait;
LoginPage lp;
AllFilesPage afp;
static DriverInstance instance;
{
instance = DriverInstance.getinstance();
driver = instance.getDiver();
wait = instance.getWait();
}
@Before
public void setUp() {
driver.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
lp = new LoginPage(driver);
afp = new AllFilesPage(driver);
}
@Given("user navigates to App box page")
public void user_navigates_to_App_box_page() {
driver.manage().window().maximize();
driver.get("https://app.box.com");
lp.pageVerify();
}
@When("user enters username and password given below")
public void user_enters_username_and_password_given_below(DataTable loginData) {
List<List<String>> loginData1 = loginData.asLists(); //convert data in list
for(List<String> row: loginData1) {
String email = row.get(0);
String password = row.get(1);
lp.loginIntoApp(email,password);
}
}
@Then("page with title {string} should open")
public void page_with_title_should_open(String title) throws InterruptedException {
Thread.sleep(3000);
afp.pageVerify(title);
}
@When("user clicks on LogOut")
public void user_clicks_on_LogOut() {
afp.logoutFromApp();
driver.quit();
}
}
LoginPage.java
package com.stepDefinition;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage {
private WebDriver driver;
WebDriverWait wait;
@FindBy(how=How.NAME, using="login")
WebElement emailTextField;
@FindBy(how=How.CSS, using="button[id='login-submit']")
WebElement nextButton;
@FindBy(how=How.NAME, using="password")
WebElement passwordTextField;
@FindBy(css="button[id='login-submit-password']")
WebElement logInButton;
public LoginPage(WebDriver driver) {
this.driver = driver;
AjaxElementLocatorFactory ajaxDriver = new AjaxElementLocatorFactory(driver, 45);
PageFactory.initElements(ajaxDriver, LoginPage.class);
}
public void loginIntoApp(String username, String password) {
emailTextField.sendKeys(username);
nextButton.click();
passwordTextField.sendKeys(password);
logInButton.click();
}
public void pageVerify() {
System.out.println("Before getting title");
String title = driver.getTitle();
System.out.println("After getting title");
if(title.equals("Box | Login"))
System.out.println("Page title is correct");
}
public static void waitUntil(WebElement element, WebDriver driver) {
new WebDriverWait(driver, 60)
.ignoring(NoSuchElementException.class)
.ignoring(StaleElementReferenceException.class)
.until(ExpectedConditions.visibilityOf(element));
}
}
AllFilePage.java
package com.stepDefinition;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AllFilesPage {
WebDriver driver;
WebDriverWait wait;
@FindBy(css="button[data-resin-target='accountmenu']")
WebElement toggleAccountMenu;
@FindBy(css="a[data-resin-target='logout']")
WebElement logOutlink;
public AllFilesPage(WebDriver driver) {
this.driver = driver;
AjaxElementLocatorFactory ajaxDriver = new AjaxElementLocatorFactory(driver, 45);
PageFactory.initElements(ajaxDriver, AllFilesPage.class);
}
public void logoutFromApp() {
toggleAccountMenu.click();
logOutlink.click();
}
public void pageVerify(String title) {
String Actualtitle = driver.getTitle();
if(Actualtitle.equals(title)) {
System.out.println("Page title is correct123");
} else {
System.err.println("Page title is not correct");
}
}
}
CreateInstance.java
package com.stepDefinition;
import com.driver.DriverInstance;
import io.cucumber.core.api.Scenario;
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
public class CreateInstance {
public static String browserName = null;
@Given("Browser is {string}")
public void browsere_is(String browser) {
// Write code here that turns the phrase above into concrete actions
if(browserName == null) {
DriverInstance.setInstance(browser);
browserName = browser;
}
if(browserName!= null && !browserName.equals(browser)) {
DriverInstance.setInstance(browser);
browserName = browser;
}
}
@Before
public void beforeScenario(Scenario sc) {
System.out.println(sc.getName());
System.out.println("Runs before all");
}
@After
public void afterScenario(Scenario sc) {
System.out.println(sc.getStatus());
}
}
DriverInstance.java
package com.driver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class DriverInstance {
public static DriverInstance instance;
WebDriver driver;
WebDriverWait wait;
public DriverInstance(String browser) {
if(browser.equals("firefox")) {
driver = new FirefoxDriver();
}
else if(browser.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", "D:\\selenium-server\\Driver\\chromedriver.exe");
driver = new ChromeDriver();
}
wait = new WebDriverWait(driver, 60);
}
public static void setInstance(String browser) {
DriverInstance.instance = new DriverInstance(browser);
}
public static DriverInstance getinstance() {
return instance;
}
public WebDriver getDiver() {
return driver;
}
public WebDriverWait getWait() {
return wait;
}
}
I am getting this error on line lp.pageVerify() in BoxJunitScript.java and when i debug my code i observed that i am getting null value for lp object after this line lp = new LoginPage(driver) in BoxJunitScript.java
-
maybe coz you are using the Driver as private in LoginPage.Updesh Kumar– Updesh Kumar2019年12月23日 09:38:08 +00:00Commented Dec 23, 2019 at 9:38
-
` lp = new LoginPage(driver); lp.pageVerify();` will workNarendraR– NarendraR2019年12月23日 09:48:07 +00:00Commented Dec 23, 2019 at 9:48
-
I'm not sure whether 'setup method' is executed or not. can you debug that using some print statement over there ?NarendraR– NarendraR2019年12月23日 09:57:26 +00:00Commented Dec 23, 2019 at 9:57
-
@NarendraR after debugging I observed that setup method is not getting executed and when I initialize LoginPage object lp before the statement lp.pageVerify it worked, but it throws another null pointer exception to the statement where I am calling again the method of login page i.e. lp.loginIntoApp(email, password); and in line emailTextField.sendKeys(username); of LoginPage.javaAman Kumar– Aman Kumar2019年12月23日 11:01:23 +00:00Commented Dec 23, 2019 at 11:01
-
@UpdeshKumar this solution doesn't work for me.Aman Kumar– Aman Kumar2019年12月23日 11:01:58 +00:00Commented Dec 23, 2019 at 11:01
1 Answer 1
I am not sure that you have provided a proper code or you have right understanding of where your exception appears.
As per your code:
{
instance = DriverInstance.getinstance();
driver = instance.getDiver();
wait = instance.getWait();
}
you get a reference to a static field instance = DriverInstance.getinstance();
that is basically you can read as static DriverInstance instance = DriverInstance.getinstance();
So after this line you do not have an object. Thus calling driver = instance.getDiver();
should cause NPE.
You never create an instance of your DriverInstance
because the instance can be created in two ways.
- Calling constructor
public DriverInstance(String browser)
- Calling static method
public static void setInstance(String browser)
So I believe you need to reconsider your architecture and use static
things only where they are really improve your experience.
Explore related questions
See similar questions with these tags.