黄瓜@Before钩子打开多个浏览器窗口

时间:2016-07-20 09:02:19

标签: java junit cucumber

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

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报告此时钩子参数= 3。

hooks:  size=3   reporter: "null"  tags:  size = 0  isBefore: true

1 个答案:

答案 0 :(得分:0)

  • 您的功能中是否有多个场景? - &GT; @Before方法将在每个场景之前执行。
  • 你有一个不同的stepdef类,带有@Before带注释的方法打开chrome吗? - &GT;在执行场景之前,将调用所有@Before方法。