Search code examples
javaselenium-chromedrivercucumber-java

Cucumber and WebDriver, opening chrome when it is not necessary


I'm starting with cucumber, following some tutorials and such. I have two feature files, one with basic stuff you see on cucumber's "get started", another one from WebDriver's "get started".

The tests run successfully, but cucumber opens the browser even for the feature/steps that don't ask for it.

project
|--src/test/java/packages
                   |--StepDefinitions.java
                   |--RunCucumberTest.java
                   |--GoogleSearchSteps.java
|--src/test/resources/packages
                        |--google_search.feature
                        |--is_it_friday_yet.feature

is_it_friday_yet.feature:

Feature: Is it Friday yet?
  Everybody wants to know when it's Friday

  Scenario Outline: Today is or is not Friday
    Given today is "<day>"
    When I ask whether it's Friday yet
    Then I should be told "<answer>"

  Examples:
    | day            | answer |
    | Monday         | Nope   |
    | Tuesday        | Nope   |
    | Wednesday      | Nope   |
    | Thursday       | Nope   |
    | Friday         | YES    |
    | Saturday       | Nope   |
    | Sunday         | Nope   |
    | 12345                  | Nope   |
    | Icecream       | Nope   |

google_search.feature:

Feature: Google Search Cheese
  Example of how to test web pages with cucumber

    Scenario: Finding some cheese
   Given I am on the Google search page
   When I search for "Cheese!"
   Then the page title should start with "cheese"

StepsDefinition.java

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

import static org.junit.Assert.*;


class IsItFriday {
    static String isItFriday(String today) {
        return "Friday".equals(today) ? "YES" : "Nope";
    }
}

public class StepDefinitions {

    private String today;
    private String actualAnswer;

    @Given("today is {string}")
    public void today_is(String today) {
        this.today = today;
    }

    @When("I ask whether it's Friday yet")
    public void i_ask_whether_it_s_Friday_yet() {
        actualAnswer = IsItFriday.isItFriday(today);
    }

    @Then("I should be told {string}")
    public void i_should_be_told(String expectedAnswer) {
        assertEquals(expectedAnswer, actualAnswer);
    }

GoogleSearchSteps.java

import io.cucumber.java.After;
import io.cucumber.java.Before;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class GoogleSearchSteps {

    private WebDriver driver;

    @Before
    public void createDriver() {
        System.setProperty("webdriver.chrome.driver","mypath\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Given("I am on the Google search page")
    public void i_am_on_the_Google_search_page() {
        driver.get("https:\\www.google.com");
    }

    @When("I search for {string}")
    public void i_search_for(String query) {
        WebElement element = driver.findElement(By.name("q"));
        // Enter something to search for
        element.sendKeys(query);
        // Now submit the form. WebDriver will find the form for us from the element
        element.submit();
    }

    @Then("the page title should start with {string}")
    public void the_page_title_should_start_with(String titleStartsWith) {
        // Google's search is rendered dynamically with JavaScript
        // Wait for the page to load timeout after ten seconds
        new WebDriverWait(driver, 10L).until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver d) {
                return d.getTitle().toLowerCase().startsWith(titleStartsWith);
            }
        });
    }

    @After()
    public void closeBrowser() {
        driver.quit();
    }

The tests are running ok, they are basically copy and paste from the tutorials with a few modifications, and they run with success also, the problem is really a browser window being opened for every test.

So, how can I make WebDriver be used only for the GoogleSearch feature?

Thanks in advance.


Solution

  • To use WebDriver only for the Google Search feature scenarios you should use a conditional hook. You can associate your Before hook with a following tag expression:

    @Before("@browser")
    public void createDriver() {
    ...
    }
    

    and in the feature file:

    @browser
    Feature: Google Search Cheese
      Example of how to test web pages with cucumber
      ...