我正在TestNG中使用Selenium Web Driver编写测试用例,并且我有一个依次运行多个测试的场景(参考下面)
@Test(priority =1)
public void Test1(){
}
@Test(priority =2)
public void Test2(){
}
这两个测试都是AJAX调用,其中对话框打开、测试执行、对话框关闭,然后页面顶部出现通知消息,每次成功测试后页面都会刷新。
问题是:Test2不会等待页面刷新/重新加载。假设当Test1成功完成时,Test2将在页面刷新之前开始(即打开对话框,执行场景等),同时页面刷新(这必然发生在Test1成功执行后)。现在由于页面刷新,由于Test2打开的diaog框不再存在,然后Test2失败。
(另外,我不知道刷新页面需要多长时间。因此,我不想在执行Test2之前使用Thread.睡眠(xxxx))
另外,我不认为
driver.navigate().refresh()
将通过将其放在Test2之前来解决我的问题,因为在这种情况下,我的页面将被刷新两次。问题是通过代码发生的刷新(这是不确定的,因为它可能需要1秒、3秒或5秒)
如果您正在等待元素呈现,那么
在selenium rc中,我们曾经使用selenium. WaitForConine("selenium.isElementPresent(\"fContent\")", "需要TimeoutInMilisec")
在Web驱动程序中,您可以使用它实现同样的事情
WebDriverWait myWait = new WebDriverWait(webDriver, 45);
ExpectedCondition<Boolean> conditionToCheck = new ExpectedCondition<Boolean>()
{
@Override
public Boolean apply(WebDriver input) {
return (input.findElements(By.id("fContent")).size() > 0);
}
};
myWait.until(conditionToCheck);
这样,您可以在执行test2之前等待元素出现。
更新:
如果您正在等待页面加载,那么在Web驱动程序中,您可以使用以下代码:
public void waitForPageLoaded(WebDriver driver)
{
ExpectedCondition<Boolean> expectation = new
ExpectedCondition<Boolean>()
{
public Boolean apply(WebDriver driver)
{
return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
}
};
Wait<WebDriver> wait = new WebDriverWait(driver,30);
try
{
wait.until(expectation);
}
catch(Throwable error)
{
assertFalse("Timeout waiting for Page Load Request to complete.",true);
}
}
这是C#中更好且经过测试的WaitForPageLoad
实现版本。网
public void WaitForPageLoad(int maxWaitTimeInSeconds) {
string state = string.Empty;
try {
WebDriverWait wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(maxWaitTimeInSeconds));
//Checks every 500 ms whether predicate returns true if returns exit otherwise keep trying till it returns ture
wait.Until(d = > {
try {
state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();
} catch (InvalidOperationException) {
//Ignore
} catch (NoSuchWindowException) {
//when popup is closed, switch to last windows
_driver.SwitchTo().Window(_driver.WindowHandles.Last());
}
//In IE7 there are chances we may get state as loaded instead of complete
return (state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase));
});
} catch (TimeoutException) {
//sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
throw;
} catch (NullReferenceException) {
//sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls
if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase))
throw;
} catch (WebDriverException) {
if (_driver.WindowHandles.Count == 1) {
_driver.SwitchTo().Window(_driver.WindowHandles[0]);
}
state = ((IJavaScriptExecutor) _driver).ExecuteScript(@"return document.readyState").ToString();
if (!(state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase)))
throw;
}
}
如果您可以触发URL更改,这将起作用:
WebDriverWait wait = new WebDriverWait(yourDriver, TimeSpan.FromSeconds(5));
wait.Until(ExpectedConditions.UrlContains("/url-fragment"));