简体   繁体   English

Selenium-IDE:如何验证/声明页面刷新

[英]Selenium-IDE: How to verify/assert page refresh

I have a link on a page, which refreshes this page when clicked. 我在页面上有一个链接,单击该链接可以刷新此页面。

How can I verify with Selenium-IDE that the page has really been refreshed/reloaded? 如何使用Selenium-IDE验证页面是否确实已刷新/重新加载?

I've solved it by asserting that an element which was originally present on the page, is not present on the page right after refresh, then wait untill the page is fully refreshed, and assert that the element is present again. 我通过断言最初存在于页面上的元素,刷新后不立即在页面上存在,然后等到页面完全刷新并断言该元素再次存在来解决此问题。

  1. refreshAndWait / or clickAndWait on refresh link/button refreshAndWait /或在刷新链接/按钮上单击AndWait
  2. assertElementNotPresent somePageSpecificElement / check that refresh was really executed assertElementNotPresent somePageSpecificElement /检查是否确实执行了刷新
  3. pause 2000ms / wait for refresh to end 暂停2000ms /等待刷新结束
  4. assertElementPresent somePageSpecificElement / check that refresh was really executed and same page has been loaded assertElementPresent somePageSpecificElement /检查是否确实执行了刷新并且已加载同一页面

UPDATE: Page refresh can also easily be verified if there is some textbox element present. 更新:如果存在一些文本框元素,也可以轻松地验证页面刷新。 Simply use type command to insert some text into the textbox field, then refresh the page, and the text inside textbox should return to it's original state. 只需使用type命令将一些文本插入到文本框字段中,然后刷新页面,文本框内的文本应返回到其原始状态。

Simple way is to wait until last element on page. 简单的方法是等到页面上的最后一个元素。

In Selenium-IDE you may use a lot of wait*-commands to do this, for example: 在Selenium-IDE中,您可以使用很多wait *命令来执行此操作,例如:

waitForElementPresent [element_xPath] waitForElementPresent [element_xPath]

waitForVisible [element_xPath] waitForVisible [element_xPath]

and so on. 等等。

If like me you found that checking for the visibility and invisibility of elements can be unreliable, there is an alternative. 如果像我一样,您发现检查元素的可见性和不可见性可能不可靠,则有另一种选择。

If your website uses jQuery, I found that the most reliable method is to check the status of jQuery.active to determine a page refresh. 如果您的网站使用jQuery,我发现最可靠的方法是检查jQuery.active的状态以确定页面刷新。

For example, In C# I use the following method: 例如,在C#中,我使用以下方法:

public bool CheckPageHasRefreshed()
    {
        try
        {
            // Wait for jQuery to become active (the page is refreshing)
            wait.Until(x => (((IJavaScriptExecutor)WebDriver).ExecuteScript("return jQuery.active != 0")));
            // Wait for jQuery to become inactive (the page refresh has completed)
            wait.Until(x => (((IJavaScriptExecutor)WebDriver).ExecuteScript("return jQuery.active === 0")));
            return true;
        }
        catch (WebDriverTimeoutException)
        {
            return false;
        }
    }

This works because jQuery.active will only return 0 when all back-end loading activity on your page has completed. 之所以可行,是因为jQuery.active仅在页面上的所有后端加载活动完成后才返回0。 If stuff is still happening behind the scenes, it won't. 如果幕后仍然有事情发生,那就不会了。

What you could assume is - if the element is found, and element.click() has been called, the element has been clicked. 您可以假设的是-如果找到了元素,并且调用了element.click(),则单击该元素。

But for good practice - you should catch an exception if the element is not found in the DOM. 但出于良好作法-如果在DOM中找不到该元素,则应捕获一个异常。 IE if it goes into catch block, then you know the page has not been refreshed. IE,如果它进入catch块,那么您知道页面尚未刷新。

try{
    WebElement element = driver.findElement(selector); //The refresh link user clicks
    element.click();
    clicked = true;
}catch(Exception e){
    clicked = false;
    throw new ElementNotFoundException();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM