Spread the love

How To Solve Synchronization Failures In Selenium or QTP?

Spread the love

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

    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")));
    }






Spread the love
READ  Shall We Execute Our Automation Test case in Random Order??
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following topics Details...

Comments

  1. There is obviously a bundle to identify about this. I consider you made some nice points in features also.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love
READ  Optional Step--a new way of Coding in QTP or UFT