Selenium:如何使Web驱动程序在执行另一项测试之前等待页面刷新 [英] Selenium: How to make the web driver to wait for page to refresh before executing another test

查看:94
本文介绍了Selenium:如何使Web驱动程序在执行另一项测试之前等待页面刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用TestNG中的Selenium Web Driver编写测试用例,并且有一个场景,我要依次运行多个测试(请参阅下文)

I am writing Test Cases using Selenium web Driver in TestNG and I have a scenario where I am running multiple tests sequentially (refer below)

@Test(priority =1)
public void Test1(){
}
@Test(priority =2)
public void Test2(){ 
}

两个测试都是AJAX调用,其中会打开一个对话框,执行测试,然后关闭对话框,然后在页面顶部显示通知消息,并且每次成功测试后页面都会刷新.

Both Tests are AJAX calls where a Dialog box opens, tests executes, then dialog box closes and then a notification message appears on top of page and after each successful test the page refreshes.

问题是:Test2不等待页面刷新/重新加载.假设Test1成功完成后,Test2将在页面刷新之前启动(即,它打开对话框,执行场景等.),同时页面刷新(这肯定在Test1成功执行之后发生).现在,由于页面刷新,不再存在由于Test2而打开的对话框,然后Test2失败.

The problem is: Test2 do not wait for the page to refresh/reload. Suppose when Test1 gets successfully completed, Test2 will start before page refresh (ie it opens the dialog box, executes scenarios etc..) meanwhile the page refreshes (which was bound to happen after successful execution of Test1 ). Now since the page refreshes, the diaog box opened due to Test2 is no longer present and then Test2 fails.

(而且,我不知道刷新页面需要多长时间.因此,我不想在执行Test2之前使用Thread.sleep(xxxx))

(Also, I do not know how long it will take to refresh the page. Therefore, I do not want to use Thread.sleep(xxxx) before executing Test2)

我也不认为

driver.navigate().refresh()

将把它放在Test2之前可以解决我的问题,在这种情况下,我的页面将刷新两次.问题是代码正在刷新(不确定,可能需要1秒钟,3秒钟或5秒钟)

will solve my problem by putting it before Test2 as in that case my page will get refreshed twice. The problem is the refresh that is happening through the code (which is uncertain as it may take 1 sec or 3 sec or 5 sec)

推荐答案

如果您正在等待元素显示,那么

If you are waiting for element to present then

在硒rc中,我们通常使用selenium.WaitForCondition("selenium.isElementPresent(\"fContent\")", "desiredTimeoutInMilisec")

In selenium rc we used to do this using selenium.WaitForCondition("selenium.isElementPresent(\"fContent\")", "desiredTimeoutInMilisec")

在Web驱动程序中,U可以使用此功能实现相同的目的

In web driver U can achieve the same thing using this

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之前等待元素出现.

This way you can wait for your element to be present before executing your test2.

已更新:

如果您正在等待页面加载,则可以在Web驱动程序中使用以下代码:

If you are waiting for page to load then in web driver you can use this code:

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);
    }
}

这篇关于Selenium:如何使Web驱动程序在执行另一项测试之前等待页面刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆