2

I am automating a web application with selenium c#.

After login I have to handle a spinner which appears in the home page.

I have given explicit wait

wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(LocatorSelect(locator, locatorvalue)));

And the result is getting passed and script wait for the element to be visible.

But after validating the spinner visibility I am validating the invisibility of the same element.

But the element still waits more than 30-40 seconds even if I have given 10 seconds. Then after around 40 seconds it is not throwing any error and continues to execute the script without any exception

public void Spinner_Check(string locator, string locatorvalue)
{
    int count = 1;

    WebDriverWait waits = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
    waits.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementIsVisible(LocatorSelect(locator, locatorvalue)));

    while (count > 0)
        {



        waits.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(By.XPath(locatorvalue)));
        IList<IWebElement> spinner_element = driver.FindElements(By.XPath(locatorvalue));
        int size = spinner_element.Count;
            if (size != 1)
            {
                count = 0;
                WriteLine("pass", "Spinner is dismissed for the page");
                break;
            }
        }
}

So how can I avoid that much wait time and execute the script soon after the spinner is dismissed from the page.

Few observations in the script:

  1. When I inspect the html, when the element is visible, I could see style of the display attribute is 'Block'.But after spinner is dismissed from the UI, I could see, xpath showing no element in the search area : //iframe/parent::body//div[text()='Loading..']

  2. For debugging purpose I had set wait for element visible after spinner dismissed from the page. Then I could see timeout exception and NoSuchElement exception triggered.

BABURAJ VD
  • 69
  • 2
  • 11

1 Answers1

1

You need to consider a few things:

  • It's unclear if you have a seperate test to validate the visibility of the spinner but invisibility of the spinner is a valid usecase. So only InvisibilityOfElementLocated() may solve your purpose.
  • Spinner_Check method looks as a complete overhead and you can replace it with just one line of code.

Solution

To validate the InvisibilityOfElementLocated e.g. the spinner you can use the following solution:

new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.InvisibilityOfElementLocated(LocatorSelect(locator, locatorvalue)));

Reference

You can find a couple of relevant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352