Strategy for selenium-webdriver in javascript for testing in different environments

I am doing functional tests using selenium-webdriver using yadda library. The problem is that the same test suite works differently in my different environments. Example:

In tests, the result is different from the environment I enter.

Local localhost: 5000

Open my search sitewhen i go to my site: 2169mswhen i write a text on the search input: 21mswhen i click the search button: 130ms
      ․ then i get the results page.": 46ms

      

Mystaging.domain.com stage

 Open my search site: 
         StaleElementReferenceError: {"errorMessage":"Element is no longer attached to the DOM","request":{"headers":{"Accept":"application/json; charset=utf-8","Connection":"close","Content-Length":"2"

      

Products www.domain.com

   Open my search sitewhen i go to my site: 2169mswhen i write a text on the search input: 21mswhen i click the search button: 130ms
      ․ then i get the results page.": 46ms

      

Currently only test cases fail, but in other situations where the internet connection slows down, the tests fail in production but go through the stage. The main problem is that the browser doesn't have a DOM ready to test, and it doesn't find the element it needs to test.

My approach to trying to solve this, wait for my page root element to appear like this:

return driver.wait(() => driver.isElementPresent(By.css(".my__homepage")), 50000);

      

But this is not enough for me, because test sets still fail randomly. So my question is:

What might be the best approach to run in different environments a test suite that contains elements that are not ready in the browser?

+3


source to share


1 answer


I am showing you a simple C # code that I made to work on IE, but it may work similarly for other browsers and languages.

private static WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(120)); //max driver wait timeout 120 seconds

try
{
    //waiting for document to be ready
    IJavaScriptExecutor jsExecutor = (IJavaScriptExecutor)driver;
    wait.Until(sc => jsExecutor.ExecuteScript("return document.readyState").Equals("complete"));

    //waiting for an element.
    //use 'ExpectedConditions.ElementToBeClickable' as in some cases element gets loaded but might not be still ready to be clicked
    wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("elementID")));
}
catch (Exception ex)
{

}

      



  • Basically I expect the document to be ready using a JavaScript executor
  • Also I put an implicit wait on every element that I would access. I am using the expected condition as ElementToBeClickable because sometime ElementIsPresent doesn't mean the element can be retrieved according to your needs.

Note. Also, you may need to check other properties of the element (like enable / disable, etc.) depending on your needs.

0


source







All Articles