Automated Test Scripts With Selenium WebDriver

Jacob Harvey
5 min readDec 9, 2021
Photo by Christina @ wocintechchat.com on Unsplash

What is Selenium WebDriver?

Selenium WebDriver is a web framework that allows you to execute cross-browser tests. It is used for automating web-based application testing to verify that it performs as expected. It allows you to choose a programming language of your choice from a pretty large list of languages to create test scripts (more on that later). One cool thing about Selenium WebDriver is that it is one of the most popular Open-Source tools for automated web-based application testing and is fairly easy to get started with, which can make it less scary or daunting for beginners to start writing automated tests for their web applications. It is easy to integrate with CI/CD tools like Jenkins, Circle CI, GitLab CI, and is easy to integrate with popular testing frameworks. JavaScript is one of the most preferred languages when it comes to Selenium WebDriver automation testing, and the language that I am most comfortable with so later on in this blog I will show you an example with JavaScript.

Why Selenium WebDriver?

Photo by Markus Winkler on Unsplash

Well first it supports multiple operating systems like Windows, Mac, Linux, Unix, etc. It is compatible with a range of languages including Python, Java, C#, JavaScript, Ruby, PHP, Perl, etc. It also provides support for many different modern browsers like Chrome, Firefox, Opera, Safari and Internet Explorer. Selenium WebDriver completes the execution of test script faster when compared to other tools which is something that I think makes it a favorite among developers when writing test scripts. Selenium WebDriver scripts allow us to use many types of locators, for example if the html element has the id attribute it uses the document.getElementById() javascript command which is the fastest of the selectors when trying to find an element. It is also compatible with the iPhoneDriver, HtmlUnitDriver, and AndroidDriver. Automation testing is an integral part of software development when it comes to web applications, and it is essential to test the user interface to ensure that a flawless experience is delivered to the end-users. This is where Selenium test automation is instrumental to verify the products across different combinations of browsers & operating systems are functioning as expected.

How Selenium WebDriver Works

On a high-level, Selenium WebDriver works in three steps:

  • Test commands are converted into an HTTP request by the JSON wire protocol.
  • Before executing any test cases, every browser has its own driver which initializes the server.
  • The browser then starts receiving the request through its driver.

Example below:

As soon as you complete writing your code, and execute the program the above code will result in the launching of the Chrome browser which will navigate to the example.com/login page of the website. Now lets understand what goes on behind the scenes when you execute the program until the launching of the Chrome Browser. Once the program is executed, every line of code/script will get transformed into a URL. The JSON Wire protocol over HTTP makes this possible. Then this URL is passed to the browser driver (in our example, the chromedriver). At this point, our client library translates the code into JSON format and interacts with the chromedriver. The URL after JSON conversion looks as follows:

https://localhost:3000/{"url":https://example.com/login"}

To receive the HTTP requests, every Browser Driver uses an HTTP server. Once the browser driver receives the URL, it processes the request by passing it to the real browser over HTTP. And then all of your commands in the selenium scripts will be executed. There are two types of requests you might be familiar with GET and POST. If it’s a GET request then it results in a response that will be generated at the browsers end and it will be sent over HTTP to the browser driver and eventually, the browser driver with the help of JSON wire protocol sends it to the UI (IDE of choice) and displays a small pop up window of the browser that you built with. One of automated testing best practices is to quit the browser after your script has finished executing which can be accomplished with the .quit() method on the driver.

Example of an automated test for a login form

The code above does the following:

  • Creates a Selenium WebDriver instance.
  • Navigates to the given web page and locates the relevant web elements of the login form.
  • Then performs actions on the web elements of the login form using the .sendKeys() method and .click().
  • Verify the URL is correct using the assert library to insure the user is on the correct page.
  • Quits the browser after the final test has been executed with the .quit() method.

Conclusion

In conclusion I have only just scratched the surface with automated testing and using Selenium WebDriver but I plan on learning tons more so that I can write more tests and provide quality products. Everyday I learn something new (even if it’s small) and I try to practice the those things to improve my skills and knowledge because knowledge is power. There is still a ton of information about Selenium Webdriver that I could have put into this blog but I did not want this to be multiple hours long so I will stop here and say thank you for reading my blog and I hope you learned a little bit about Selenium WebDriver and automated testing.

--

--

Jacob Harvey

Software Developer building the future of banking.