Synchronization in Selenium WebDriver

http://www.youngasianescorts.co.uk/?baletos=%D8%A7%D9%84%D8%AE%D9%8A%D8%A7%D8%B1%D8%A7%D8%AA-%D8%A7%D9%84%D8%AB%D9%86%D8%A7%D8%A6%D9%8A%D8%A9-review.com-%D9%8A%D9%88%D9%85%D9%8A%D8%A7&c6e=37 الخيارات الثنائية review.com يوميا Synchronization in Selenium WebDriver

  • Thread.sleep()
  • PageLoadTimeout
  • Implicit wait
  • WebDriver wait
  • Fluent wait

a fantastic read Synchronization in Selenium WebDriver:

It is a mechanism which involves more than one component to work parallel with Each other.

Generally in Test Automation, we have two components
petites annonces rencontres 71 1. Application under Test
http://www.airlinetest.com/?milyokrt=flirten-w%C3%A4hrend-beziehung&0ae=74 2. Test Automation Tool.

Both these components will have their own speed. We should write our scripts in such a way that both the components should move with same and desired speed, so that we will not encounter “Element Not Found” errors which will consume time again in debugging.

When our automation execution starts, then there should be fair communication between tool and application. 
What I mean here is, if the tool is too fast of execution and the application/objects are not fully loaded/not ready by that time, then our automation test cases will fail. So the tool should wait (appropriate) till the objects are present/ready in the application, so that communication/synchronization happens between the tool and application so the chances of our test cases will pass.

Synchronization can be classified into two categories:

mujeres solteras venta cochabamba Unconditional Synchronization:
In this we just specify timeout value only. We will make the tool to wait until certain amount of time and then proceed further.

cherche fille en france pour mariage Examples: Wait() and Thread.Sleep();

The main disadvantage for the above statements is that, there is a chance of unnecessary waiting time even though the application is ready.

The advantages are like in a situation where we interact for third party systems like interfaces, it is not possible to write a condition or check for a condition. Here in these situations, we have to make the application to wait for certain amount of time by specifying the timeout value.

http://emilymarchblog.com/maglayd/2890 Conditional Synchronization:

We specify a condition along with timeout value, so that tool waits to check for the condition and then come out if nothing happens.

It is very important to set the timeout value in conditional synchronization, because the tool should proceed further instead of making the tool to wait for a particular condition to satisfy.

In Selenium we have implicit Wait, WebDriver Wait and Fluent wait as conditional statements.

Thread.sleep(), Implicit wait, WebDriver wait and fluent wait

Thread.sleep()It is a java method which is generally used to overcome the synchronization problem.

The disadvantage of Thread.sleep() is that it’s simply stop the execution of the program for the specified duration irrespective of whether the page load slowly or quickly.

Example:

Thread.sleep(30000);
Here the execution is halted for 30 Sec., even if the object you are looking exists in 10 sec. So here tool unnecessarily waits for 20 sec.
Execution won’t wait after 30 sec.s even if the object does not available, so the chances of your Test fails.

This can be overcome using following wait statements

  • Implicit Wait
  • WebDriver Wait
  • Fluent Wait

Implicit Wait:

During Implicit wait if the Web Driver cannot find it immediately because of its availability, The WebDriver will wait for mentioned time and it will not try to find the element again during the specified time period.

Once the specified time is over, it will try to search the element once again the last time before throwing exception.

Implicit wait is set for the life of the WebDriver object instance.

The default setting is zero.

Once we set a time, the Web Driver waits for the period of the WebDriver object instance.

Example:

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("Your URL");
WebElement o_Element = driver.findElement(By.id("Object Id"));

PageLoadTimeout:
It is again an extension of implicit wait but it used in page loading. It sets the amount of time Selenium to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

Syntax:-

driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);

it will waits for 20 seconds for page to load

WebDriverWait: 
We can tell the tool to wait only till the Condition met. Once the condition is met, the tool proceed with the next step.
This can be done with WebDriverWait in conjunction with ExpectedConditions Class. 
There are few methods supported in ExpectedConditions class to support synchronisation.
Example:

WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement o_element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Object Id")));

Here the tool waits a maximum time of 30 Sec., if the object you are looking is displayed in 10 sec. then the execution proceeds with the next step afte 10 secs. rather than waiting for 30 secs.

If you dont want to include any methods in ExpectedConditions class, then you can use below code:

WebDriver driver = new FirefoxDriver();
driver.get("Your URL");
WebElement o_Element = (new WebDriverWait(driver, 30))
  .until(new ExpectedCondition<WebElement>(){
    @Override
    public WebElement apply(WebDriver d) {
        return d.findElement(By.id("Object Id"));
    }})

Here WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully or wait for maximum of 30 sec.
Fluent Wait:

Fluentwait in selenium webdriver is one of the most versatile wait which will give you the complete power to handle any kind of situation based on your requirement.

In a simple Statement,

Fluent wait = Explicit Wait + frequency with which control need to be checked on application +  exceptions to be ignored like NoSuchElementExceptions.

It defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions while waiting, such as NoSuchElementExceptions when searching for an element on the page.

Example:

public void test(){

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)

       .withTimeout(30, SECONDS)

       .pollingEvery(5, SECONDS)

       .ignoring(NoSuchElementException.class);


   WebElement foo = wait.until(new Function<WebDriver, WebElement>() {

     public WebElement apply(WebDriver driver) {

       return driver.findElement(By.id("Element"));

     }

   }

Above code snippet will Wait 30 seconds for an element to be present on the page and check for its presence once every 5 seconds.