【发布时间】:2019-02-06 09:42:49
【问题描述】:
我设置了一个非常简单的测试场景,它基本上会打开一个浏览器并进行一些导航,并且一切正常。当我将一些步骤分离到一个 common_steps 文件中时,浏览器的多个实例 (4x) 正在打开,这反过来又导致测试失败,因为找不到元素。
当我将常用步骤复制回原始文件时,测试运行良好。我有点困惑,因为当功能文件中没有额外的步骤或行告诉它这样做时,我不明白它是如何打开多个浏览器的。
这是我如何处理它的一个例子。我知道代码有点杂乱,需要全局等待等,但在我解决此问题后会完成,并且正在进行中。
场景
Scenario Outline: Navigate through Chrome
Given I opened the home page of "<homeUrl>"
Then navigated to my favourite site of "<myFavUrl>"
Then navigated to the Dojo page
When the full catalog was displayed
And the performance option was selected
Examples:
|homeUrl |myFavUrl |
|https:\\www.google.co.uk |https://www.ministryoftesting.com/ |
常量文件。
public class Constant {
public WebDriver driver;
public Constant() {
System.setProperty("webdriver.chrome.driver", "path_to\\chromedriver.exe");
driver = new ChromeDriver();
}
public WebDriver setChromeDriver() {
if(driver == null) {
driver = new ChromeDriver();
driver.manage().window().maximize();
return driver;
}else
return driver;
}
}
常用步骤文件。
public class CommonSteps extends Constant {
@Given("^I opened the home page of \"([^\"]*)\"$")
public void navigateToHomePage(String url) throws Throwable{
driver.get(url);
driver.manage().window().maximize();
}
@Then("^navigated to my favourite site of \"([^\"]*)\"$")
public void navigateToFavourite(String myFavSite) throws Throwable{
driver.get(myFavSite);
}
//To be used in a separate scenario
@Given("^I opened the home page of Ministry Of Testing$")
public void quickLinkToMot() throws Throwable{
driver.get("https://www.ministryoftesting.com/");
driver.manage().window().maximize();
}
}
具体步骤文件
@Then("^navigated to the Dojo page$")
public void navigateToDojo() throws Throwable{
WebDriverWait wait = new WebDriverWait(driver, 5000);
WebElement djLinkParent = driver.findElement(By.id("navbar-collapse"));
WebElement djLink = wait.until(ExpectedConditions.elementToBeClickable(djLinkParent.findElement(By.linkText("Dojo"))));
djLink.click();
}
@When("^the full catalog was displayed$")
public void displayFullCatalog() throws Throwable{
WebDriverWait wait = new WebDriverWait(driver, 5000);
WebElement fullCatBtn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("[class='btn btn-xl btn-home']")));
fullCatBtn.click();
}
@And("^the performance option was selected$")
public void selectPerformance() throws Throwable{
WebDriverWait wait = new WebDriverWait(driver, 5000);
WebElement pBtnParent = driver.findElement(By.cssSelector("[class='cat cat2']"));
WebElement perfLink = wait.until(ExpectedConditions.elementToBeClickable(pBtnParent.findElement(By.xpath("//a[@href='/dojo/lessons?topic=performance']"))));
perfLink.click();
}
【问题讨论】:
-
嗨@Jd_Daniels,您如何启动/导航新站点 - 在同一窗口或新浏览器窗口中?
-
Constant 类中
setChromeDriver()方法的用户是什么?你在哪里使用它?
标签: selenium selenium-webdriver cucumber cucumber-java