Selenium WebDriver- Revisiting Important Features

SeleniumAutomation TestingTesting ToolsProgramming

It’s been more than a decade since Selenium automation tool is here to automate our testing needs. As a free to use tool with dedicated community, it has lot of features to make our automation testing more reliable and comfortable. Seeing the growing popularity of Selenium webdriver, let’s revisit some of the important features of it, which proves why Selenium webdriver still stand out in the crowd.

Key Features – Selenium WebDriver

Compatibility with Many Web Browsers

Selenium WebDriver supports wide range of web browsers available in the market such as Firefox, Chrome, Internet Explorer, Opera, Safari and many more. Unless the other tools, the Selenium WebDriver interacts with web page and its elements like a real user. It uses the browser’s native support to make direct calls without taking help from any intermediate software.

And the best part is we can launch the browser using WebDriver by simple commands. For example to launch Firefox, Chrome and Internet Explorer browser the following commands can be used −

WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
WebDriver driver = new InternetExplorerDriver();

There are other drivers such as AndroidDriver, HtmlUnitDriver and IPhoneDriver that can be used whenever required. For mobile application testing the AndroidDriver and IPhoneDriver are used to test the Android and IOS applications respectively.

Supports Many Programming Languages

Selenium WebDriver provides us the freedom to choose many commonly used programming languages, such as Java, C#, PHP, Ruby, Pearl and python. So, we can choose one of any supported programming languages as per our needs and can create the test scripts accordingly.

It also empowers us to write the scripts using conditional statements, decision making statements, switch case statements, looping and branching statements whenever required, making our test scripts more powerful to handle all conditions.

Simple Commands for Pause the Consecutive Steps

While executing the automation scripts, it is common that many times we get errors just because the 2nd condition starts executing before the execution of 1st condition has been completed. Hence we do not get the expected results.

To deal with those situations Selenium WebDriver has two basic wait functions.

  • Implicit Wait − It is the default wait time we can provide between each successive step for the entire test script. For example if the default time is say 20 seconds then the system will wait for 20 seconds to execute the 1st step before moving to the next step.

A sample command of implicit wait is as follows −

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
  • Explicit Wait − This is used to stop the progress of the execution till the particular condition is met, the system will wait for the explicitly given time period to allow the condition to be executed.

A sample command of explicit wait is as follows −

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable (By.cssSelector(“#submitButton”)));

You can also pause the test execution for a specific time at any point while executing the script by using java.lang.Thread.sleep(milliseconds) method.

For example −

Thread.sleep(20000);
//To pause for 20 seconds

Provides Easy Ways to Webpage Navigation

There are some useful commands available in webdriver for webpage navigation. Let’s discuss those helpful commands.

  • driver.navigate().to () method //To open a specific URL.

For example- driver.navigate().to(“http://www.tutorialspoint.com/ “);

  • driver.navigate().forward() method //To navigate one page forward on the browser’s history
  • driver.navigate().back() method //To move back to the previous page on the browser’s history
  • driver.navigate().refresh() method // To refresh the current page

There is also Get() command to open a new browser window.

For example −

driver.get(“http://www.tutorialspoint.com/ “);

As we have seen above the driver.navigate.to() and driver.get() commands are used for same purpose to open an URL or web page, but the only difference between them is , the get() command waits till the page completely loaded while the navigate() commands doesn’t bother whether the page completely loaded or not.

Easy to Identify and Use Web Elements

Selenium webdriver has different type of locators to distinctly address the web elements within the web page. With the help of those locators we can use the elements accurately in our automation scripts.

The locators are as follows

  • Name
  • ClassName
  • ID
  • TagName
  • LinkText
  • PartialLinkText
  • Xpath
  • CSS Selector
  • DOM

Better Handling of Dynamic Web Elements

One of the common issues we often encounters while automating our applications is handling of the dynamic elements. In case of static elements it is easy to use unique locators such as Name, ID, ClassName etc to identify that elements, but when the elements ID or Xpath is keep on changing each time the page reloads then it is very difficult to handle them.

Let’s discuss some of the possible ways to handle the dynamic elements.

  • Absolute XPath − Very often we use Absolute Xpath to handle the dynamic elements. It is the complete path of the element starting from root node with a single forward slash (/).

  • Contains() − The Contain() function can be used to handle the dynamic elements while it is surrounded by static values. It has ability to find the element with partial text.

  • Starts-with() − It finds and matches the starting text of the attribute supplied, with the attribute of dynamic element, thus it able to find the element when the values change on page reloads.

Selenium 3.0 – Changes and Impact on Selenium WebDriver

After a very long period of waiting, finally the Selenium 3.0 is released to the market. The last version selenium 2.0 was released some 5 years ago, since then there are many changes took place to cater our requirements.

As per the official release notes, Selenium 3.0 has some major changes related to the Selenium RC API as they have removed the original Selenium Core implementation and replacing it with one backed by WebDriver. Also they have updated the Json config file format by adding the command line parameter options; so Selenium Grid users may require to update their settings and configuration.

But for the WebDriver, nothing has changed. You can simply update to Selenium 3.0 without worrying of any code or configuration changes. The features which we are using currently will be there. There are some implementations to the major web browsers like Firefox, Chrome, Internet Explorer, and Safari related to the WebDriver for better testing experience.

raja
Published on 17-Jan-2020 12:47:23
Advertisements