top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Selenium Commands must be known to create effective test scripts.

Selenium is one of the most preferred tools for test automation of any software. It is widely used for functional testing and cross-browser testing of websites. QAs prefer Selenium WebDriver as it is compatible with leading programming languages like Python, Java, and Ruby, and it facilitates browser automation of popular browsers like Chrome, Firefox, Edge, etc. This means that QAs can quickly write Selenium test cases in their preferred language. Apart from being flexible in terms of programming language, the Selenium framework also has strong community support from contributors worldwide. Being proficient in Selenium is essential for any QA who intends to run frequent and extensive automated tests. To achieve this, it is necessary to master the specific basic Selenium commands and methods. These commands are simple functions or methods defined in several object-oriented programming languages.

Navigational Selenium Commands:

To navigate to a webpage, you can use either of the following commands


Using get method

Using the navigate method

Forward Command: The forward command allows you to navigate forward one page in your browsing history.

driver.navigate().forward();

Back Command: To return to the previous webpage, use the "back" command.

driver.navigate().back();

The Refresh Command: The "Refresh" command is used to reload or update the current web page in the browser.

driver.navigate().refresh();

Selenium Commands to search for specific Web Elements


When automating tests for a particular web page, it is crucial to identify the correct elements on the page to interact with. One way to do this is to use the findElement() command in Selenium. This command can be implemented using the following syntax:

WebDriver driver = driver.findElement (By.id(“text-box”));

The method returns a web element with "text-box" as its id attribute. Selenium has other locators that function similarly. These locators include:


Commands to manage frames in Selenium Webdriver

Frames in HTML are a useful way to divide a web page vertically or horizontally. They allow external content to be displayed on a target web page, such as an advertisement for an online programming course. To interact with any web element within a frame, the user must switch to that frame. This enables the user to identify elements present on that page and write tests accordingly. QAs can switch between frames using the

Switch.frame() function. This function can be implemented using three different locators: `By.index,` `By.id,` and `By WebElement.` Please see the commands below:

  • By Index

driver.switchTo().frame(1);

Switches to the frame with index number 1

  • By Id

driver.switchTo().frame(“resultframe”);

Switches the frame where the value of the id attribute is resultframe

  • By Web Element

WebElement iframeElement = driver.findElement(By.id("resultframe"));
driver.switchTo().frame(iframeElement);

The above command identifies and passes a web element through an iframe element object.


Basic Get commands used in the Browser: Get commands in Selenium can help retrieve specific parameters from the webpage to be tested.


  • getCurrentUrl() – This function retrieves the URL of the currently open webpage in your browser.

  • getPageSource() – This command retrieves the complete HTML source code of the currently open web page.

  • getTitle() – You can use this command to show the title of the webpage that you are currently viewing.

Wait commands in Selenium: Selenium tests require wait commands to troubleshoot delays in loading web elements. Without these commands, testers may encounter the "Element Not Visible Exception" message. Selenium Webdriver wait commands pause test execution to verify web elements' presence, visibility, and clickability.


Users need Selenium Webdriver Wait commands because When a web page loads on a browser, web elements such as buttons, links, and images may load at different times.


Selenium WebDriver provides three commands to implement waits in tests.

  1. Implicit Wait

  2. Explicit Wait

  3. Fluent Wait


1. Implicit Wait: These commands direct the WebDriver to wait for a specified time before throwing an exception.

Implicit Wait Syntax example command below.

driver.manage().timeouts().implicitlyWait(Duration of seconds(5));

2. Explicit wait commands: An explicit wait command in WebDriver waits for a certain condition before executing the code. It's useful for elements that take longer to load. Implicit wait causes unnecessary delays by waiting for every element to load. It is more intelligent and can be applied to specific elements. It's an improvement over implicit wait, allowing the program to pause for dynamically loaded Ajax elements.

See the Explicit Wait Syntax example command below:


WebDriverWait wait = new WebDriverWait(driver,30);

3. Fluent Wait: A Fluent Wait instance allows the user to set a maximum time limit for waiting for a condition to be met and also specifies the frequency with which the condition should be checked. Additionally, the user can customize the wait to ignore certain types of exceptions while waiting, for example, when searching for an element on a webpage.


Fluent Wait Syntax example command below:


Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);

WebElement foo=wait.until(new Function<WebDriver, WebElement>() {
public WebElement applyy(WebDriver driver) {
return driver.findElement(By.id("foo"));
}
});

Now let us see some differences between Implicit and explicit wait commands in Selenium


The main difference between implicit and explicit wait is that an implicit wait is applied to all elements in a test script, while an explicit wait is applied to a particular element only.



Implicit Wait in Selenium

Explicit Wait Selenium

It applies to every element present in a test script.


It only applies to particular elements that are intended by the user.

You do not have to specify "ExpectedConditions" when locating an element.

It is important always to specify the "ExpectedConditions" when locating an element.

It is most effective to use this in a test case where the elements are located within the specified time frame of the implicit wait.

Useful for slow-loading elements and verifying properties like visibility and clickability.


Conclusion: Selenium Wait commands are essential for efficient test automation. They simplify automated selenium testing and detect anomalies on web pages. However, to be 100% accurate, they should only run on real browsers and devices. BrowserStack's real device cloud offers access to over 3000 real browser device combinations for comprehensive testing.


I hope this blog helped you understand the Selenium Commands!!

26 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page