Selenium-IDE: How to verify/assert page refresh

2020-03-30 03:23发布

问题:

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?

回答1:

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
  2. assertElementNotPresent somePageSpecificElement / check that refresh was really executed
  3. pause 2000ms / wait for refresh to end
  4. assertElementPresent somePageSpecificElement / check that refresh was really executed and same page has been loaded

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.



回答2:

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:

waitForElementPresent [element_xPath]

waitForVisible [element_xPath]

and so on.



回答3:

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.

For example, In C# I use the following method:

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. If stuff is still happening behind the scenes, it won't.



回答4:

What you could assume is - if the element is found, and element.click() has been called, the element has been clicked.

But for good practice - you should catch an exception if the element is not found in the DOM. I.E if it goes into catch block, then you know the page has not been refreshed.

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