Dilip Agheda
Dilip Agheda

Reputation: 2547

Selenium - How to verify loader that appears on a page refresh

I have this test case:

  1. Refresh Page
  2. Loader appears for a short while. This is actually just a DIV with

    aria-label = 'Loading'

  3. Verify that loader element is visible on the page while the page is refreshing

  4. Verify that loader element is not visible on the page while the page is finished loading.

This is the code i wrote:

   _driver.Navigate().Refresh();
    IList<IWebElement> loaders = _driver.FindElements(By.XPath("//*[@aria-label='Loading']"));
    loaders.Count.Should().Be(1);

My problem is that when Refresh() executes, the page refreshes so quickly that subsequent lines never find the element. I was thinking to write some lengthy loop that continuously refreshes the page in a background thread while foreground thread checks the presence of loader. and once loader is found, it will stop the background thread.

Is it a good approach? what are the alternatives? Or, This test case is not a good fit for automation.

Many Thanks

Upvotes: 1

Views: 727

Answers (2)

Mate Mrše
Mate Mrše

Reputation: 8444

Here's an idea. I haven't tested it myself, but try to adjust it to your case and see if it works.

If you try to click the element that is no longer present in the DOM you might get a Stale Element Reference Exception.

So you could use a try-catch block to catch that exception:

try {
    _driver.Navigate().Refresh();
    IList<IWebElement> loaders = _driver.FindElements(By.XPath("//*[@aria-label='Loading']"));
    loaders.Count.Should().Be(1);
} catch (StaleElementReferenceException) {
    //do something with this
}

Upvotes: 1

akshay patil
akshay patil

Reputation: 688

So you can add wait 2 or 3 seconds after .Refresh() and after that you can continue with your actions. so you have create the loop and the loop will be iterate until the it reaches to the element, so that's why you loop is ending so quickly you can create loop for some time like initiate the loop at i=0 then iterate until i reaches to 100 and in between that you can find element or check the availability of the element. I have also shared the link with you of Check Visibility of Web Elements Using Various Types WebDriver Commands

so happy to help let us know that if this works

https://www.softwaretestinghelp.com/webdriver-commands-selenium-tutorial-14/

Upvotes: 1

Related Questions