Selenium WebDriver
Benefits of Selenium
Four components of Selenium
Difference b/w Selenium IDE, RC & WebDriver
Selenium WebDriver Architecture
Background when user execute selenium code
Download and Install Java
Download and Install Eclipse
Download Selenium WebDriver
Selenium WebDriver Locators
Selenium - Launch Browser
Selenium WebDriver Waits
Selenium- Implicit wait
Selenium- Explicit wait
Selenium- Fluent wait
Selenium- Commonly used commands
Selenium- findElement & findElements
Selenium- Selenium-Handling check Box
Selenium- Handling Radio button
Selenium- Handling drop down
Selenium- Take Screenshot
Selenium- Handle Web Alerts
Selenium- Multiple Windows Handling
Selenium- Handle iframes
Selenium- Upload a file
Selenium- Download a file
Selenium- Actions Class Utilities
Selenium- Mouse Actions
Selenium- Keyboards Events
Selenium- Handle mouse hover Actions
Selenium- Drag and Drop
Selenium- Scroll a WebPage
Selenium- Context Click / Right Click
Selenium- Double Click
Selenium- Desired Capabilities
Selenium- Assertions
Selenium- Exceptions and Exception Handling
Selenium- Difference b/w driver.close() & driver.quit()
Selenium- difference b/w driver.get() & driver.navigate()
Selenium- JavascriptExecutor
Selenium- Read excel file using Fillo API
Selenium- Database Testing using Selenium
Selenium- Read & write excel file using Apache POI
Selenium- Read and Write csv file in Selenium
Selenium- Dynamic Web Table Handling
Selenium- Maven Integration with Selenium
Selenium- Set up Logging using Log4j
Selenium-Implement Extent Report
Selenium WebDriver Waits : Implicit , Explicit and Fluent Wait
This article will present you with a complete idea about Wait commands used in selenium and different waits used by webDriver for executing our test scripts smoothly.
We will learn below topics in this tutorials
Wait commands in Selenium :
Waits are required in Selenium to ensure that the flow of the test cases should be in synchronisation with the application under test.
When test cases are run, the application may not always respond with the same .
We can handle these anticipated timing problems by synchronising our test to ensure that Selenium WebDriver waits until the application is ready before performing a certain action.
When a page is loaded by the browser ,the elements which we want to interact with may load at different time intervals due to usage Ajax and Javascript in the application so we have to provide some waits for performing an action on a particular element.
Selenium webDriver provides three types of waits :
-
- Implicit Wait
- Explicit Wait
- Fluent Wait
Implicit Wait in Selenium WebDriver
We are telling WebDriver to wait for a specific amount of time before it can expect the element to be visible after loading.
so after waiting for a specific time , it will try finding the element. If still element is not found, NoSuchElementFound Exception will be thrown.
Syntax –
driver.manage().timeouts().implicitlyWait(time, TimeUnit.SECONDS);
there are two parameters used by this
time – It is an integer value Second parameter is the time measurement in terms of SECONDS, MINUTES, MILLISECOND, MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc. Example-
WebDriver driver= new FirefoxDriver(); driver.get("https://tutorialshut.com"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Explicit Wait in Selenium WebDriver
-
- Explicit wait is used to tell the Web Driver to wait for certain conditions (Expected Conditions) before proceeding with executing the code.
- Explicit wait is applicable to only a certain element which is specific to a certain condition.
- The Explicit wait is one of the dynamic Selenium waits which waits dynamically for specific conditions.
- The Explicit wait can be implemented by WebDriverWait class.
Syntax –
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(“ID”)));
We will use the wait reference variable for using predefined methods of the ExpectedCondition Class in above command.
Below are a few Expected Conditions which can be used in Explicit Wait.
-
- elementToBeClickable()
- elementToBeSelected()
- alertIsPresent()
- elementSelectionStateToBe()
- invisibilityOfTheElementLocated()
- invisibilityOfElementWithText()
- visibilityOfAllElements()
- visibilityOfAllElementsLocatedBy()
- visibilityOfElementLocated()
- presenceOfAllElementsLocatedBy()
- presenceOfElementLocated()
- textToBePresentInElement()
- textToBePresentInElementLocated()
- textToBePresentInElementValue()
- visibilityOf()
In our selenium scripts we are also using Thread.Sleep() ,It is a static wait and it is not recommended to use.
Fluent Wait in Selenium WebDriver
We are telling WebDriver to poll the DOM every ‘n’ second to check if the element is visible on the page. For Explicit wait, Polling frequency is by default 500 milliseconds. and in fluent wait polling frequency can be changed based on your needs, During polling, in case you do not find an element, you can ignore any exception for example ‘NoSuchElement’ exception etc. Syntax:
FluentWait wait = new FluentWait(driver) .withTimeout(TotalTime, TimeUnit.SECONDS) .pollingEvery(pollingTime, TimeUnit.SECONDS) .ignoring(NoSuchElementException.class);
Example:
FluentWait wait = new FluentWait(driver) .withTimeout(15, TimeUnit.SECONDS) //total amount of time to wait for .pollingEvery(3, TimeUnit.SECONDS) //polling frequency .ignoring(NoSuchElementException.class);
In above command
- Total timeout is defined as 15 Seconds and a polling time of 2 secs.
- so WebDriver will not wait for 15 sec similar to implicit wait .The fluent wait will keep on finding the element every 2 secs.
Practice Exercise
- Launch a Browser
- Open URL “https://tutorialshut.com/demo-website-for-selenium-automation-practice/”
- Click on the Button “Generate Alert Box”
- Use Explicit wait for Alert to be appeared let’s say 2 sec)
- Accept the Alert
package com.test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; public class WaitCommands { public static WebDriver driver; public static void main(String[] args) { // Create a new instance of the Firefox driver driver = new FirefoxDriver(); // define Implicit wait which will be applicable for all elements before throwing an exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Launch the URL driver.get("https://tutorialshut.com/demo-website-for-selenium-automation-practice/"); // Click on the Button "Generate Alert Box" Button driver.findElement(By.xpath("//button[text()='Generate Alert Box']")).click(); // Create new WebDriver wait WebDriverWait wait = new WebDriverWait(driver, 10); // Wait for Alert to be present Alert alertbox1 = wait.until(ExpectedConditions.alertIsPresent()); // Accept the Alert alertbox1.accept(); // Close the main window driver.close(); } }
Selenium WebDriver Tutorials
Benefits of Selenium
Four components of Selenium
Difference b/w Selenium IDE, RC & WebDriver
Selenium WebDriver Architecture
Background when user execute selenium code
Download and Install Java
Download and Install Eclipse
Download Selenium WebDriver
Selenium WebDriver Locators
Selenium - Launch Browser
Selenium WebDriver Waits
Selenium- Implicit wait
Selenium- Explicit wait
Selenium- Fluent wait
Selenium- Commonly used commands
Selenium- findElement & findElements
Selenium- Selenium-Handling check Box
Selenium- Handling Radio button
Selenium- Handling drop down
Selenium- Take Screenshot
Selenium- Handle Web Alerts
Selenium- Multiple Windows Handling
Selenium- Handle iframes
Selenium- Upload a file
Selenium- Download a file
Selenium- Actions Class Utilities
Selenium- Mouse Actions
Selenium- Keyboards Events
Selenium- Handle mouse hover Actions
Selenium- Drag and Drop
Selenium- Scroll a WebPage
Selenium- Context Click / Right Click
Selenium- Double Click
Selenium- Desired Capabilities
Selenium- Assertions
Selenium- Exceptions and Exception Handling
Selenium- Difference b/w driver.close() & driver.quit()
Selenium- difference b/w driver.get() & driver.navigate()
Selenium- JavascriptExecutor
Selenium- Read excel file using Fillo API
Selenium- Database Testing using Selenium
Selenium- Read & write excel file using Apache POI
Selenium- Read and Write csv file in Selenium
Selenium- Dynamic Web Table Handling
Selenium- Maven Integration with Selenium
Selenium- Set up Logging using Log4j
Selenium-Implement Extent Report