0

The single feature file contains 2 Scenarios.

Scenario: Go to
    Given Open Firefox
    When Go to "https://somewebsite.com/"
Scenario: Find profile icon
    Given Find user profile icon

StepDefination class contains the glue methods for above both scenarios.

  • In first Scenario go to: I Created a webdriver object and navigated to the link.
  • In the second scenario I tried to use the same webdriver object but the webdriver has become null.

For the workaround I created a separate class that will instantiate and store the webdriver and from my StepDefination class I can get the driver and use it across munltiple scenario without getting NullPointerException.

But if I create an object in a StepsDefination class and try to use the same object across scenarios then the object becomes null to next scenario.

I also found an answer here https://stackoverflow.com/a/69468458 which says "Objects are persisted along the single scenario. You set the value in one scenario and try to read it in another This is why you get null."

I just want to know how the Feature file and the step definition file are working ?

Code for reference

For the scenario Go to:

    @Given("Open Firefox")
    public void open_firefox() {
        driver = new FirefoxDriver();
    }
    @When("Go to {string}")
    public void go_to(String url) {
        driver.get(url);
    }

For the above 2 scenarios opens the browser and navigate to the link. In the same step file, there is another glue method for the next scenario which will use the existing driver and find the element.

    @Given("Find user profile icon")
    public void find_user_profile_icon() {
        driver.findElement(); // now in this line the driver becomes null and thorws an exception.
    }
2
  • What is your question exactly - since you said that you already know that the state is persistent only within a scenario, not across scenarios? Jul 18 at 15:01
  • @JoãoFarias I just want to know how the Cucumber and Java work together. How the multiple scenarios in a single file are executing? Jul 19 at 14:54

0

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.