Advanced Selenium

Implicit Wait,Explicit Wait,Fluent Wait,TakesScreenshot

Synchronization

The communication between Selenium WebDriver script and the Application under Test (AUT) is known as synchronization.

Resolution:

Insert wait statements in the WebDriver script.

Unconditional Wait:

Unconditional Wait is achieved using Thread.sleep(long milliseconds). Thread is class in java and sleep is a static method.
This needs to be added before the statement which needs to be find out.
Thread.sleep(5000); // 5 seconds…
driver.findElement(By.id(“username”)).sendKeys(“selenium”);
——————————————————————————————————————————

Conditional Wait:

a. IMPLICIT WAIT

This statement is applicable for all the findElement() and findElements() methods in the program.

Scenario

  • Available immediately proceed to next line
  • Not available immediately and available after 5 few seconds goto next line.
  • Not Available with in 30 seconds wait for 30 secs and throw exception

b. EXPLICIT WAIT

Explicit Wait is mainly useful to apply wait on a single element. In order to achieve the Explicit Wait, we have to use a class called WebDriverWait. AJAX calls are handled using explicit wait in selenium webdriver.

  • WebDriverWait wait = new WebDriverWait(driver,30);
  • wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(“”));
  • wait.until(ExpectedConditions.isAlertPresent());
  • wait.until(ExpectedConditions.textToBePresentElement(Expected,By.id(“”)));
  • wait.until(ExpectedConditions.titleContains(“”));
  • wait.until(ExpectedConditions.titleIs(“”));

WARNING: Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times. For example setting an implicit wait of 10 seconds and an explicit wait of 15 seconds, could cause a timeout to occur after 20 seconds.

Reference URL : https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp

NoSuchElementException Occurs due to following reasons:

1. Property values are Dynamic.
2. Locator is wrong
3. Element is in the frame.
4. Element is taking time to load.
5. Status/Message is taking time to display.

Taking a Screenshot

Using a FirefoxProfile

Using ChromeOptions

Leave a Reply

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