Cucumber 和 WebDriver,不需要的时候打开 chrome

Cucumber and WebDriver, opening chrome when it is not necessary

我从 cucumber 开始,遵循一些教程等。 我有两个功能文件,一个包含你在 cucumber 的 "get started" 上看到的基本内容,另一个来自 WebDriver 的 "get started"

测试 运行 成功,但 cucumber 即使 feature/steps 不需要它也能打开浏览器。

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

测试 运行 正常,它们基本上是从教程中复制和粘贴并进行一些修改,它们 运行 也成功了,问题确实是浏览器 window 每次测试都打开。

那么,如何让 WebDriver 仅用于 GoogleSearch 功能?

提前致谢。

您的 @Before 注释函数将为每个测试生成一个新的 WebDriver 实例。

你可以用

之类的东西跳过它
@Before
public void createDriver(Scenario scenario) {
    if(!scenario.getName().equals("GoogleSearch")){
        System.setProperty("webdriver.chrome.driver","mypath\chromedriver_win32\chromedriver.exe");
        driver = new ChromeDriver();
    }  
}

注意:您需要将 scenario 传递给 createDriver() 方法。

要仅将 WebDriver 用于 Google 搜索功能场景,您应该使用 conditional hook。您可以将 Before 挂钩与以下标记表达式相关联:

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

在功能文件中:

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