Practical guide to testing applications using Selenium

Practical guide to testing applications using Selenium
13 min read
01 November

Selenium open-source software suite is used for testing applications in automatic mode and administration of Internet resources (locally or directly in the network). Selenium tools automate browser actions - it speeds up the process of software development, shortens the testing and debugging period and improves the quality of applications.

Let's find out what features and advantages Selenium has, how to install and configure the program, how to write tests and how to run them.

What Selenium is

The list of programs from the suite includes: a plug-in for Firefox, Chrome and other browsers, a unified development environment with a record of user actions IDE, an up-to-date library for browser management Selenium WebDriver, a cluster for Grid servers and other tools.

Selenium is a feature-rich framework for testing web applications, which implements an innovative approach to application validation, i.e. the process of evaluating the final product. Selenium Java and Python engages a dynamic validation and testing mechanism when the code is run.

The key advantage of Selenium is that unlike other web service testing tools that stimulate HTTP requests, it acts on the browser algorithm. In automated testing, Selenium actually runs the browser and performs all the steps that a user would do when working with the application.

Another difference between Selenium and other applications is that not only programmers, but also ordinary users can write tests using the tools of this environment. The point is that tests can be written as code and as Fit-tables. A set of tests is launched with the help of the ANT utility or in the environment of continuous integration.

Selenium's main product is the WebDriver library for browser management. It includes a family of drivers designed for the most popular browsers - Firefox, Edge, Chrome, Opera and others. It also includes client libraries for interacting with drivers in different languages. WebDriver works with the most current programming languages - Java, C#, Python, JavaScript and others.

The WebDriver algorithm is simple - it directly sends commands to the browser using its control interface and immediately receives the test results. That is, it uses a method of action that is as close to user behavior as possible.

Being an effective automation tool, Selenium in Java and Python simulates such actions as clicking on various elements, entering text, clicking on links and other types of activity. In essence, using Selenium WebDriver is launching a bot that performs all manual operations with the application in automatic mode.

Experts and users emphasize the following advantages of Selenium:

  • support for all popular programming languages including Java and Python, which makes this tool in demand among a wide range of developers and users;
  • cross-browser testing - tests can be run in all popular browsers, which provides extensive product compatibility;
  • integration with other tools (e.g. Jenkins), which allows you to automate testing as part of the development process.

Since Selenium perfectly models the behavior of real users, it is often used for testing against audience expectations and requirements. It is a rather flexible tool that integrates seamlessly with various testing frameworks and many testing methods. Thus, a developer can expand his toolkit at any time and use the library for more narrow tasks - for example, performance testing.

Testing with Selenium has been used and improved since 2004. For 20 years it has become the most widespread tool for testing web-applications. It is used both by ordinary developers for solving local tasks and by huge corporations, including Google.

Installing and configuring Selenium

The library can be used together with Java, Python and other languages. Let's consider the variant on the example of Python of the latest version.

First of all, the Selenium library is bound to Python. This is done using Selenium Python API, which provides access to the full functionality of WebDriver.

Component installation starts with loading Python on the working machine. First, you need to go to the official programming language resource and download the installer for your operating system (for example, Windows). During the installation process, don't forget to check the boxes to add the necessary components to the system variables. Next, the Selenium library itself is installed.

When interacting with Python, this is done using a command in the console:

pip install selenium

Creating applications requires the use of an integrated development environment, but code can also be written in a standard text editor. You can choose PyCharm environment, which is considered to be the most popular and convenient one

For testing in a browser, apart from Selenium, you will need a web driver. In the case of Chrome, it is ChromeDriver - its version should correspond to the current version of the browser.

The next step is to create the first script using the driver. To do this, open the development environment (in our case PyCharm) and create a new project by specifying its name and clicking Create.

The first test can look as follows:

from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-logging"])
driver = webdriver.Chrome(options=options, executable_path=r'.../chromedriver')
driver.get("https://www.saucedemo.com/")
input_username = driver.find_element_by_id("user-name")
if input_username is None:
   print("Element was found")
else:
   print("Element not found")

Next, install Selenium into the project using the Install package selenium command. If all the steps are correct, the browser will be launched. The message “element found” is the result of successful launch of the Python script.

Next, you can create the algorithms needed to test the elements of the application. This can be a case of user authentication on the pages of an online store - he enters the correct login and password and gets to the main page. Other applications are tested in a similar way.

Basics of working with Selenium

Selenium tools detect and interact with various components of web services.

For example, you can discover an element using identifiers. The following command will help with this:

element = driver.find_element_by_id (“expame_id”)

There are other methods of search organization - by name (by_name), by class (class_name), by tag (tag_name).

This is by no means an exhaustive list of possibilities. There are other ways to search for components on the page, but the above mentioned are used more often than others.

Special commands are used to interact with elements. For example, entering text into the corresponding field and pressing (clicking) on a button. The send_keys and click commands are used for this purpose.

input_field = driver.find_element_by_id("input_example")
input_field.send_keys("Hello, Selenium!")
button = driver.find_element_by_id("submit_button")
button.click()

Waiting and processing of asynchronous items

Testing the search for the required items on the page of an online store and adding the found item to the cart is a common action that is implemented on many web services. As a result of the algorithm, the console displays a message that the item has been placed in the cart.

However, in practice we face a common problem when when selecting an item we immediately click on the cart, but the desired action does not happen. If the Internet is slow or the response from the server is too long, nothing is added to the cart. To solve this problem, there are waiting times.

Selenium WebDriver has two types of expectations - explicit and implicit (explicit and implicitly). In case of explicit expectations, special techniques will help to use the test execution time rationally: a minimum of the waiting time is set, after which the element is returned if it is loaded before the scheduled time.

An example of an explicit expectation is as follows:

element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable(
        (By.XPATH, '//*[@id=\"page_wrapper\"]/footer/ul/li[2]/a')
    )
)

The wait lasts for 10 seconds - if the item becomes available during this time, you can click on it. If it has not loaded, a TimeoutException exception is thrown.

Implicit waits work in a different scenario. They are set once for a driver, but not for each component. The implicit wait is triggered when Selenium tests fail to find the component. After the set time expires, a TimeoutException exception is also created. This method is less flexible and can increase the total time of test runs.

One of the examples of implicit waiting:

driver.implicitly_wait(10)

Thus, explicit expectation in testing is a preferable option.

Parameterization and running tests in different browsers

Selenium Grid cluster is used for remote management and organization of a network of several browsers on different computers. This way developers perform parallel testing, which significantly saves their time.

Grid is a part of Selenium library created specially for parallel launching. The process is implemented by routing browser commands, while the server becomes a hub and is configured for the task of running tests.

Selenium Grid architecture consists of a hub and a node (Hub and Node). Hub is the center where all the tests are loaded. There is only one hub in each Grid, which runs on one device.

Nodes are instances of the Selenium tool that execute the tests loaded into the hub. There can be several nodes in the network, they can be launched on different devices with different browsers.

The hub-node concept implies launching a test on one device and executing it on several nodes.

The schematic algorithm of Selenium Grid configuration is as follows:

  1. Downloading Selenium Grid from the official site of the project.
  2. Placing Selenium Server.jar file in any folder of your hard disk.
  3. Launching the hub on the main device. This is done through the command line.
  4. Launching the hub on slave devices.
  5. Actions in Selenium Grid to configure the network.

A cluster is used when tests need to be launched in different browsers, OS and on several devices. Such testing ensures that the application under test will be compatible with any systems and computers. At the same time, the developer saves time when running the tests.

Practical recommendations and best practices

Several valuable tips from experienced developers will help you use Selenium Grid more efficiently.

Using POM - Page Object Model

Applying POM design pattern helps to organize test code by separating its logic and logic of interaction with the web page. This approach improves readability and maintains code, making it easier to update tests after page changes.

POM is a design pattern in the Selenium environment. It is used to create an object repository where all web elements are stored. Using POM reduces the risk of code duplication and improves test support.

CI/CD integration

Continuous integration and delivery systems ensure that tests are automatically triggered whenever there is a change when. This solution keeps product quality high and allows you to find bugs quickly. Popular CI/CD tools include Jenkins, GitLab CI, and many others.

Code organization

To improve code readability and support, experienced developers recommend dividing tests into several logical blocks and using classes and functions. This way it will be easier to manage tests and reuse code.

Parallel execution

To speed up test execution, parallel testing using Selenium is used. This is done through frameworks like Pytest or TestNG. Parallel execution provides launching several tests simultaneously - this approach significantly reduces the total testing time.

Selenium Server

Selenium Server is intended for remote control of browsers. It supports WebDriver commands acting according to the set algorithm:

  1. On the device where the browser is to be configured, the server is installed and started.
  2. On another device, the user runs the RemoteWebDriver driver program. This program connects to the server on the first device and sends it commands in Java or Python.
  3. The server activates the browser and acts through the driver corresponding to the browser.

Using the IDE plugin

Special plugins have been created that simplify work with Mozilla and Chrome. The plugin records the actions of the user, then plays them back and generates program code in Java, C#, Python and other languages for WebDriver. The libraries then repeat the process.

Plugin is a Selenium development created specifically for productive actions with web page testing script. The main plus of the IDE is the ability to record and save tests to process them in the future.

Selenium IDE is an effective mechanism of testing debugging using test cases. At the same time, test cases can be re-run within other cases.

Results

Web application testing automation with Selenium is a vast topic that cannot be covered in detail within one article. Effective work with libraries and tools is achieved in practice, so it is important to spend more time on the practical application of the techniques mentioned in our article. Working with Selenium saves time, saves developers from routine tasks and speeds up writing clean and correct code.

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Den W. 2K
I'm a passionate tech enthusiast who loves diving into the world of software, programming, and tech reviews.
Comments (2)
  1. Divyesh Sharma

    Selenium automation streamlines web app testing, saving time and reducing human error. By automating repetitive tasks, developers can focus on writing clean code and ensuring software quality.

    1 week ago ·
    1
You must be logged in to comment.

Sign In