0
Browser utility:

package com.gmsr.utility;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BrowserUtility {

    public static WebDriver driver;
    public static WebDriver openBrowser(WebDriver driver, String browserName, String url) throws InterruptedException {

        if(browserName.equals("chrome")) {

            System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
            driver=new ChromeDriver();
            driver.manage().window().maximize();
            driver.get(url);
            driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
            return driver;




        }else
        if(browserName.equals("IE")) {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(5000); 
        return driver;


    }else
    if(browserName.equals("Firefox")) {
        System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.manage().window().maximize();
        driver.get(url);
        Thread.sleep(5000); 
        return driver;
    }
        return driver;

}

public static WebDriver getDriver() throws Exception { if(driver == null) {

        PropertiesFileReader obj=new PropertiesFileReader(); 
        Properties properties=obj.getproperty();
        openBrowser(properties.getProperty("browserName"), properties.getProperty("URL"));
    }else {
        return driver;
    }
    return driver;
}


}

Login page :

package com.gmsr.pageobjects;

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.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class LoginPage {

    WebDriver driver;

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

    }

    @FindBy(how=How.ID,using="ignin-username")
    public WebElement usernametexbox;

    @FindBy(how=How.ID,using="signin-password")
    public WebElement passwordtextbox;

    @FindBy(how=How.ID,using="signin-submit")
    public WebElement signin;

    @FindBy(how=How.XPATH,using="//button[@class='-primary']")
    public WebElement acceptbutton;

   public void enter_username(String username) {

    usernametexbox.clear();
    usernametexbox.sendKeys(username);
    usernametexbox.getText();
    }

    public void enter_password(String password) {

        passwordtextbox.clear();
        passwordtextbox.sendKeys(password);
    }

    public void clickToSigninbutton() {
        signin.click();
    }

    public void clickToAcceptbutton() {


    acceptbutton.click();

    }

public void fill_LoginDetails() {

    enter_username("gggg");
    enter_password("fffff");



    }


}  

Logout page:

package com.gmsr.pageobjects;

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 com.gmsr.stepdef.StepDefinition;

public class LogoutPage  {

    WebDriver driver;

    public LogoutPage() {



    }

    @FindBy(how=How.XPATH,using="//*[@class='icon-caret']")
    public WebElement chevron;

    @FindBy(how=How.XPATH,using="//a[@class='logout-link']")
    public WebElement logoutlink;

    public void clickTochevron() {
        chevron.click();
    }

    public void clickToLogoutLink() {


        logoutlink.click();

    }

    }

Login step definition:

package com.gmsr.stepdef;

import java.util.Properties;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import com.gmsr.pageobjects.LoginPage;
import com.gmsr.utility.BrowserUtility;
import com.gmsr.utility.PropertiesFileReader;

import cucumber.api.PendingException;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;

public class StepDefinition {

/*    public WebDriver getDriver() {
        return getDriver();

      } */


       public static WebDriver driver;

        private LoginPage loginpage;



    Properties properties=new Properties();
    PropertiesFileReader obj=new PropertiesFileReader(); 


    @Given("^Open browser and enter url$")
    public void open_browser_and_enter_url() throws Throwable {



     Properties properties=obj.getproperty();
     driver=BrowserUtility.openBrowser(driver, properties.getProperty("browserName"), properties.getProperty("URL"));

    }

    @Then("^Enter username and password$")
    public void enter_username_and_password() throws Throwable {

    LoginPage loginpage=new LoginPage(driver);
    loginpage.fill_LoginDetails();

            }

    @Then("^click on sign in button$")
    public void click_on_sign_in_button() throws Throwable {

        new LoginPage(driver).clickToSigninbutton();
        System.out.println("Sign-In successfully");

    }


    @Then("^Terms and conditions page open and click on Accept button$")
    public void terms_and_conditions_page_open_and_click_on_Accept_button() throws Throwable {


        new LoginPage(driver).clickToAcceptbutton();
    }



}  

Logout stepdef:

package com.gmsr.stepdef;

import java.util.Properties;

import org.openqa.selenium.WebDriver;

import com.gmsr.pageobjects.LoginPage;
import com.gmsr.pageobjects.LogoutPage;
import com.gmsr.utility.BrowserUtility;
import com.gmsr.utility.PropertiesFileReader;

import cucumber.api.java.After;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;



public class Logoutstepdef{

public static WebDriver driver;

// private StepDefinition stepdefinition; public void loadDriver() throws Exception { this.driver = BrowserUtility.getDriver(); }

public LogoutPage logoutpage = new LogoutPage();

// PropertiesFileReader obj=new PropertiesFileReader();

@Given("^Chevron near username$")
public void chevron_near_username() throws Throwable {

loadDriver();

// Properties properties=obj.getproperty();
/driver= BrowserUtility.openBrowser(driver, properties.getProperty("browser.Name"), properties.getProperty("URL"));/ // stepdefinition.open_browser_and_enter_url(); // stepdefinition.click_on_sign_in_button(); // stepdefinition.terms_and_conditions_page_open_and_click_on_Accept_button();

//  LogoutPage logoutpage=new LogoutPage();


   logoutpage.clickTochevron(driver);
}

@And("^click on chevron and it should get expands$")
public void click_on_chevron_and_it_should_get_expands() throws Throwable {

    System.out.println("when user click on checvron it should further expands a window");

}

@And("^click on Logout link$")
public void click_on_Logout_link() throws Throwable {

//  logoutpage.clickToLogoutLink();
    new LogoutPage().clickToLogoutLink(driver);

}

} Login step definition is working fine but getting nullpointerexception for Logout step definition.

Pls help to resolve this- I have updated my code and now getting null value for element chevron and logoutlink available in logoutpage

  • According to your code structure you have not made the instance of BrowserUtility , so i will suggest you that just add this line in your Logout class ``` Properties properties=obj.getproperty();``` ~~~ driver=BrowserUtility.openBrowser(driver, properties.getProperty("browserName"), properties.getProperty("URL")); ~~~ – Upkar Singh Apr 29 at 7:27
  • No it doesnt work still getting the null pointer exception – jay Apr 29 at 7:32
  • Yes, you can not create driver instance multiple time for one test, you need to make a base class then inherit that base class in your test class, it will work . – Upkar Singh Apr 29 at 7:36
  • we cannot inherit any class into step definition file, cucumber doesnt allow to do so, do you have any alternative solution for the same I am stuck – jay Apr 29 at 9:09
  • why are you using this public LogoutPage() { this.driver=StepDefinition.driver; } if this is page factory class? – Upkar Singh Apr 29 at 9:15

Your Answer

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

Browse other questions tagged or ask your own question.