{{announcement.body}}
{{announcement.title}}

Selenium Testing for Effective Test Automation

DZone 's Guide to

Selenium Testing for Effective Test Automation

Selenium: testing for effective automation.

· Web Dev Zone ·
Free Resource

chemical-testing-with-pippette

While there is a substantial increment in the mobile app market share, web apps are still prevalent with a significant user base. Enterprises are focusing on the quality at speed when it comes to web apps before deployment. This is where testing has an important role to play. UI testing is still mostly a manual process, unlike some functional testing, which can be automated.

But it is sensible to automate testing which will save time and effort. When it comes to automation, Selenium testing is the first thing that comes to mind as it is the most popular test automation tool in the world. So, let’s learn more about selenium testing.

You may also like: Getting Started With Selenium.

What Is Selenium Testing

Selenium is open source, and it enables users to drive interactions with the page and test apps across various platforms. It allows users to control a browser from their preferred language like Java, JavaScript, C#, PHP, Python, etc. Selenium has many tools and APIs for automating user interactions on web apps on browsers like IE, Chrome, Firefox, Safari, Opera, etc.

Selenium testing suite

Selenium testing suite

Selenium Framework is a code structure that helps to simplify and reuse the code. Without frameworks, we will place the code, as well as data, in the same place which is neither reusable nor readable. Selenium automation frameworks are beneficial for higher portability, increased code re-usage, higher code readability, reduced script maintenance cost, etc.

What Is Selenium Web Driver

Selenium WebDriver accepts commands via the client API and sends them to browsers. Selenium WebDriver is a browser-specific driver that helps in accessing and launching different browsers like Chrome, Firefox, IE, etc. The WebDriver provides an interface to create and run automation scripts, and every browser has different drivers to run the tests. The different drivers are IE Driver, Firefox Driver, Safari Driver, Chrome Driver, etc.

Selenium WebDriver was introduced to negate limitations of Selenium RC, which offers enhanced support for web pages where the elements on a page change without reloading. Many browsers support selenium WebDriver and it uses each browser’s native support for automation testing. The features are supported and direct calls are made depending on the browser that is being used.

Selenium WebDriver architecture

Selenium WebDriver architecture

WebDriver Project depends on the programming language as well as the development environment. Selenium supports multiple languages and projects can be set up using many programming languages. Selenium provides JavaScript bindings on npm. Java projects can be set up using Maven, and it will also download Java bindings and dependencies to create the project.


Run pip install selenium from the command line to add Selenium in a Python environment. C# bindings are there as part of a set of signed dlls and dependency dlls. You can get PHP bindings through third parties, including Chibimagic and Facebook. To add Selenium to your Ruby environment, run gem install selenium web driver from the command line.

Once you set up the environment, the Selenium WebDriver operates like a normal library. The Selenium WebDriver can self sustain, as there is no need to run any installers or process before using it.

Writing a Script for Selenium Automation

Let’s see how to launch a browser from the script. Make sure you have downloaded the Selenium jar files and the third-party drivers. To launch a browser, the first thing you need to do is to create an object for WebDriver. Then, use a get method to launch a new browser session and direct it to the URL specified in the parameters.

Now, you need the system pointing to the location of the downloaded exe file of your browser. You can do that by using  System.setProperty. Then, provide the browser driver in the key and the path to the location of the browser exe file in the value. Here, System is a built-in class and setProperty is a method to access the browser driver.

Image title

Once the path is set, you need to create an object for the WebDriver class. Hover the mouse on the highlighted value and then import it from the list, as we have already added the jar files in the project.

There are two more methods, driver.close(), which can be used to close the active browser window and driver.quit() is used to close the browser as well as all the associated window.

To get the ID of any element in a web page, like a button, image, logo, textbox etc., just right-click and select inspect. You will get the source code, and from there, you can copy the Id to be used in the script. You can also use any other element locator for the script.

Inspecting element and getting Id

Inspecting element and getting Id

Now, get the driver instance and select the findElement method from the WebDriver class. Then, select the Id locator and get the value that we copied from the source code. Use sendKeys to pass some value to a particular web element.

How to Use Selenium IDE for Test Automation

Running test cases in Selenium IDE is easy. It is a Firefox plug-in that helps in recording the test and converting into code. There are multiple options for running a test case including a Run button to run the displayed test case.

The Run All button is used to run the test cases in the test suite that are currently loaded. The Pause button allows pausing and restarting of a running test case. There is a Speed button to maintain the speed of the test case runs. With the Record button, you can record the actions taken in the web browser.

Recording actions taken in browser

Recording actions taken in browser

You can also use a base URL in selenium IDE to run test cases in multiple domains by mentioning a relative URL as the argument to open the test case.

The table displays the command and its parameters in a readable format. Whereas the command target and value fields display the currently selected command along with its parameters. The log will show you any kind of error messages and information messages and in the reference, you will find the documentation of the selenium command currently used.

You can use a simple HTML file and define your list of tests to define a test suite. Each row in the file should define the path to the tests you want to add in the suite. You’ll find many ways to put these tools to use to improve the development and testing processes.

To Sum Up

Enterprises are always looking to optimize the testing process and automation is one way to do it. Although it is important to use an effective tool for automation and Selenium automation testing is the best choice as it is open source and easy to use. Apart from Selenium testing, you can also opt for advanced tools like pCloudy, which has better features to make automation testing smooth as butter.


Further Reading

Topics:
mobile app testing ,selenium automation ,testing ,cross-browser ,chrome ,firefox ,webdriver

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}