Selenium 第二次硒测试在第一次测试完成之前开始
我有两个Selenium测试要一个接一个地运行。我将服务员注入测试中,等待元素在与它们交互之前变得可见。 问题是,有时第二个测试在等待第一个测试中加载元素的过程中启动 我是服务员:Selenium 第二次硒测试在第一次测试完成之前开始,selenium,testng,autotest,Selenium,Testng,Autotest,我有两个Selenium测试要一个接一个地运行。我将服务员注入测试中,等待元素在与它们交互之前变得可见。 问题是,有时第二个测试在等待第一个测试中加载元素的过程中启动 我是服务员: public void waitElementIsVisible(String locator) { logger.info("Waiting for element with locator " + locator + " to load in " + driver.getCurrentUrl() + "
public void waitElementIsVisible(String locator) {
logger.info("Waiting for element with locator " + locator + " to load in " + driver.getCurrentUrl() + " page");
WebDriverWait wait = new WebDriverWait(driver, 15, 100);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(locator)));
}
这是使用服务生的方法:
protected void clickJs(String locator){
waiters.waitElementIsVisible(locator);
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement el = findPageElementByXpath(locator);
js.executeScript("arguments[0].click();", el);
}
这是使用上述javascript单击的方法:
public ActivitiesPage goToActivitiesPage(){
clickJs(activitiesPage);
return new ActivitiesPage();
}
这些是测试:
@Test
public void doSuccessfulLoginTest(){
LoginPage page = new LoginPage();
page.doSuccessfulLogin("ir2.0bb16be1@mailosaur.in", "qwerty").
goToActivitiesPage().
goToSignUpFormsPage().
clickCreateSignupFormButton().
specifySignUpFormName(Helper.getCurrentDateAndTime()).
clickNextButton().
selectLayout().
clickNextButton().
clickNextButton().
clickNextButton();
try {
System.out.println("assertions");
Assert.assertEquals(getDriver().getCurrentUrl().contains("dashboard"), true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void doSuccessfulLoginTestAndPublishSignupForm(){
ActivitiesPage ap = new ActivitiesPage();
ap.goToSignUpFormsPage().
clickCreateSignupFormButton().
specifySignUpFormName(Helper.getCurrentDateAndTime()).
clickNextButton().
selectLayout().
clickNextButton().
clickNextButton().
clickNextButton();
try {
System.out.println("assertions");
Assert.assertEquals(getDriver().getCurrentUrl().contains("subscribe"), true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
第二个测试在第一个测试的goToActivitiesPage()方法开始,前提是其中的元素正在等待
我是否可以确保第一个测试在第二个测试开始之前完成?假设您将“用@test注释的分离方法”作为两个selenium测试调用。
发生这种情况的唯一可能性是:
测试配置为并行运行。否则它们将始终按顺序运行,请检查测试报告以确定故障原因。如果是这样,并且您使用的是testNg,那么只需使用名为(
前
调试提示:除非指定并行运行,否则测试始终按顺序运行。检查测试报告以确定失败原因.您没有提供测试方法,但是如果您没有并行运行它们,如果第二个测试开始,则意味着第一个测试结束。在您的情况下,原因可能是
waitElementIsVisible
方法中的异常。您可以添加代码吗?可以清楚地识别问题。在Description中添加的代码Guy说,您的第一个测试必须在第二个测试开始之前完成。您的第一个测试可能由于某些异常甚至超时而失败。
@Test
public void test1() {}
@Test(dependsOnMethods = {"test1"}) //test2 will wait until test1 is finished
public void test2() {}