Filip Lipiński
Filip Lipiński

Reputation: 3

Is there a way to check if page was reloaded with WebDriver (C#)?

In some of my projects to correctly save forms user needs to perform click on "Save" or "Save changes" button. That click cause whole page to reload (changes done will be visible on page after that reload), but if something is wrong validation will stops page from reloading. I want to create a simple assertion to check if that page was reloaded, if not my test will fail. I tried to use this code:

public bool WasPageRefreshed(float seconds)
{
    DriverLocal.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.MinValue);
    string getReadyState = "return document.readyState;";

    for (int i = 0; i < 10; i++)
    {
        string readyState = GetJsExecutor().ExecuteScript(getReadyState) as string;
        if (readyState != "complete")
            return true;
        Thread.Sleep(TimeSpan.FromMilliseconds(seconds / 10));
    }

    DriverLocal.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(60));
    return false;
}

I use it in Assert.True() to check if page was reloaded but it doesn't work always (I can use it 5 times on same form - 3 times its ok, 2 times test will fail). Is there a way to upgrade it to work correctly in 100% of usage? Or maybe there is another way to check if page was reloaded?

Upvotes: 0

Views: 3534

Answers (3)

nilesh
nilesh

Reputation: 14307

For Webpage load, there is unfortunately no one size fits all solution. Every situation varies. How you wait in your automation suite is very critical. Explicit waits are recommended way of waiting, instead of using Javascript options, you can just wait for a new element after the page load or wait for invisibility of certain element that tells you that the page is loaded

 new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut))
.Until(ExpectedConditions
.ElementExists((By.Id("new Element Id"))));

or

 new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut))
.Until(ExpectedConditions
.InvisibilityOfElementLocated((By.Id("old Element Id"))));

You can check ExpectedElements docs here

Upvotes: 1

Viet Pham
Viet Pham

Reputation: 214

As my opinion, you better use SeleniumWait instead of Sleep. Try this

        public bool WasPageRefreshed(double seconds)
    {
        WebDriverWait wait = new WebDriverWait(DriverLocal, TimeSpan.FromSeconds(seconds));
        wait.PollingInterval = TimeSpan.FromSeconds(1);
        string getReadyState = "return document.readyState;";
        try
        {
            wait.Until(d =>
            {
                string readyState = GetJsExecutor().ExecuteScript(getReadyState) as string;
                if (readyState == "complete")
                    return true;
            });
        }
        catch (Exception)
        {
        }
        return false;
    }

I suggest you Wait for a specific element that marked the status of a refreshed page.

Upvotes: 0

Mesut GUNES
Mesut GUNES

Reputation: 7421

There may be some Ajax call works asynchronously. You can wait until ajax call finished, then do your case, see this:

public void WaitForAjax()
{
    while (true) // break if ajax return false
    {
        var ajaxIsComplete = (bool)(driver as IJavaScriptExecutor).ExecuteScript("return jQuery.active == 0");
        if (ajaxIsComplete)
            break;
        Thread.Sleep(100);
    }
}

Upvotes: 0

Related Questions