How to Write Automated Test Scripts Using Selenium

According to the MarketsAndMarkets report, the global test automation (AT) market will expand from $12.6 billion in 2019 to $28.8 billion by 2024, growing by 18% annually. Among AT tools, most companies (27.7%) choose Selenium, and about 49% of active users live in the United States. It has gained a lot of popularity among automation testers. In this article, we will analyze the main stages of creating scripts, give recommendations, and tell you about the features of Selenium.

  11 300x153 - How to Write Automated Test Scripts Using Selenium

Preparing for Automated Testing

What are automation scripting and the right tools to implement it? A script is a list of commands in a programming language (C, Java, Python, JavaScript) that can be executed without interaction with the user. It consists of a launch point, variables with corresponding binding values, and source code.

Script automation is the process of using AT to replay scripts that have been written. It runs without code generation or external management.

The correct tool is selected based on the AT requirements for a specific project. They are formed according to general criteria as the required scripting language, support for the framework and external conditions, testing the elements used, and others.

Standards of creating automated test scripts:

  • Detailing. Inaccuracies in the script can change the behavior of the autotest. The validation will throw an error that will lead to useless results. Any ambiguous move is subject to detailing because one action can have several ways of performing.
  • Identity. The code should be written in a consistent style to be understood by another IT professional. It is unacceptable to mix tabs and spaces in indentation. It is necessary to create comments for classes, methods, variables, and constants.
  • Name. The rules for naming test scenarios must be established for all projects in operation. A single approach will help you navigate a large number of scripts and quickly find the one you need. Scenarios can be named according to the following scheme: project name + name of the module or functional area for testing.
  • Supplements. Documents such as screenshots, specifications, or photos can be attached to understand the script better. Relevance. The scripts require periodic updates. It is necessary to warn testers about changes in the user interface or additional tabs.
  • Providence. Scripts must contain preconditions. For example, a specific user account or compatibility with other test cases. In addition, descriptions and the expected results of actions must be specified.
 

Key Features of Selenium

It is one of the free automation scripting tools and the basis for testing web and mobile applications. The latest version of Selenium is 4.0.0. It provides simple navigation tools and a domain-specific programming language for creating test cases.

Among the technical characteristics of the software product are:

  • Support for 5 browsers when installing driver files: Internet Explorer, Opera, Chrome, Safari, and Mozilla Firefox.
  • Export of scripts in different languages: Java, Python, Ruby, C#, etc.
  • Usage with all popular frameworks.
  • Support for multiple platforms like Windows, Linux, and Mac.
  • Storing tests in various formats such as Ruby Script, HTML, and others.

The open-source tool has 4 options like Selenium IDE, Selenium RC, Selenium WebDriver, and Selenium Grid. They can become the basis for writing complex and simple scripts. The main differences between Selenium and other test automation tools are the following features:

  1. establishing titles for each page;
  2. provision of recording and playback of the object for testing;
  3. running several test cases simultaneously;
  4. performing regression testing;
  5. compatibility with all types of web and mobile applications.

The Selenium IDE supports global speed dialing commands, simplifying the final maintenance of tests. When writing, it is possible to insert a pause after some steps. The tool allows one script to run another, saving testers time to write repetitive actions. Editing scripts and running tests simultaneously make Selenium one of the most popular AT tools.

 

How to write automated test scripts: 3 stages with Selenium Webdriver

 
1. Setting configuration elements

Necessary components must be installed before writing the script.

These include:

  • Java (JDK). Download, install on PC, and specify the path to the environment. Verify that everything is correct by writing “java -version” on the command line.
  • IntelliJ IDEA. Download the platform to run and write code. After downloading, configure the program by setting appropriate flags on .java and on acceptance of all terms and conditions. Check if the file appears in the project catalog.
  • WebDriver Language. Install the Selenium Java client driver and extract the content from the downloaded file. You can also choose a different language option.

WebDriver is built on a server/client model. Server (remote) is a browser launched using test code. The client sends an HTTP request. The browser accepts it, performs the operation, and responds with the value “complete”.

photo 2021 11 17 15 48 41 300x135 - How to Write Automated Test Scripts Using Selenium

 
2. Connecting Selenium Webdriver to IntelliJ IDEA
  • Run the program file IntelliJ IDEA (.exe) and create the required number of workspaces.
  • Type a name for the new project in the root Java folder.
  • Add external Selenium .Jar files from the catalog to IntelliJ and click OK.
  • Check the structure of your project in Java Class.
  • Write your first web automation script.

Try to write a script to open an online store website in one of the browsers. For example, for Chrome, you need to download the executable driver file. It can be downloaded from the Selenium web page in the browser section. When writing a script, you should use keywords that will trigger basic “open/close window” commands. The result of the test is determined by comparing what is expected with what was actually performed.

 
3. Selenium Grid Selection

An online tool may be needed to run test scripts. Let’s take a look at BrowserStack. It is a cloud-based web and mobile digital product testing platform that provides website and application validation features. Supported browsers are Google Chrome, Internet Explorer, Opera, and Safari. The platform also provides access to real iOS and Android devices. BrowserStack offers trial accounts with 60 minutes of free browser testing. Premium tariff plans start at $19 per month.

 

photo 2021 11 17 15 48 50 300x135 - How to Write Automated Test Scripts Using Selenium

 

Bonus Option: How to Write Automation Scripts with Selenium IDE

The tool exists as a Google Chrome extension and Firefox add-on. It can be used to run tests on Selenium WebDriver, and it also launches scripts on servers. Use the SIDE Runner, which accepts the Selenium IDE script as a file. Then launch it using the browser drivers.

Creating a test involves only 3 steps:

  1. Install the extension. Click the additional icon on the top toolbar, which takes you to the Selenium IDE window. Fill in the URL of the website you want to check.
  2. Enable the “Record” mode in the menu. Perform actions on the website, which will be automatically turned into commands. For example, go to the “Clothes” section, select a filter by size, etc.
  3. Rewrite Selector. Press the “Select” button to activate the automatic path determination mode. Correct the given variant manually.

Now you can run the script. The Selenium IDE allows you to write test groups and periodically check their validity. It saves time and can reproduce user steps. The tool is suitable for those who want to figure out how to create simple automated test scripts.

 

Basic Recommendations for Successful Start

Carry out AT in the project sequentially, performing the following points:

  • Determine the list of scenarios to be automated.
  • Choose language, tool, and framework.
  • Comply with standards and requirements for creating a script.
  • Combine checks and coverage.
  • Research the user path.

The main task of the test is to detect problems and bugs to provide a high-tech digital product. Using Selenium for test automation scenarios reduces costs for business customers, reduces IT time spent on AT, and improves the accuracy of results.

Related Posts