1
我不清楚爲什麼我會爲以下示例打開3個Chrome瀏覽器。我有一個@Before(cucumber版本)註釋,可以在場景運行之前簡單地設置一個chrome webdriver實例。據我所知,它應該打開一個瀏覽器,運行場景(step defs),然後使用@After黃瓜掛鉤關閉。什麼情況是2個窗口打開第三個也是最後窗口前實際執行的步驟:Cucumber @Before hook打開多個瀏覽器窗口
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個場景「,」您有不同的stepdef類,使用@Before註釋方法打開chrome嗎?「 - 不僅一步def ! – Steerpike
對不起,你是對的,@Before還被另外兩個類別調用過兩次 – Steerpike