Friday 2 March 2018

Published March 02, 2018 by with 0 comment

Difference between ImplicitWait, ExplicitWait, FluentWait, PageLoadTimeOut and Thread.sleep in Selenium WebDriver

Hello Learners,

Synchronization is a very hot topic in Selenium. We have been always asked questions related to synchronization and webdriver waits in interviews. So, lets dig out some good stuff out of synchronization. And next time whenever synchronization related questions are asked in interview answer them confidently with all the conceptual knowledge you will get in this blog.

1. What is Synchronization.
• It is a mechanism which involves more than one components to work parallel with Each other.
• It is a process of coordinating or matching two or more activities/devices/processes in time.
• It is a process of matching the speed of AUT(Application under test) & Test tool in order to get proper execution.

2. Why Synchronization is required.
• Generally in Test Automation, we have two components
1. Application Under Test.
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.

• During the test execution Test tool gives instructions one by one with same speed, but AUT takes less time for some steps execution and more time for some steps execution

So, in order to keep both Application Under Test and Test Automation Tool in sync we use synchronization

3. When Synchronization is required.
• Whenever any step required time more than Synchronization time for execution, then Synchronization is required. 

4. Conditional and Unconditional Synchronization in Selenium WebDriver.


Thread.Sleep:

• Thread.sleep method is used to pause the execution for defined time. Time is defined in milliseconds for this method.

Implicit Wait: 
• During Implicit wait if the Web Driver cannot find it immediately because of its availability, it will keep polling the DOM to get the element.
• If the element is not available within the specified Time an NoSuchElementException will be raised.
• The default setting is zero.
• Once we set a time, the Web Driver waits for the period of the WebDriver object instance.

Explicit Wait:  
• There can be instance when a particular element takes more than a minute to load.
• In that case you definitely not like to set a huge time to Implicit wait, as if you do this your browser will going to wait for the same time for every element.
• To avoid that situation you can simply put a separate time on the required element only.
• By following this your browser implicit wait time would be short for every element and it would be large for specific element.

Fluent Wait: 
• Let’s say you have an element which sometime appears in just 1 second and some time it takes minutes to appear.
• In that case it is better to use fluent wait, as this will try to find element again and again until it find it or until the final timer runs out.

Page Load Time Out:
• The pageLoadTimeout limits the time that the script allots for a web page to be displayed.
• If the page loads within the time then the script continues.

• If the page does not load within the timeout the script will be stopped by a TimeoutException.


The Source Code for above topic is available on GitHub Repository, You can get it from this URL:- 

ExplicitWait.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.synchronization;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class ExplicitWait {

	static WebDriver driver;

	@Test
	public void main() throws InterruptedException {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/src/main/resources/chromedriver");
		driver = new ChromeDriver();

		driver.get("http://the-internet.herokuapp.com/dynamic_loading/1");

		driver.findElement(By.xpath("//button[text()='Start']")).click();
		WebElement content = driver.findElement(By.xpath("//h4[text()='Hello World!']"));

		// Explicit Wait
		WebDriverWait explicitWait = new WebDriverWait(driver, 10);
		explicitWait.until(ExpectedConditions.visibilityOf(content));

		Assert.assertTrue(content.isDisplayed(), "Content is not displayed");
		System.out.println(content.getText());

	}
	
	@AfterTest
	public void close() {
		driver.quit();
	}

}

FluentWait.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.synchronization;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class FluentWait {

	static WebDriver driver;

	@Test
	public void main() throws InterruptedException {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/src/main/resources/chromedriver");
		driver = new ChromeDriver();

		driver.get("http://the-internet.herokuapp.com/dynamic_loading/1");

		driver.findElement(By.xpath("//button[text()='Start']")).click();

		// Fluent Wait
		Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
				.withTimeout(30, TimeUnit.SECONDS)
				.pollingEvery(1, TimeUnit.SECONDS)
				.ignoring(NoSuchElementException.class);

		WebElement content = fluentWait.until(new Function<WebDriver, WebElement>() {
			public WebElement apply(WebDriver driver) {
				return driver.findElement(By.xpath("//h4[text()='Hello World!']"));
			}
		});

		System.out.println(content.getText());
	}
	
	@AfterTest
	public void close() {
		driver.quit();
	}

}

ImplicitWait.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.synchronization;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class ImplicitWait {

	static WebDriver driver;

	@Test
	public void main() throws InterruptedException {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/src/main/resources/chromedriver");
		driver = new ChromeDriver();

		// Implicit Wait
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

		driver.get("http://the-internet.herokuapp.com/dynamic_controls");

		driver.findElement(By.xpath("//button[@id='btn']")).click();

		WebElement checkBox = driver.findElement(By.xpath("//input[@id='checkbox']"));

		Assert.assertTrue(checkBox.isDisplayed(), "checkBox is not displayed");

		driver.findElement(By.xpath("//button[text()='Remove']")).click();

		WebElement message = driver.findElement(By.id("message"));

		Assert.assertTrue(message.isDisplayed(), "Message is not displayed");

	}

	@AfterTest
	public void close() {
		driver.quit();
	}

}

PageLoadTimeOut.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.synchronization;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class PageLoadTimeOut {

	static WebDriver driver;

	@Test
	public void main() {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/src/main/resources/chromedriver");
		driver = new ChromeDriver();

		// Page Load Time Out
		driver.manage().timeouts().pageLoadTimeout(1, TimeUnit.MILLISECONDS);

		driver.get("http://the-internet.herokuapp.com/dynamic_loading/1");

	}

	@AfterTest
	public void close() {
		driver.quit();
	}

}

ThreadSleep.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.synchronization;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

public class ThreadSleep {

	static WebDriver driver;

	@Test
	public void main() throws InterruptedException {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/src/main/resources/chromedriver");
		driver = new ChromeDriver();

		driver.get("http://the-internet.herokuapp.com/dynamic_controls");

		driver.findElement(By.xpath("//button[@id='btn']")).click();

		
		
		// Thread.Sleep()
		Thread.sleep(1000);

		WebElement checkBox = driver.findElement(By.xpath("//input[@id='checkbox']"));

		Assert.assertTrue(checkBox.isDisplayed(), "checkBox is not displayed");

		driver.findElement(By.xpath("//button[text()='Remove']")).click();


		
		// Thread.Sleep()
		Thread.sleep(10000);

		WebElement message = driver.findElement(By.id("message"));

		Assert.assertTrue(message.isDisplayed(), "Message is not displayed");

	}
	
	@AfterTest
	public void close() {
		driver.quit();
	}

}

(Synchronization with all types of waits in WebDriver)

Synchronization.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.synchronization;

import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class synchronization {

	static WebDriver driver;

	public static void main(String[] args) throws InterruptedException {
		System.setProperty("webdriver.chrome.driver",
				System.getProperty("user.dir") + "/src/main/resources/chromedriver");
		driver = new ChromeDriver();
		
		
		// Page Load Time Out
		driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

		// Implicit Wait
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

		// Explicit Wait
		WebDriverWait explicitWait = new WebDriverWait(driver, 30);
		explicitWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("id")));

		// Fluent Wait
		Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)

				// Waiting 30 seconds for an element to be present on the page, checking
				.withTimeout(30, TimeUnit.SECONDS)

				// for its presence once every 5 seconds.
				.pollingEvery(5, TimeUnit.SECONDS)

				.ignoring(NoSuchElementException.class);

		// Thread.sleep()
		Thread.sleep(3000);

		driver.get("http://www.google.com");

	}

}

The Source Code for above topic is available on GitHub Repository, You can get it from this URL:-


0 comments:

Post a Comment