Tuesday, 5 January 2021

Selenium Learnings - INTERMEDIATE

 Wait Commands


Listing out the different WebDriver Wait statements that can be useful for an effective scripting and can avoid using the Thread.sleep() commands.

  • ImplicitlyWait Command
  • FluentWait Command
  • ExpectedConditions Command
  • PageLoadTimeout Command
  • SetScriptTimeout Command
  • Sleep Command


ImplicitlyWait Command

Purpose: Selenium WebDriver has borrowed the idea of implicit waits from Watir. 

This means that we can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for the entire time the browser is open. 

This means that any search for elements on the page could take the time the implicit wait is set for

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


FluentWait Command

Purpose: Each FluentWait instance 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 whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.


 Wait wait = new FluentWait(driver)

     .withTimeout(30, SECONDS)

     .pollingEvery(5, SECONDS)

     .ignoring(NoSuchElementException.class);

//In the above example, we are declaring a fluent wait with the timeout of 30 seconds and the frequency is set to 5 seconds by ignoring "NoSuchElementException"

   WebElement foo = wait.until(new Function() {

     public WebElement apply(WebDriver driver) {

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

     }   });

   

ExpectedConditions Command 

 Purpose: Models a condition that might reasonably be expected to eventually evaluate to something that is neither null nor false.

WebDriverWait wait = new WebDriverWait(driver, 10);

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(>someid>)));


The following are the Expected Conditions that can be used in Selenium Explicit Wait

  • alertIsPresent()
  • elementSelectionStateToBe()
  • elementToBeClickable()
  • elementToBeSelected()
  • frameToBeAvaliableAndSwitchToIt()
  • invisibilityOfTheElementLocated()
  • invisibilityOfElementWithText()
  • presenceOfAllElementsLocatedBy()
  • presenceOfElementLocated()
  • textToBePresentInElement()
  • textToBePresentInElementLocated()
  • textToBePresentInElementValue()
  • titleIs()
  • titleContains()
  • visibilityOf()
  • visibilityOfAllElements()
  • visibilityOfAllElementsLocatedBy()
  • visibilityOfElementLocated()

PageLoadTimeout Command

Purpose: Sets the amount of time to wait for a page-load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.

driver.manage().timeouts().pageLoadTimeout(100, SECONDS);


SetScriptTimeout Command

Purpose: Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

driver.manage().timeouts().setScriptTimeout(100,SECONDS);


Sleep Command

Purpose: This is rarely used, as it always force the browser to wait for a specific time. Thread. Sleep is never a good idea and that’s why Selenium provides wait for primitives. If you use them you can specify much higher timeout value which makes tests more reliable without slowing them down as the condition can be evaluated as often as it’s required.

thread.sleep(1000);


IMPLICIT WAIT

EXPLICIT WAIT

  • Implicit Wait time is applied to all the elements in the script

 

  • Explicit Wait time is applied only to those elements which are intended by us

 

  • In Implicit Wait, we need not specify "ExpectedConditions" on the element to be located
  • In Explicit Wait, we need to specify "ExpectedConditions" on the element to be located
  • It is recommended to use when the elements are located with the time frame specified in Selenium implicit wait

 

  • It is recommended to use when the elements are taking long time to load and also for verifying the property of the element like(visibilityOfElementLocated, elementToBeClickable,elementToBeSelected)

 


Conclusion:

Implicit, Explicit and Fluent Wait are the different waits used in Selenium. Usage of these waits are totally based on the elements which are loaded at different intervals of time. It is always not recommended to use Thread.Sleep() while Testing our application or building our framework.