Getting nullpointer exception for 2nd test case working fine for login



  • 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();
    
    }
    
    }
    


  • 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;
    }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2