0

Driver set to null for 2nd test case, working fine for login.

Driver class:

public class Driver {

private static WebDriver driver;;

/**
 * 
 * @return
 * @throws Exception
 */
public static synchronized WebDriver getDriver() throws Exception {

    if (null == driver) {
        Properties properties = Utility.getproperties(Constant.CONFIGURATION_PROPERTIES_FILE_PATH);
        String browser = properties.getProperty(Constant.BROWSER);
        String url = properties.getProperty(Constant.URL);
        switch (browser) {
        case Constant.CHROME:
        //  WebDriverManager.chromedriver().setup();

            WebDriverManager.chromedriver().version(Constant.CHROME_DRIVER_VERSION).setup();
            driver = new ChromeDriver();
             System.out.println(WebDriverManager.chromedriver().getVersions());
            break;
        case Constant.IE:
            WebDriverManager.iedriver().setup();
            //WebDriverManager.iedriver().version(Constant.IE_DRIVER_VERSION).setup();
            driver = new InternetExplorerDriver();
            break;
        case Constant.FIREFOX:
            WebDriverManager.firefoxdriver().setup();
            //WebDriverManager.firefoxdriver().version(Constant.FIREFOX_DRIVER_VERSION).setup();
            driver = new FirefoxDriver();
            break;
        default:
            System.out.println(Message.SUITABLE_DRIVER_NOT_FOUND);
            break;

        }
        if (null != driver) {
            driver.manage().window().maximize();
            driver.get(url);
            driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        }

    }

    return driver;

}

@AfterMethod
public void quitDriver() {
    if (null != driver) {
        driver.close();
        driver.quit();
    }
}
}

Login page:

public class LoginPage {

public LoginPage(WebDriver driver) {
    PageFactory.initElements(driver, this);
}

@FindBy(how = How.ID, using = PageUtility.OKTA_SIGNIN_USERNAME_ID)
@CacheLookup
public WebElement userNameTextField;

@FindBy(how = How.ID, using = PageUtility.SIGNIN_PASSWORD_ID)
@CacheLookup
public WebElement passwordTextField;

@FindBy(how = How.ID, using = PageUtility.SIGNIN_SUBMIT_ID)
@CacheLookup
public WebElement signInButton;
}

LoginBusiness:

public class LoginBusiness extends LoginPage {

public LoginBusiness(WebDriver driver) {
    super(driver);

}
public void loginToApplication() throws IOException {
    userNameTextField.clear();
    Properties properties=Utility.getproperties(Constant.CONFIGURATION_PROPERTIES_FILE_PATH);
    String username=properties.getProperty(Constant.USERNAME);
    userNameTextField.sendKeys(username);
    String password=properties.getProperty(Constant.PASSWORD);
    passwordTextField.clear();
    passwordTextField.sendKeys(password);
    signInButton.click();
    }   
}

LoginTest:

public class LoginTest extends LoginBusiness {

public LoginBusiness loginBusiness;
public static WebDriver driver;

public LoginTest() throws Exception {
    super(driver);
    loginBusiness = PageFactory.initElements(Driver.getDriver(), LoginBusiness.class);
}

@Test
public void loginToGmsrApplication() throws IOException {



    loginBusiness.loginToApplication();



}

Terms&condpage:

public class TermsAndConditionsPage {

private static WebDriver driver;

public TermsAndConditionsPage(WebDriver driver) {
    PageFactory.initElements(driver, this);


}

@FindBy(how = How.XPATH, using = PageUtility.ACCEPT_BUTTON_XPATH)
@CacheLookup
public WebElement acceptButton;

}

Terms&condibusines:

public class TermsAndConditionsBusiness extends TermsAndConditionsPage {

private static WebDriver driver;

public TermsAndConditionsBusiness(WebDriver driver) {
    super(driver);

}

public void clickToAcceptbutton() {

    Assert.assertEquals("Wrong message", "Accept", acceptButton.getText());
    acceptButton.click();


}
}

Terms&condtc:

public class TermsAndConditionsTest extends TermsAndConditionsBusiness{

public TermsAndConditionsBusiness termsAndConditionsBusiness;

public static WebDriver driver;
public TermsAndConditionsTest() throws Exception {

    super(driver);


    termsAndConditionsBusiness = PageFactory.initElements(Driver.getDriver(),TermsAndConditionsBusiness.class);
}

@Test
public void acceptTermsAndConditions() throws Exception {


    termsAndConditionsBusiness.clickToAcceptbutton();

}

}
  • At a guess, it's because you have an @AfterMethod that completely destroys the driver object. I'd suggest debugging and stepping through your code to find out exactly what is happening and working from there. – Kate Paulk May 10 at 11:47
0

You did not set the private static WebDriver driver in TermsAndConditions page object, also the static is unnecessary:

private WebDriver driver;

public TermsAndConditionsBusiness(WebDriver driver) {
    super(driver);
    this.driver = driver;
}
  • why do we need to inialize Webdriver on every page – jay May 13 at 7:14
  • no webdriver initialized for login page as well only its defined in Driver class – jay May 13 at 7:15
  • To use WebDriver objects in other classes you need to pass instance to it. That is why you need to set it in the constructor via this.driver = driver. This way all your page object classes use the same driver object. Now I am not 100% sure if that is the cause of NullPointer here, as you did not provide the log output. Attaching the logs can help with the answer. – Moro May 13 at 7:20
0

Just as Moro suggested you are not using the correct driver instance in term and condition. Below pointers can help:

  1. remove public static WebDriver driver; from all page, business and testcase classes as you are getting it in constructor there is no need for new instance in each class

  2. In test case class use super(Driver.getDriver()) in constructor instead super(driver)

  3. Last move your @afterMethod to testcase class from Driver class. As it won't be running in driver class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.