Selenium Webdriver Page Load Synchronization

When you run selenium test scripts, you may encounter “No Such Element Exception”, “Element Not Found Exception” or “Stale Element Reference Exception”. Some times it is because of the web page element which need to be used is not loaded completely. In selenium, there has below two method to resolve these kind of issues.

  1. Unconditional Synchronization.
  2. Conditional Synchronization.

Unconditional Synchronization

We can use Thread.sleep(milliseconds) or Wait() method to make the selenium test scripts waiting for a period of time. And this time duration is enough for the web element to load complete.

Disadvantage: If the wait time is more than the time needed for web element load complete, it will waste some time.

Conditional Synchronization

The selenium test scripts will wait for the web element to load complete until a specified condition occurred.

But you should provide a timeout value with the condition. This timeout value can avoid block situation for the test script.When the wait timeout, the test script will continue to run.

Selenium provide below conditional synchronization wait methods.

Implicit Wait

  1. After setting implicit wait time, the test scripts will try to find the web element or web elements until the specified amount of wait time timeout.
  2. If the web element has been found during the wait time, it will returned immediately. This can reduce the waste wait time.
  3. This setting will take effect for the entire life time of the webdriver instance.
  4. So you should define it after initialization of the webdriver object.
  5. The default value is 0.
  6. It will take effect only for webdriver’s findElement() or findElements() methods.
  7. If not find required web element, findElement() will throw a NoSuchElementException, and findElements() will return an empty list without any exception.
  8. Example Java Code
    	private void testImplicitWait()
    	{
    		// Initiate FirefoxDriver object. 
    		WebDriver ffDriver = new FirefoxDriver();
    		
    		// Set the implicit wait time.
    		ffDriver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    	
    		// Time recorder.
    		long startTime = 0;
    		long endTime = 0;
    		long deltaTime = 0;
    		
    		startTime = System.currentTimeMillis();
    		// Get the bing home page.
    		ffDriver.get("http://www.bing.com");
    		endTime = System.currentTimeMillis();
    		
    		deltaTime = endTime - startTime;
    		System.out.println("Get bing.com use " + deltaTime + " milliseconds.");
    		
    		startTime = System.currentTimeMillis();
    		// Find the bing search box by it's id.
    		By byId = By.id("sb_form_q");
    		ffDriver.findElement(byId);
    		endTime = System.currentTimeMillis();
    		
    		deltaTime = endTime - startTime;
    		
    		System.out.println("Find bing.com search box use " + deltaTime + " milliseconds.");
    		
    		startTime = System.currentTimeMillis();
    		try
    		{
    			// Find a not exist web element.
    			byId = By.id("text-field-container-not-exist");
    			List<WebElement> webEleList = ffDriver.findElements(byId);
    		}catch(NoSuchElementException ex)
    		{
    			ex.printStackTrace();
    		}
    
    		endTime = System.currentTimeMillis();
    		
    		deltaTime = endTime - startTime;
    		
    		System.out.println("Find bing.com not exist web element use " + deltaTime + " milliseconds.");
    		
    		ffDriver.quit();
    	}
  9. Output
    Get bing.com use 2756 milliseconds.
    Find bing.com search box use 8515 milliseconds.
    Find bing.com not exist web element use 15019 milliseconds.

Explicit Wait

  1. This method will wait until desired condition happened or specified time timeout.
  2. It is always used to wait for a specified web element or it’s attribute changed after perform an action. Such as after you click a button, you wait for a image to be visible.
  3. WebDriverWait class is used to implement the explicit wait object.
  4. ExpectedConditions class is used to create the required condition.
  5. It will throw TimeoutException if the setting timeout passed.
  6. Java Code Example
    	private void testExplicitWait()
    	{
    		// Initiate FirefoxDriver object. 
    		WebDriver ffDriver = new FirefoxDriver();
    	
    		// Time recorder.
    		long startTime = 0;
    		long endTime = 0;
    		long deltaTime = 0;
    		
    		startTime = System.currentTimeMillis();
    		// Get the bing home page.
    		ffDriver.get("http://www.bing.com");
    		endTime = System.currentTimeMillis();
    		deltaTime = endTime - startTime;
    		System.out.println("Load bing.com home page use " + deltaTime + " milliseconds.");
    		
    		
    		startTime = System.currentTimeMillis();
    		// Declare search button Explicit wait object.
    		WebDriverWait waitSearchButton = new WebDriverWait(ffDriver, 10);
    		By byId = By.id("sb_form_q");
    		waitSearchButton.until(ExpectedConditions.elementToBeClickable(byId));
    	    
    		endTime = System.currentTimeMillis();
    		
    		deltaTime = endTime - startTime;
    		System.out.println("Bing.com search button become clickable use " + deltaTime + " milliseconds.");
    		ffDriver.quit();
    	}

Fluent Wait

  1. Can customize maximize timeout value for a condition.
  2. Can define poll frequency duration time.
  3. Can define ignore exception class.
  4. Java Example Code
    	private void testFluentWait()
    	{
    		// Initiate FirefoxDriver object. 
    		WebDriver ffDriver = new FirefoxDriver();
    		
    		// Create FluentWait object.
    		FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(ffDriver);
    		
    		// Set timeout time to 10 seconds.
    		fluentWait.withTimeout(10, TimeUnit.SECONDS);
    		
    		// Find the web element for every 5 seconds. 
    		fluentWait.pollingEvery(5, TimeUnit.SECONDS);
    		
    		// Ignore NoSuchElementException
    		fluentWait.ignoring(NoSuchElementException.class);
    		
    		// Time recorder.
    		long startTime = 0;
    		long endTime = 0;
    		long deltaTime = 0;
    		
    		startTime = System.currentTimeMillis();
    		ffDriver.get("http://www.bing.com");
    		
    		WebElement notExistWebEle = fluentWait.until(ExpectedConditions.visibilityOfElementLocated(By.id("abc")));
    		endTime = System.currentTimeMillis();
    		
    		deltaTime = endTime - startTime;
    		System.out.println("Find a not exist web element use " + deltaTime + " milliseconds.");
    		
    		ffDriver.quit();
    	}

Download “TestPageLoadSynchronization.zip” TestPageLoadSynchronization.zip – Downloaded 52 times – 1 KB

(Visited 402 times, 1 visits today)
READ :   How To Setup Selenium Grid In Selenium Testing Automation

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.