Java Cucumber@在钩子打开多个浏览器窗口之前

Java Cucumber@在钩子打开多个浏览器窗口之前,java,junit,cucumber,Java,Junit,Cucumber,我不清楚为什么我在下面的例子中打开了3个chrome浏览器。我有一个@Before(cucumber版本)注释,可以在场景运行之前简单地设置一个chromewebdriver实例。就我所见,它应该打开一个浏览器,运行场景(step defs),然后使用@After cucumber钩子关闭。在第三个窗口(即最后一个窗口)实际执行以下步骤之前打开两个窗口: Scenario: Given I am on the holidays homepage When I select the

我不清楚为什么我在下面的例子中打开了3个chrome浏览器。我有一个@Before(cucumber版本)注释,可以在场景运行之前简单地设置一个chromewebdriver实例。就我所见,它应该打开一个浏览器,运行场景(step defs),然后使用@After cucumber钩子关闭。在第三个窗口(即最后一个窗口)实际执行以下步骤之前打开两个窗口:

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
步骤Def:

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) 

  • 您的功能中是否有多个场景?->@Before方法将在每个场景之前执行
  • 您是否有另一个带有@Before注释方法的stepdef类打开chrome?->在执行场景之前,将调用所有@Before方法

“您的功能中有不止一个场景吗?”-没有,只有一个场景”,“您有不同的stepdef类,带有@Before注释的方法打开chrome吗?“-不只是一步定义!对不起,您是对的,在另外两个类中,@Before也被调用了两次。那么解决方法是什么?对于其他场景,也应执行相同的步骤。如何实现这一点?文档说:“Cucumber JVM不支持只运行一次钩子”,因此您需要根据您想要实现的目标寻找替代方案。
hooks:  size=3   reporter: "null"  tags:  size = 0  isBefore: true