Cucumber @Before 钩子打开多个浏览器 windows

Cucumber @Before hook opening multiple browser windows

我不清楚为什么我会为以下示例打开 3 chrome 个浏览器。我有一个@Before(黄瓜版本)注释,可以在场景 运行 之前简单地设置一个 chrome webdriver 实例。据我所知,它应该打开一个浏览器,运行 场景(步骤 defs),然后使用 @After cucumber 挂钩关闭。发生的事情是 2 windows 在第三个也是最后一个 window 实际执行步骤之前打开:

Scenario:
    Given I am on the holidays homepage
    When I select the departure location "LON"
    And I select the destination location "PAR"
    And I submit a search
    Then search results are displayed

步骤定义:

public class FindAHolidayStepDefs {

    private WebDriver driver;

    @Before
    public void setup() {
        System.setProperty("webdriver.chrome.driver",
                System.getProperty("user.dir") + "\drivers\chromedriver.exe");
        driver = new ChromeDriver();
    }

    @Given("^I am on the Holidays homepage$")
    public void IAmOnTheThomasCookHomepage() {
        driver.get("http://uat7.co-operativetravel.co.uk/");
        driver.manage().window().maximize();
        String pageTitle = driver.getTitle();

        assertEquals("the wrong page title was displayed !", "Holidays - welcome", pageTitle);
    }


    @When("^I select the departure location \"([^\"]*)\"$")
    public void ISelectTheDepartureLocation(String departureAirport) {
        WebElement dropDownContainer = driver.findElement(By.xpath("(//div[@class=\"custom-select departurePoint airportSelect\"])[1]"));
        dropDownContainer.click();

        selectOption(departureAirport);
    }

    @When("^I select the destination location \"([^\"]*)\"$")
    public void ISelectTheDestinationLocation(String destinationAirport) {
        WebElement destinationField = driver.findElement(By.xpath(("(//div[@class=\"searchFormCol destinationAirport\"]/div[@class=\"combinedInput searchFormInput\"]/span/input)[1]")));
        destinationField.sendKeys(destinationAirport);
        selectOption("(" + destinationAirport + ")");
    }


    @When("^I submit a search$")public void iSubmitASearch() throws Throwable {
        WebElement submitButton = driver.findElement(By.xpath("(.//*[@type='submit'])[1]"));
        submitButton.click();
    }


    @Then("^search results are displayed$")
    public void searchResultsAreDisplayed() throws Throwable {
        waitForIsDisplayed(By.xpath(".//*[@id='container']/div/div[3]/div/div[1]/div/h3"), 30);
        assertThat(checkPageTitle(), equalTo("Package Results"));
    }


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

当我单步执行 Intellij 中的代码时,会调用以下方法:

private void runHooks(List<HookDefinition> hooks, Reporter reporter, Set<Tag> tags, boolean isBefore) 

并且 Intellij 报告此时 hooks paramter = 3。

hooks:  size=3   reporter: "null"  tags:  size = 0  isBefore: true
  • 您的功能中有不止一个场景吗? -> @Before 方法将在每个场景之前执行。
  • 您是否有不同的 stepdef class 和打开 chrome 的带有 @Before 注释的方法? -> 所有@Before 方法将在执行场景之前被调用。