An All-Inclusive Manual for Automation Test Professionals
Selenium WebDriver is an extensively utilized open-source tool for automating websites. Many automation testers, including me, prefer utilizing WebDriver 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 guide, we will delve into 25 commonly employed Selenium WebDriver instructions, showcasing their syntax and examples for clarity.
Content Overview:
- WebDriver Commands Types
- Top 7 Selenium Commands Detailed
- List & Examples of 25 More Common WebDriver Commands
- #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()
- Endnote
WebDriver Commands Types
In our preceding Selenium tutorial, we examined the variety of alerts encountered during web-based application testing and how to effectively handle them. We took a comprehensive look at both web-based and window-based alerts. We also introduced the Robot Class, a utility based in Java for managing Windows-based pop-ups.
In this guide, we will concentrate on common and frequently utilized Selenium WebDriver instructions. Each instruction will be treated in detail, demonstrating its syntax and examples, to help you comprehend and utilize them in your automation projects effectively.
Each automation project in Java begins with creating a browser reference that we aim to use, as demonstrated in the syntax below:
WebDriver driver = new ChromeDriver();
WebDriver offers several methods that can be accessed using the instance variable driver. These methods are utilized to perform various actions and operations on web elements.
Simplistically, we can classify WebDriver instructions into the following categories:
- Commands for browsers
- Get commands
- Commands for navigation
- WebElement commands
- Action commands
- Result commands
In manual testing context, result commands are utilized to compare expected and actual results and verify if a test is pass or fail. The remaining commands are utilized to outline the steps of a test case.
Top 7 Selenium Commands Detailed
Here, we will delve into a deeper discussion of the top 7 Selenium WebDriver commands:
- #1) get() Functions
- #2) Identifying links using linkText() and partialLinkText()
- #3) Selecting multiple items in a dropdown menu
- #4) Submitting forms
- #5) Managing iframes
- #6) close() and quit() functions
- #7) Handling Exceptions
#1) get() Functions
The get()
function is utilized in launching a new browser and opening the provided URL in the browser instance. It accepts a single parameter, which is usually the URL of the application being tested. This function mirrors the open command in Selenium IDE. For instance:
driver.get("https://google.com");
Note: The getClass()
, getCurrentUrl()
, getPageSource()
, getTitle()
, getText()
, getAttribute()
, getWindowHandle()
, and getWindowHandles()
functions are also explored in this section.
#2) Identifying links using linkText() and partialLinkText()
The findElement(By, by)
function is utilized to identify links on a webpage. The linkText()
and partialLinkText()
functions are used to browse for elements that contain the provided link text and partial link text, respectively. For instance:
driver.findElement(By.linkText("Google")).click(); driver.findElement(By.partialLinkText("abodeQA")).click();
Note: Similar commands can also be used in conjunction with the findElements(By, by)
function to obtain a list of multiple elements that meet the criteria.
#3) Selecting multiple items in a dropdown menu
The Select
class is used in managing dropdowns in Selenium. There are two dropdown types:
- Single select dropdown: Permits only one value to be selected at a time.
- Multi-select dropdown: Allows for the selection of multiple values at a time.
The following commands illustrate how to select and deselect dropdown items:
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 forms
The submit() method is employed in submitting a form on a webpage. It is commonly used alongside the click() method to imitate the click action on a submit button. For instance:
driver.findElement(By.id("submit")).submit();
#5) Managing iframes
Occasionally, web applications encompass multiple frames or iframes within a window. In such scenarios, the switchTo() method is utilized to switch between frames and execute actions within them. For instance:
driver.switchTo().frame("frameName"); driver.switchTo().defaultContent();
#6) close() and quit() functions
The close()
function is employed to shut the current window that is being accessed by the WebDriver instance. Conversely, the quit()
function is utilized to close all windows opened by the WebDriver instance.
driver.close(); driver.quit();
#7) Handling Exceptions
Exception handling is a crucial aspect of automation testing. It enables us to manage unanticipated conditions or errors that may manifest during test execution. In Selenium WebDriver, we can utilize try-catch blocks to manage exceptions. For instance:
try { // Execute actions on web elements } catch (NoSuchElementException e) { e.printStackTrace(); }
List & Examples of 25 More Common WebDriver Commands
Here is a list of an additional 25 commonly utilized WebDriver commands along with brief explanations:
- #1) get(): Launches a new browser and opens the specified URL.
- #2) getCurrentUrl(): Fetches the current URL of the webpage.
- #3) findElement(By, by) and click(): Locates an element on the webpage and clicks it.
- #4) isEnabled(): Verifies whether an element is enabled or disabled.
- #5) findElement(By, by) with sendKeys(): Locates an element on the webpage and inputs into it.
- #6) findElement(By, by) with getText(): Acquires the inner text of a web element.
- #7) Submit(): Makes a form submission on the webpage.
- #8) findElements(By, by): Obtains a list of various elements that match the criteria.
- #9) findElements(By, by) with size(): Confirms if an element is present on the webpage.
- #10) pageLoadTimeout(time,unit): Sets the time duration for page load.
- #11) implicitlyWait(): Sets a wait time preceding a web element location.
- #12) until() and visibilityOfElementLocated(): Waits until an element is visible on the webpage.
- #13) until() and alertIsPresent(): Waits until an alert is presented on the webpage.
- #14) getTitle(): Retrieves the webpage title.
- #15) Select: Handles dropdowns and selects items within them.
- #16) navigate(): Navigates between different URLs.
- #17) getScreenshotAs(): Captures a snapshot of the webpage.
- #18) moveToElement(): Simulates a mouse-hover effect.
- #19) dragAndDrop(): Moves an element and drops it onto another.
- #20) switchTo() and accept(), dismiss() and sendKeys(): Switches to popup alerts and manages them.
- #21) getWindowHandle() and getWindowHandles(): Handles multiple windows in Selenium WebDriver.
- #22) getConnection(): Initiates a database connection.
- #23) POI: Enables reading from Excel files using the Apache POI library.
- #24) assertEquals(), assertNotEquals(), assertTrue() and assertFalse(): Compares expected and actual outcomes.
- #25) close() and quit(): Closes windows and driver instances.
Endnote
In this guide, we explored an array of WebDriver commands that are vital knowledge for every automation tester. We discussed their syntax and provided examples to clarify their application.
These commands will empower you to operate smoothly with Selenium WebDriver and automate your web testing mechanisms. Ensure you practice them and integrate them into your automation projects.
If you have questions or suggestions regarding the commands we examined or if you feel any crucial command was overlooked, kindly let us know. We value your feedback.
In our next tutorial, we will delve into handling web tables, frames, and dynamic elements, which are quintessential components of web applications. We will also treat exception handling in more nuanced detail in future guides.