A Comprehensive Guide for Automation Testers
Selenium WebDriver is a widely used open source tool for website automation. Many automation testers, including myself, prefer using WebDriver in combination with Java.
Recommended IPTV Service Providers
- IPTVGREAT – Rating 4.8/5 ( 600+ Reviews )
- IPTVRESALE – Rating 5/5 ( 200+ Reviews )
- IPTVGANG – Rating 4.7/5 ( 1200+ Reviews )
- IPTVUNLOCK – Rating 5/5 ( 65 Reviews )
- IPTVFOLLOW -Rating 5/5 ( 48 Reviews )
- IPTVTOPS – Rating 5/5 ( 43 Reviews )
In this tutorial, I will discuss 25 frequently used Selenium WebDriver commands along with their syntax and examples for easy understanding.
Table of Contents:
- Types Of Commands in WebDriver
- Top 7 Selenium Commands with Details
- List of 25 More Popular WebDriver Commands & Examples
- #1) get()
- #2) getCurrentUrl()
- #3) findElement(By, by) and click()
- #4) isEnabled()
- #5) findElement(By, by) with sendKeys()
- #6) findElement(By, by) with getText()
- #7) Submit()
- #8) findElements(By, by)
- #9) findElements(By, by) with size()
- #10) pageLoadTimeout(time,unit)
- #11) implicitlyWait()
- #12) until() and visibilityOfElementLocated()
- #13) until() and alertIsPresent()
- #14) getTitle()
- #15) Select
- #16) navigate()
- #17) getScreenshotAs()
- #18) moveToElement()
- #19) dragAndDrop()
- #20) switchTo() and accept(), dismiss() and sendKeys()
- #21) getWindowHandle() and getWindowHandles()
- #22) getConnection()
- #23) POI
- #24) assertEquals(), assertNotEquals(), assertTrue() and assertFalse()
- #25) close() and quit()
- Conclusion
Types Of Commands in WebDriver
In the previous Selenium tutorial, we discussed different types of alerts encountered while testing web-based applications and how to handle them effectively. We covered both web-based alerts and window-based alerts in detail. We also introduced the Robot Class, a Java-based utility for handling Windows-based pop-ups.
In this tutorial, we will focus on various commonly and frequently used Selenium WebDriver commands. We will discuss each command in detail, including its syntax and examples, to help you understand and use them effectively in your automation projects.
Every automation project in Java starts with creating a reference to the web browser we want to use, as shown in the syntax below:
WebDriver driver = new ChromeDriver();
WebDriver provides several methods that can be accessed using the instance variable driver. These methods are used to perform various actions and operations on web elements.
In simple terms, we can classify WebDriver commands into the following categories:
- Browser commands
- Get commands
- Navigation commands
- WebElement commands
- Action commands
- Result commands
In the context of manual testing, result commands are used to compare the expected and actual results and determine whether a test passes or fails. The remaining commands are used to define the steps of a test case.
Top 7 Selenium Commands with Details
Here, we will discuss the top 7 Selenium WebDriver commands in more detail:
- #1) get() Methods
- #2) Locating links by linkText() and partialLinkText()
- #3) Selecting multiple items in a dropdown
- #4) Submitting a form
- #5) Handling iframes
- #6) close() and quit() methods
- #7) Exception Handling
#1) get() Methods
The get()
method is used to launch a new browser and open the specified URL in the browser instance. It takes a single parameter, which is usually the URL of the application under test. This method is similar to the open command in Selenium IDE. For example:
driver.get("https://google.com");
Note: The getClass()
, getCurrentUrl()
, getPageSource()
, getTitle()
, getText()
, getAttribute()
, getWindowHandle()
, and getWindowHandles()
methods are also discussed in this section.
#2) Locating links by linkText() and partialLinkText()
The findElement(By, by)
method is used to locate links on a webpage. The linkText()
and partialLinkText()
methods are used to search for elements that contain the specified link text or a partial link text. For example:
driver.findElement(By.linkText("Google")).click(); driver.findElement(By.partialLinkText("abodeQA")).click();
Note: The same commands can also be used with the findElements(By, by)
method to get a list of multiple elements matching the criteria.
#3) Selecting multiple items in a drop dropdown
The Select
class is used to handle dropdowns in Selenium. There are two types of dropdowns:
- Single select dropdown: Allows only a single value to be selected at a time.
- Multi-select dropdown: Allows multiple values to be selected at a time.
The following commands demonstrate how to select and deselect items in a dropdown:
Select selectByValue = new Select(driver.findElement(By.id("SelectID_One"))); selectByValue.selectByValue("greenvalue"); selectByValue.selectByVisibleText("Red"); selectByValue.selectByIndex(2); selectByValue.deselectByVisibleText("Red"); selectByValue.deselectByValue("greenvalue"); selectByValue.deselectByIndex(2);
#4) Submitting a form
The submit() method is used to submit a form on a webpage. It is commonly used with the click() method to simulate clicking on a submit button. For example:
driver.findElement(By.id("submit")).submit();
#5) Handling iframes
Sometimes web applications contain multiple frames or iframes within a window. In such cases, the switchTo() method is used to switch between frames and perform actions within them. For example:
driver.switchTo().frame("frameName"); driver.switchTo().defaultContent();
#6) close() and quit() methods
The close()
method is used to close the current window that is being accessed by the WebDriver instance. The quit()
method, on the other hand, is used to close all windows opened by the WebDriver instance.
driver.close(); driver.quit();
#7) Exception Handling
Exception handling is an important aspect of automation testing. It allows us to handle unexpected conditions or errors that may occur during test execution. In Selenium WebDriver, we can use try-catch blocks to handle exceptions. For example:
try { // Perform actions on web elements } catch (NoSuchElementException e) { e.printStackTrace(); }
List of 25 More Popular WebDriver Commands & Examples
Here is a list of 25 more commonly used WebDriver commands along with their brief explanations:
- #1) get(): Launches a new browser and opens the specified URL.
- #2) getCurrentUrl(): Retrieves the current URL of the webpage.
- #3) findElement(By, by) and click(): Locates an element on the webpage and clicks on it.
- #4) isEnabled(): Checks if an element is enabled or disabled.
- #5) findElement(By, by) with sendKeys(): Locates an element on the webpage and types in it.
- #6) findElement(By, by) with getText(): Retrieves the inner text of a web element.
- #7) Submit(): Submits a form on the webpage.
- #8) findElements(By, by): Gets a list of multiple elements that match the criteria.
- #9) findElements(By, by) with size(): Verifies if an element is present on the webpage.
- #10) pageLoadTimeout(time,unit): Sets the time for a page to load.
- #11) implicitlyWait(): Sets a wait time before locating a web element.
- #12) until() and visibilityOfElementLocated(): Waits until an element is visible on the webpage.
- #13) until() and alertIsPresent(): Waits until an alert appears on the webpage.
- #14) getTitle(): Gets the title of the webpage.
- #15) Select: Handles dropdowns and selects items from them.
- #16) navigate(): Navigates between different URLs.
- #17) getScreenshotAs(): Captures a screenshot of the webpage.
- #18) moveToElement(): Simulates a mouse hover effect.
- #19) dragAndDrop(): Drags an element and drops it on another element.
- #20) switchTo() and accept(), dismiss() and sendKeys(): Switches to popup alerts and handles them.
- #21) getWindowHandle() and getWindowHandles(): Handles multiple windows in Selenium WebDriver.
- #22) getConnection(): Starts a database connection.
- #23) POI: Reads from Excel files using the Apache POI library.
- #24) assertEquals(), assertNotEquals(), assertTrue() and assertFalse(): Compares expected and actual results.
- #25) close() and quit(): Closes windows and driver instances.
Conclusion
In this tutorial, we covered a wide range of WebDriver commands that every automation tester should be familiar with. We discussed their syntax and provided examples to help you understand their usage.
These commands will enable you to work effectively with Selenium WebDriver and automate your web testing processes. Make sure to practice them and incorporate them into your automation projects.
If you have any questions or suggestions regarding the commands we discussed or if you feel we missed any important commands, please let us know. Your feedback is valuable to us.
In the next tutorial, we will dive deeper into handling web tables, frames, and dynamic elements, which are essential components of web applications. We will also discuss exception handling in more detail in future tutorials.