Basic Commands in Selenium WebDriver
Jash Unadkat, Technical Content Writer at BrowserStack - December 23, 2019
Selenium is indisputably one of the most preferred tools when it comes to test automation for any software. This is because Selenium WebDriver is compatible with leading programming languages like Python, Java, Ruby to facilitate automation of popular browsers like Chrome, Firefox, Edge, etc. This means that QAs can quickly write Selenium test cases in their preferred language.
Besides being flexible in terms of programming language, Selenium framework also has firm community support from contributors across the world.
Being proficient at Selenium is a necessity for any QA who intends to run frequent and extensive automated tests. For this purpose, it is essential to master specific basics Selenium commands and methods. These commands are simple functions or methods defined in several object-oriented programming languages.
This article aims at highlighting basic Selenium commands that are used to perform actions on a web browser.
1. Navigate To Commands
To navigate to a particular webpage URL, one can use either of the following commands:
Using get method
driver.get (“https://www.browserstack.com”) ;
Using the navigate method
2. Forward Command
The forward command navigates the browser forward by one page recorded in the browsing history.
3. Back Command
The back command instructs the browser to redirect to the immediate previous webpage.
4. The Refresh Command
The Refresh command instructs the browser to reload or refresh the current web page.
Selenium Commands to search for specific Web Elements
For QAs to automate tests for a specific web page, it’s vital for scripts to identify the right elements on a web page to interact with. To identify specific elements on a web page, QAs can use the findElement() command. It can be implemented with the following syntax:
WebDriver driver = driver.findElement (By.id(“text-box”));
The above method returns the specific web element that has text-box as a value of the id attribute. Additionally, there are multiple locators in Selenium that can be used to locate specific elements on a web page. Their function is similar to the “id locator” used in the code snippet above. These locators are as follows:
- By.CSS Selectors
- By.Link Text
To understand how these locators can be implemented to detect specific elements on a web page, refer to this detailed guide on locators in Selenium.
Commands to handle frames in Selenium
Frames in HTML can be used to divide a web-page vertically or horizontally. It is mainly used for displaying external content on a target web page, for example, an advertisement for any online programming course on a web page. To interact with any web element present within any frame, one needs to switch to that particular frame. This allows the user to identify elements present on that page and write tests accordingly.
QAs can switch between frames using the Switch.frame() function. The switch function can be implemented using three different locators: By.index, By.id, By WebElement. Refer to the commands below:
Switches to the frame with index number 1
Switches the frame where the value of id attribute is resultframe
By Web Element
WebElement iframeElement = driver.findElement(By.id("resultframe")); driver.switchTo().frame(iframeElement);
The WebElement command above identifies the web element and then passes it through the iframe element object.
To understand how to identify frames on a webpage, refer to this guide on How to handle frames in Selenium.
Basic Get commands used in the browser
Get commands or methods help QAs or developers to fetch specific parameters on the target webpage to be tested. Let’s quickly go through some basic Get commands in Selenium.
getCurrentUrl() – This command returns the URL of the currently active web page in the browser.
getPageSource() – This command helps in getting the entire HTML source code of the open web page.
getTitle() – This command can be used for displaying the title of the current web page.
Wait commands in Selenium
Wait commands in Selenium enable the QAs to pause the execution of test cases for specified length time. These commands help in observing and troubleshooting errors that may arise due to time variations or any lags.
Implicit wait commands: These commands instruct the WebDriver to wait for a specified time before throwing an exception. Refer to the command below.
Explicit wait commands: These commands instruct the WebDriver to wait until a particular condition occurs before executing further scripts.
WebDriverWait wait = new WebDriverWait(driver,30);
To understand the in-depth implementation of how these commands operate in Selenium, refer to this detailed guide on wait commands in Selenium WebDriver.
Mastering Selenium is essential for any QA who wants to automate tests. Given that automated tests are absolutely necessary for thoroughly testing any website, knowing how to navigate and use Selenium is a non-negotiable skill for any tester worth their title. These basic selenium commands are an excellent place to start and provide a foundation for writing advanced tests with greater ease.