1

Below is the error I am getting after the run of the scenario:-

@scenario
  Scenario: creating a account to user               # /Users/ajaysithagari/Documents/workspace/Selenium_Cucumber/features/signup.feature:6
    Given I am landing on nike homepage              # CommonStepDefination.i_am_landing_on_nike_homepage()
    And I click on join now                          # SignupStepDefination.i_click_on_join_now()
      java.lang.NullPointerException
        at pages.Signup.join(Signup.java:12)
        at step_defination.SignupStepDefination.i_click_on_join_now(SignupStepDefination.java:13)
        at ✽.And I click on join now(/Users/ajaysithagari/Documents/workspace/Selenium_Cucumber/features/signup.feature:8)

    When I provide the user the user details to join # SignupStepDefination.i_provide_the_user_the_user_details_to_join()
    Then I need to see the user details              # SignupStepDefination.i_need_to_see_the_user_details()

1 Scenarios (1 failed)
4 Steps (1 failed, 2 skipped, 1 passed)
0m8.658s

Here is my scenario:- @feature Feature: In order to signup user needs to create the account

@scenario Scenario: creating a account to user Given I am landing on nike homepage And I click on join now When I provide the user the user details to join Then I need to see the user details

Here is step defination for "given":-
package step_defination;
;
import cucumber.api.java.en.Given;
import utils.BrowserandDriver;

public class CommonStepDefination {

        String PageURL = "xxxxx";
        int ImplicitWait = 15;
        int pageLoadTimeOut = 30;
        String browserName = "safari";

        BrowserandDriver BD = new BrowserandDriver();

        @Before
        public void launchBrowser()
        {
            BD.launchBrowser(browserName);
            BD.maximizeBrowser();
            BD.setImplicitWait(ImplicitWait);
            BD.setPageLoadTimeout(pageLoadTimeOut);
        }   

        @Given("^I am landing on nike homepage$")
        public void i_am_landing_on_nike_homepage() throws Throwable {
        BD.launchApp(PageURL);
        }

        @After
        public void tearDown(Scenario scenario) {
        BD.tearDown(scenario);
       }
}


    Here is my step defination:- 

    package step_defination;

    import cucumber.api.java.en.And;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    import pages.Signup;

    public class SignupStepDefination {

        @And("^I click on join now$")
        public void i_click_on_join_now() throws Throwable {
            Signup sign = new Signup();
            sign.join();   
        }
    @When("^I provide the user the user details to join$")
        public void i_provide_the_user_the_user_details_to_join() throws Throwable {
        }
        @Then("^I need to see the user details$")
        public void i_need_to_see_the_user_details() throws Throwable {
        }

    Here is my page:-

    package pages;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;

    public class Signup {

        public static WebDriver driver;

        public void join()
        {
        driver.findElement(By.xpath("/html/body/div[7]/nav/div[1]/ul[2]/li[2]/button")).click();
        }
    }

Every thing is fine but when I mouse over on steps in feature file I am getting this error(Given is working but and, when then are not working) Step 'I click on join now' does not have a matching glue code @feature Feature: In order to signup user needs to create the account

@scenario Scenario: creating a account to user Given I am landing on nike homepage And I click on join now When I provide the user the user details to join Then I need to see the user details

Ajay
  • 21
  • 7
  • 1
    Your `Signup` class declares a static field `WebDriver driver` but you never initialize it. That's the reason that you get a `NullPointerException` while trying to call `driver.findElement()` – Thomas Kläger Jun 02 '17 at 05:37
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Thomas Kläger Jun 02 '17 at 05:37
  • I have a question? I have already initialized the WebDriver driver in BrowserandDriver but how to use the WebDriver driver from this package(BrowserandDriver). I dont want to initialize the webdriver in each package. thanks – Ajay Jun 02 '17 at 06:13
  • @Ajay Can you consider updating us your Cucumber jar versions `cucumber-core`,`cucumber-java`,`cucumber-junit` for further analysis? Thanks – undetected Selenium Jun 02 '17 at 09:27

1 Answers1

0

If your BrowserandDriver class looks like this:

package utils;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.safari.SafariDriver;

public class BrowserandDriver {
    public static WebDriver driver;

    public static void launchBrowser(String browserName) {
        if (browserName.toLowerCase().contains("safari")) {
            driver = new SafariDriver();
        }
    }

    public static void setPageLoadTimeout(int waitTime) {
        driver.manage().timeouts().pageLoadTimeout(waitTime, TimeUnit.SECONDS);
    }

    public static void launchApp(String appURL) {
        driver.get(appURL);
    }
}

(I made all your methods static, since they only operate on the static field driver anyway), then you can write your step classes like this (CommonStepDefination.java):

package step_defination;

import cucumber.api.java.en.Given;
import utils.BrowserandDriver;

public class CommonStepDefination {
    String PageURL = "xxxxx";
    int ImplicitWait = 15;
    int pageLoadTimeOut = 30;
    String browserName = "safari";

    // no need for a BrowserandDriver instance here, since everything in BrowserandDriver is static

    @Before
    public void launchBrowser()
    {
        BrowserandDriver.launchBrowser(browserName);
        BrowserandDriver.maximizeBrowser();
        BrowserandDriver.setImplicitWait(ImplicitWait);
        BrowserandDriver.setPageLoadTimeout(pageLoadTimeOut);
    }
    // in other methods the references to the field BD must also be replaced with the class name BrowserandDriver
}

and Signup.java:

package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class Signup {

    public void join()
    {
        BrowserandDriver.driver.findElement(By.xpath("/html/body/div[7]/nav/div[1]/ul[2]/li[2]/button")).click();
    }
}

On a side note, I would not select the button with By.xpath("/html/body/div[7]/nav/div[1]/ul[2]/li[2]/button") - this will break with almost every change that you do on the html page. It would be better to give the button a unique id (like joinButton) and select it with By.id("joinButton")

Thomas Kläger
  • 17,754
  • 3
  • 23
  • 34
  • Why did you use this.BD = BD; and can you explain me little bit more about this. The BrowserandDriver BD is class how can you use private BrowserandDriver BD; – Ajay Jun 06 '17 at 19:52
  • @Ajay your `CommonStepDefination` class has also a field `BrowserandDriver BD` that you intialize with `BD = new BrowserandDriver();`. If you were to do that within `Signup` too you would get two instances of `BrowserandDriver` with two distinct browsers. So I took a different approach: define a `private BrowserandDriver BD;` field in every step class and initialize it with a `BrowserandDriver` instance that is passed into the class constructor. – Thomas Kläger Jun 06 '17 at 20:18
  • If I use your code I am getting error at getDriver. Here is the error "The method getDriver() is undefined for the type BrowserandDriver" – Ajay Jun 06 '17 at 20:25
  • @Ajay I don't know your class `BrowserandDriver` so it's hard to say what the problem is. I suspect that it somehow has an instance field of type `WebDriver` and you need to access this instance field to call `findElement` on it. – Thomas Kläger Jun 06 '17 at 20:29
  • public class BrowserandDriver { public static WebDriver driver; public void launchBrowser(String browserName) { if(browserName.toLowerCase().contains("safari")) { driver = new SafariDriver(); } public void setPageLoadTimeout(int waitTime) { driver.manage().timeouts().pageLoadTimeout(waitTime, TimeUnit.SECONDS); } public void launchApp(String appURL) { driver.get(appURL); } – Ajay Jun 06 '17 at 21:03
  • @Ajay I've adjusted my anwser accordingly – Thomas Kläger Jun 06 '17 at 21:10
  • Thank you very much – Ajay Jun 06 '17 at 21:18