At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Automation Testing With Selenium WebDriver

  • By Mercy Diana
  • December 9, 2019
  • 1089 Views

Tired of repeating the tests after each deployment? If you want to make your application testing easier, you’re at the right place.
Selenium is an open source framework for making automation testing simpler for web-based applications. It is basically used to automate each and every task in your browser just as manual execution. Selenium WebDriver is an interface used to send a command to different browsers.
Get started with automation using selenium for web based applications. And also know what are key features that will work for you.

Why selenium web driver is most preferred for web-based applications?

Selenium is simply a suite of testing tools that are widely used by the testing community. It is the most familiar cross-browser testing, it is compatible across browsers and OS. It is built to make the testing task easier and time efficiency. WebDriver is the main interface of Selenium which is used to write the test cases.

How to automate a web-based application using Selenium WebDriver?

The user should know these 7 basic steps of Selenium test scripts, which applies to all test suite related to the web application.
Step 1: WebDriver instance creation
Step 2: Web Page Navigation (Gmail.com)
Step 3: Locating elements on the Web page.
Step 4: WebDriver Action on a web element
Step 5: Wait for the browser response to the action performed
Step 6: Run automated test scripts and recording test results using a test framework
Step 7: Quit the test.
Now let’s see the implementation of these 7 steps explained with examples.

1. WebDriver instance creation

If a user wants to write and run the selenium test, the first and foremost thing is to instantiate the web driver. Users can also create an instance of the WebDriver interface using a constructor for that specific web browser. The constructor names will vary based on the browser the user select.

Example

Here, we are instantiating the Chrome WebDriver, and assigning it to a variable named driver.

import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.chrome.ChromeDriver; 
WebDriver driver = new ChromeDriver ();

2. Web Page Navigation – (Gmail.com)

Once the web driver has been instantiated, then the next step is navigating to the web page which has to be tested. This can be done with the help of get method. The get method is used to navigate to the particular URL of the web page the tester going to automate

Example

driver.get("http://www.gmail.com");

3. Locating elements on the web page

Web elements on the page will be located by the locators to perform actions/interactions with the web page which is being automated. A locator expression will always be a (key: value) pair which contains locator type and a locator value.
If you want to know more about locators in Selenium. Take a look at the list of commonly used locators and their syntax here.

Example

Locating elements for username and password for Gmail application.

Syntax

import org.openqa.selenium.By; 
import org.openqa.selenium.WebElement; 
WebElement userName = driver.findElement(By.id("username")); 
WebElement passWord = driver.findElement(By.name("password"));
WebElement signInBtn = driver.findElement(By.id(“Sign-in”));

4. WebDriver Action on a web element

Once the element has been identified, then the user will be able to perform an action on a web element by interacting with an instance of the WebElement interface.
The basic interaction methods that can be used

  • The sendKeys method – to enter text
  • The clear method – to clear entered text
  • The submit method – to submit a form
  • The click method – to perform a click

Example

Here in this example, we are performing the action of sending input to the user name and password and performing click operation on the sign-in button.

userName.sendKeys("abcxx@gmail.com");
passWord.sendKeys("*******");
signInBtn.click();

5. Wait for the browser response to the action performed

When the user clicks a Submit button, the user has to wait for a second or two to continue with the next action.
To reach the server >For the server to respond
For testing this response, the user needs to build that waiting time into the test. Otherwise, the elements that are expected in the next step will not load into the browser.
Here is the technique for anticipating browser response by waiting.
Implicit Wait – It is a definite/fixed time for the action to be completed. Implicit waits have two major drawbacks to be considered before use.

  • The user can’t predict or assume on the Web browser response times
  • All the interactions will not have the same fixed time slab

Syntax

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Explicit Wait – It is the most preferred wait, as it provides plenty of options to wait until some expected condition is met. For setting the explicit wait, the user should create a webDriverWait class with the maximum time waiting for that expected condition to occur

Syntax

WebDriverWait wait = new WebDriverWait(driver, 10); 
WebElement messageElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("SignIn")));

6. Run automated test scripts and recording test results using a test framework

Running tests and recording test results is the ultimate purpose of the automated test script. The automation test suite will be run in order to ensure that the application is running smoother without any human interaction.

Frameworks

Test Frameworks are used to run the automated test suite and to record test results. There are many test frameworks available which include the frameworks in the XUnitfamily.
Here is the list of frameworks you can use,

  • JUnit for Java – Most preferred
  • NUnit for C#
  • unittest or pyunit for Python – Most preferred
  • RUnit for Ruby

Assertions in JUnit

Assert is a method which is used to determine the status of a test case whether it is passed or failed the expectation, The assert methods are usually provided by the class org.junit.Assert which extends java.lang.Object class

Recording Test Results

Once the testing is completed, the result should be recorded for log purposes.
It can be done in the following ways,

  • Test framework
  • Logging framework for the programming language
  • Both together

Selenium supports capturing screenshots in an application based on failure/blocked conditions to know where exactly the test failure issues arise.

Example

import junit.framework.Assert;
import junit.framework.TestCase;
 WebElement messageElement  = driver.findElement(By.id("loginResponse"));
String message  = messageElement.getText();
String successMsg  = "Welcome to Gmail login. You logged in successfully.";
assertEquals (message, successMsg);

7. Quit the test

Once the actions are performed on the web page, the quit method is used to conclude the test run. It disposes of the resources, which will clear all the dependencies or logs which is not a blocker for later tests to run and also for an application to be stable without getting affected by earlier tests.
The quit method quits the web browser application. It closes all the web pages and quits the server.

Syntax

driver.quit();

Example – Covering all the 7 steps

Below is the full set of basic test case consolidating all the 7 steps discussed above.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.junit.Assert;
public class Example {
    public static void main(String[] args) {       
//Initializing the web driver
        WebDriver driver = new ChromeDriver();        
// Web page navigation – gmail.com
        driver.get("https://www.gmail.com");       
// Action on web elements, entering credentials and submitting the login form 
        WebElement userName = driver.findElement(By.name("username"));
        WebElement passWord = driver.findElement(By.name("password"));
        WebElement signInBtn = driver.findElement(By.id(“Sign -in”));
        userName.sendKeys("abcxx@gmail.com");
        passWord.sendKeys("*******");
        signInBtn.click();      
// Wait for web browser response, with an explicit wait
        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement messageElement = wait.until(
            ExpectedConditions.presenceOfElementLocated(By.id("loginResponse"))
        );        
// test run
        String message = messageElement.getText();
        String successMsg = "Welcome to Gmail login. You logged in successfully.";
        Assert.assertEquals(message, successMsg);        
// End a test
        driver.quit();
    }
}

Advantages of Selenium WebDriver

1. Easy Availability – Open-Source Tool

Selenium is free software and also a portable tool. You can directly download it here.

2. Multiple languages support

As most of the software tools may not be compatible with many system languages. But selenium is quite different, its outstanding support for multiple languages such as Java, Perl, Python, C#, Ruby, Groovy, JavaScript, etc., is an added advantage for the tester/developer to work with various languages.

3. Different Operating Systems Compatibility

Likewise, stated for language support, selenium also provides support for multiple operating systems such as Windows, Mac, Linux, UNIX, etc. And it is also inter-compatible across OS. For example, the tester will be allowed to run the test in Linux based system which was written on Windows or Mac

4. Cross-Browser Compatibility

Just as the multiple language support and multiple OS support, it also provides support across multiple browsers as listed below

  • Internet Explorer
  • Chrome
  • Firefox
  • Opera
  • Safari

It is more useful features if the business is indecisive about which browser the end-user will use. It is always better to test it across browsers to check for compatibility.

5. Support for the framework and its functionality

Selenium can be integrated with different frameworks and different languages. For example, Maven is the most preferred framework to be integrated with Selenium for source code compilation.
TestNG testing framework can also be integrated with Selenium which is used for testing applications and mainly for reporting purposes.
Jenkins can be integrated with selenium for Continuous Integration

6. Community-based updates and upgrades

Selenium has huge community support in turn which helps the testers/developers to get constant updates and upgrades on the same. These upgrades are available readily and it does not require any specific training. This makes Selenium more useful and cost-effective as well.

7. Implementation made easier

User-friendly interface is the most needed and most likable factor when comes to testing. Selenium provides a user-friendly interface that helps the testers to create and execute tests easily and effectively. Since selenium is an Open-Source tool, it helps the testers to script their own extensions that make it easy to develop defined actions/customized actions and even manipulate further to an advanced level.
It allows the tester to run their test suite directly across browsers and can watch it, while it is being executed. In addition to this, the reporting feature of selenium is one of the reasons for choosing it, as it allows to extract the results and take necessary follow-up actions.

8. Reusability, Cost-effective and Time effective

Selenium Test Automation Framework uses scripts that are reusable and can be tested directly across multiple browsers. It is absolutely possible to execute multiple tests with Selenium.

The Challenges and limitations of Selenium

1. Pop-up alerts

Handling pop-up windows might be a challenge since their part of the operating system but not the browser.
It can be handled with the switchTo method which helps in controlling the windows/desktop pop-up in the foreground by keeping the browser running in the background.

2. Dynamic web element handling

When a locator cannot identify a new element present at that time, Selenium WebDriver uses an Explicit Wait for the expected action to be performed. With the Explicit wait, the user can specify an amount of wait time before automating an action. It will provide the page enough time to load and identify the element to interact with or to perform the action.

3. Multi-tab Testing

Multiple tabs in the same browser window are also supported in Selenium WebDriver, but if the user doesn’t know the correct commands, it will lead to a big hazard.
For Example, if a user wants to open a new tab to perform some action and at the same time, the user should not leave the original tab. But, it is possible with the Selenium WebDriver.
The best way provided by Selenium WebDriver for this is to store the previous window handle in a variable. And continue to the new window to store that in a second variable. The user can use the switchTo method to go between both windows as required.

4. Limited Reporting

5. Page Load handling

Limitations

  • No support to automate desktop applications
  • No support to automate mobile applications
  • Test flakiness
  • Captcha can’t be handled

Final Say

If there are a lot more automation cases that need to be run in a short time, selenium paves the way to test 24*7 from a remotely held device as well (Without any manual intervention). As the user has plenty of options of explicit waits rather than sleep to handle the web elements, the automation run will be smoother and faster that there is no/very minimal manual interference, so the possibility of errors diminishes.
In consideration of all those limitations/Challenges and in addition to all the benefits, there are few more added advantages of the Selenium WebDriver. Using Selenium WebDriver for automating the web applications is easy, precise, cost-effective and time-efficient. And also comforts the developer with the language support they already have.
It makes the test scripts reusable. So, you won’t need new scripts every time even with changes in the version of the OS on the device and the tests can recur without any errors. Most importantly, it enables testing in volumes. For instance, it allows you to run tests on thousands of mobile devices or across systems, which is impossible with Manual Testing. As all the business intends cost-benefit and time efficiency, it ensures higher ROI on the initially huge investments done.
Like this post? Interested in staying updated on Development technologies and testing? Subscribe for the exclusive weekly newsletter now!

Turn your vision to magnificent reality With
Our Web and Mobile Solutions

Mercy Diana

She is an aspiring IT enthusiast with 8 years of experience in manual and automation testing. Presently, she is into automating the test cases and offers proficient Quality checks for web-based and Angular applications at Agira. She is a dancer who also loves to play badminton in her free hours.