Header Ads

How To Solve Synchronization Failures In Selenium or QTP?

While automation testing using selenium tool for webpages , I am facing issue with Synchronization. Individually tests are passing. But while running in nightly mood manner, these are failing. One step failure leads to multiple failures in my testing.
I tried with Thread.sleep(30)..but found that is very much unreliable and prone to error. 
Thread.sleep will halt script at the step we are using and not at every step. selenium.setspeed halts script after each action/step performed on the application and this will be time consuming way.
I was googling for solution ..and I found this link http://mark-story.com/posts/view/selenium-synchronization-with-custom-events . This is a very nice solution indeed. Even the latches given here. . http://sauceio.com/index.php/2011/02/advanced-selenium-synchronization-with-latches/ is good to me. 

This is one important aspect of QTP automation test execution.Application speed depends on several external factors but QTP executes the code faster. In such condition it may so happen that QTP execution becomes faster than application . In this case execution fails.To avoid such condition we can use the below for synchronization:




  • Checkpoints applicable for UFT/QTP
  • Adding more sync time[wait]
  • Inserting synchronizing point
  • Through code like exists() or waitproperty()
In the same context, we can also see how to handle ajax calls:
Issues:

  • Partial loading of website: It can be addressed by 
  • try{
    thread.sleep(<required time>);
    }
    catch(Exception e)
    {
    }
    
  • Explicit wait:This will wait for a certain condition to occur before proceeding further
  • WebDriver driver=new FirefoxDriver();
    driver.get(URL);
    WebElement myDynamicElement=(new WebDriver Wait(driver,10)).until
    (ExpectedCondition.presenceofElementLocated(By.id("myElement")));
    
  • Implicit Wait: It is to tell webdriver to poll the DOM for a certain amount of time when trying to find an element if they are not immediately available. The default settings is zero.Once set the implicit wait, it is set for the life span of the webdriver object instance. It is taken from watir.
  • WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().implicitlywait(10,TimeUnit.SECONDS);
    driver.get(URL);
    WebElement element=driver.Element(By.id("myElement"));
    
  • Using Javascript
  • If an application triggers an ajax call, we need to wait until the call has ended. But using thread.sleep(),implicit or explicit wait is not clean and proper approach the browser does not change much while fetching data from server. jQuery keeps a count of how many ajax calls are active in its ajax.active variable. The implementation is described here
    public void waitForAjax(int timeOutSeconds)
    {
    try
    {
    if (driver instanceof JavaScriptExecutor)
    {
    JavaScriptExecutor jsDriver=(JavaScriptExecutor) driver;
    for(int i=0;i<timeOutSeconds;i++)
    {
    object numberOfAjaxConnections=jsDriver.executeScript("return jQuery.active");
    if(numberOfAjaxConnections instanceOf long)
    {
    Long n=(Long) numberOfAjaxConnections;
    if(n.longValue()==0L)
    {
    break;
    }
    Thread.sleeep(100);
    }
    }
    catch(InterruptedException e)
    {
    }
    }
    
  • Using Fluent Wait
  • Selenium webdriver provides Fluent wait option to handle uncertain waits. The advantages of this approach is that element polling mechanism is configurable.
    FluentWait<By>fluentWait=new FluentWait<By>
    (By.tagName("TextArea"));
    //description of the element for which we want to poll
    fluentWait.pollingEvery(300,TimeUnit.MILLISECONDS);
    //it will ping in every 3 seconds
    fluentWait.withTimeOut(1000,TimeUnit.MILLISECONDS);
    fluentWait.until(new Predicate<By>(){
    public boolean apply(By by)
    {
    return browser.findElement(by).isDisplayed();
    }
    catch(NoSuchElement ex)
    {
    return false;
    }
    }
    });
    browser.Element(By.tagName("TextArea")).sendKeys("Hi");
    
  • Using WebDriver Wait
  • public ExpectedCondition<WebElement>
    visibilityElementLocated(final By by)
    {
    retuen new ExpectedCondition<WebElement>()
    {
    public WebElement apply(WebDriver driver)
    {
    WebElement element=driver.findElement(by);
    return element.isDisplayed()?element:null;
    }
    };
    }
    public void performSomeAction(){
    //your code
    Wait wait=new WebDriverWait(driver,20);
    WebElement element=wait.until(visibilityofElementLocated(By.tagName("a")));
    }
    
    
    
    

Powered by Blogger.