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 test web applications. When we will 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, which 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 more, we can also manage multiple environments from a central point. We will focus on Selenium WebDriver in this tutorial.
How Selenium WebDriver works?
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:
Java version: java version “15.0.1” 2020-10-20
Java(TM) SE Runtime Environment (build 15.0.1+9-18)
Java HotSpot(TM) 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)
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.
If you don’t have Java installed yet, don’t worry, you can download it and install it here.
Once we have installed Java, we need to install Eclipse from the official website.
When we will open the website, we will see Get Eclipse IDE 2021-03 Download x86_64. Don’t worry, if you’re using macOS or Linux, it will work, just click and download.
Once downloaded, open the file and you will see the following options:
We are choosing the Eclipse IDE for Java Developers. Once we have done that we have to choose the Java 11+ VM (once you have installed Java, it will be chosen automatically) and installation folder, then click Install.
After the installation, we can install the selenium. To install the selenium library we go again to its official website and under the Selenium WebDriver, we are clicking the Download button. There will open a new page.
We are looking for a Java version and clicking Download and also look for Browsers.
Once we have clicked the documentation under Chrome, the new page will open.
We download the latest stable version of ChromeDriver.
When we did it, we can check the ChromeDriver version in the terminal.
Okay, we have all what we need to start writing the first Selenium script.
Now we can run Eclipse to check if everything is working properly.
We need to choose the workspace and click Launch.
Our eclipse should look like in the following screenshot.
We can also create a folder where we will keep our files. I have called the folder: Selenium example.
We will create a new project by clicking: File -> New -> New Java Project
Once Libraries opened, click Add External JARs and choose two files: client-combined-3.141.59-sources.jar and client-combined-3.141.59.jar. After doing that, click Add External JARs one more time, go into the lib folder and add all JAR files. Then click Finish.
First Selenium script
Now we will create our first script. The following script opens the website: https://testingtraveler.com.
We have created a main method, class Test, every script requires a driver object (depending on the website, selenium requires a different object and the driver is used to 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 a 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 driver object will no longer exist
Let’s see it.
When we opened and closed the web browser with Selenium already, now we can focus on the interaction with the web browsers.
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 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 which inside 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 are using 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 which inside we can place: Type, ID, Child elements, First-, Last- and n-th child, and other elements.
XPath is a query language for selecting nodes from an XML document. We use the 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 on example.
We will try to navigate the testing traveler blog by using the different types of locators. We will click the Lessons Learned in Software Testing 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 see the results of the Selenium script.
If you will 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 🙂