Selenium WebDriver

In this tutorial, we will focus on Automation Testing.
This what I will present in this post will be testing in Selenium.

What is Selenium?

Selenium is a portable framework that we can use for testing web applications. When we visit the official Selenium website, we can see that we have 3 types of Selenium: Selenium WebDriver to code browser-based tests across many environments, and is a collection of language-specific bindings to drive a browser. Moreover, we have Selenium IDE, a Chrome and Firefox add-on that will do simple record-and-playback interactions with the browser to create quick bug reproduction scripts and create scripts to aid in automation-aided exploratory testing. Lastly, we have Selenium Grid, which we can use to scale by distributing and running tests on several machines. What’s more, we can also manage multiple environments from a central point. We will focus on Selenium WebDriver in this tutorial.

How does Selenium WebDriver work?

Selenium WebDriver is a browser automation framework that accepts commands and sends them to a browser. Furthermore, it is implemented through a browser-specific driver. It controls the browser by directly communicating with it.

What do we need to know before using Selenium WebDriver?

Selenium WebDriver supports the programming languages such as Java, Python, C#, PHP, Perl, and Ruby. It supports the Operation Systems: Windows, macOS, Linux, Solaris. Selenium supports a wide range of web browsers such as Mozilla Firefox, Internet Explorer, Google Chrome 12.0.712.0 and above, Safari, Opera 11.5 and above, Android, iOS, and HtmlUnit 2.9 and above.

We will use Selenium WebDriver in Java programming language so you will need to know at least the basics of Java.

How to install Selenium?

My environment is:
System: macOS
Java version: java version “19.0.2” 2023-01-17
Java(TM) SE Runtime Environment (build 19.0.2+7-44)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.2+7-44, mixed mode, sharing)

Installing Java

Firstly, we need to check the java version on our machine. We can do this by opening the terminal and type the command: java -version.

The version of installed Java should appear.

We need to enter the command: java -version
We need to enter the command: java -version, and we can see the current Java version in the terminal

If you don’t have Java installed yet, don’t worry, you can download it and install it here.

Installing IntelliJ

Once we have installed Java, we need to install IntelliJ from the official website.

IntelliJ website

When we open the website, we will see a Download button. It is a free trial version for 30 days, but you can also get a community edition if you are just learning 🙂

Once downloaded, open the file and you will see the following options:

IntelliJ installer mac

We just drag and drop to the Applications folder. It will look a bit different on Windows, but we would simply need to follow the steps when installing using Windows system.

After the installation, we can install Maven. We can do it using a command:

brew install maven
brew install maven on terminal

Once installed, we can check the version using the command:

mvn -v

Once Java and Maven are installed, we open IntelliJ; we should see the standard Welcome to IntelliJ screen.

Then we need to click on New project -> name it whatever we like, in that case selenium-testing, select project location, in that case ~/IdeaProjects.

The build system is set to IntelliJ automatically, so we will need to change it to Maven, and we are leaving the JDK and adding sample code as it is, and clicking the Create button.

We should get the following view. As we can see, this is a clear Maven project without Selenium added. We can find the version of Selenium on the Selenium official website.

In that case, the latest version is 4.46.0. Sometimes it is added immediately without any issues; sometimes it is highlighted in red colour. It doesn’t mean it doesn’t work; it means that Maven hasn’t loaded it yet, so we need to close and reopen IntelliJ, and it should fix itself 🙂

After adding Selenium, we need to add JUnit to run our tests. We can find the latest version here.

In that case, it is version 6.1.3 (we can check that when clicking the documentation button on the website). So we add it to our pom.xml file.

Now we are ready to create our first test. So we will start by creating the test package and our first test file. We first open a test folder, and we can see that we have Java, so we are creating a package called tests by right-clicking and selecting Create -> Package. Then we are creating our first Java class called Test.java. The following script opens the website: https://testingtraveler.com.

We have created a main method within the Test class; every script requires a driver object (depending on the website, Selenium requires a different object, and the driver is used for all interactions with the browser).

Once we’ve created the driver object, we’ve simulated the user navigating to a website by using the ‘get()’ method; the desired URL is passed in as a string parameter.

Once the website has been opened, we have closed it by using the driver.quit() method.

In Selenium, we have two options to close the browser:

  • driver.close() – closes the currently active browser, retains the driver object so you can open a new browser, or continue a test in a different browser
  • driver.quit() – closes all open browsers and the driver object will no longer exist

Let’s see it in action 🙂

When we opened and closed the web browser with Selenium already, now we can focus on the interaction with the web browsers.

Locators

When we test the web applications manually, we may easily click an element or enter some text. However, when we perform the tests automatically, our scripts cannot do it. We should tell Selenium how to find an element and what to do with it. We have 3 types of locators in Selenium:

  • HTML Locators
  • CSS Locators
  • XPath Locators

HTML Locators

HTML Locators simulate the interactions with the web applications using HTML tags. We can find an element on the web page by using the findElement() method. As the parameter, we can use the ‘By’ method, inside which we can place: ID, Name, Class name, and Link Text.

When we want to store an element to use it later without repeating the searching it every time, we can create the WebElement object.

CSS Selectors

CSS Selectors are used to bind style properties to the HTML elements. It is more powerful than HTML but also more complicated. We can use the same as in the HTML Locators findElement() method, and as a parameter, we may use the ‘By’ method, inside which we can place: Type, ID, Child elements, First-, Last- and n-th child, and other elements.

XPath Selectors

XPath is a query language for selecting nodes from an XML document. We use XPath to find elements using their type, ID, value, and/or attributes. We can notice the XPath as an absolute path, relative path, element path, attributes, descendants, boolean (AND/OR), or even tree navigation.

Interaction with the elements

We have found the element, so now we may need to interact with the element. In this post, I will present 3 methods to interact with the website.
First, we can click an element by using the click() method.
Second, sendKeys() allows us to enter the value in the fields.
The last one, getText() lets us get the text stored in the element.

Let’s take a look at an example.

We will try to navigate the Testing Traveler blog by using the different types of locators. We will click the Set of skills for Software Tester post element on the homepage, which will redirect us to the post. After that, we will enter the value in the comment field. Then, we will click the Post Comment button. In the end, the web browser will close itself.

Let’s see the following code, which presents the example shown above. We can also notice that some Thread.sleep() has been added. That’s because we wanted to see what the test is doing; without it, when we would run the test, we wouldn’t see anything as the test would complete in less than a second.

We can also see the results of the Selenium script.

Selenium documentation

If you get any errors during running the Selenium script, you can always find some help on the Selenium documentation website. It a really helpful resource, especially when you’re starting with Selenium WebDriver.

I hope you’ve enjoyed the tutorial and found some inspirational information.
The next post will arrive soon 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *