gpt4 book ai didi

java - Cucumber @Before 钩子(Hook)打开多个浏览器窗口

转载 作者:太空宇宙 更新时间:2023-11-04 12:24:58 25 4
gpt4 key购买 nike

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

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 报告此时 Hook paramter = 3。

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

最佳答案

  • 您的功能中是否有不止一种场景? -> @Before 方法会在每个场景之前执行。
  • 您是否有一个不同的带有@Before注释方法的stepdef类来打开chrome? -> 所有@Before 方法都会在场景执行之前调用。

关于java - Cucumber @Before 钩子(Hook)打开多个浏览器窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com