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