What is web automation testing?
Traditionally, testing the functionality of any web application was done manually. It required a dedicated team of testers to test different scenarios which took a vast amount of time and effort. In web development, accuracy and time constraints are a major part, which led to the idea of automated test cases that can be used to automatically test out web applications without changing much code. This idea gave birth to automation testing tools like Selenium. Automation testing is nothing but a process of converting manual test cases into test scripts using tools like Selenium.
What is the need for automation testing?
Automation testing has numerous advantages over manual testing. It gives more accuracy and speed of execution. It also helps when test cases are time-consuming, tedious/difficult, or repeatedly executed. You can leave them unattended and it would give you reports back in some time. It also increases test coverage and helps in saving costs. However, it is recommended to test the functionality manual first a couple of times and then only put it in the automation script later.
The tools we are going to use in this blog are Python3, Selenium 4, and PyTest. Besides there are numerous tools available in this market, we chose those tools because not only are they open source and free to use but offer many other advantages.
Selenium is one of the most popular web automation tools available in the market right now. You can write selenium test scripts using multiple programming languages like Java, Python, C#, Perl, Ruby, etc. It supports Operating systems like Windows, Mac, Linux, and browsers like Google Chrome, Mozilla Firefox, Safari, Internet Explorer, etc. Once written, the test scripts can be used across any browser or any platform any number of times. It also works well with Maven, Jenkins, and Docker to achieve continuous testing.
Python is an open-source general-purpose programming language that supports a huge number of libraries and packages. It is not only the most simple and easy-to-use programming language but also very feature-rich and provides large community support. Selenium provides a Python API that connects straight to the browser and since it is developed very close to the English languages, selenium commands are easy to write and execute.
PyTest is considered the best among all Python testing frameworks. It provides a single solution for Unit, Functional, and Acceptance testing. It is popular because of its features. No boilerplate code, parameterized tests, 150+ external plugins to support functional testing, support for TDD and BDD, fixtures, well integration with GUI automation testing with Selenium Webdriver are some of the highlighted features of PyTest.
- You need to have a certain level of knowledge in python.
- You should feel comfortable importing packages, building functions, calling methods on objects, etc.
- You need to have either of the operating systems: Windows, Mac, or Linux.
- You need to have Python3.7 or a greater version installed and added to the PATH environment variable. Also, the python packages PyTest and selenium need to be installed. You can prefer any browser from Google Chrome, Mozilla Firefox, Apple Safari, or Microsoft Edge.
- You will also need to download a suitable webdriver for your browser and browser version. The steps are given in the section below.
First, you need to have a minimum of Python3.7 installed in your system and added to the PATH variable. If you are new to Python, visit official docs.
You can check the Python version on CLI using:
$ python3 –version Python 3.8.10
Once Python is installed, you can use the following command to install Selenium.
$ pip install selenium
If you have already installed Selenium, time to upgrade it using:
$ pip install –upgrade selenium
You will need to install the latest Pytest package:
$ pip install -U pytest
Once the above steps are done, you will need to install webdrivers for browsers that you want to run tests for. Webdrivers are required to interact with specific browsers. Each browser has a particular Selenium webdriver associated with it. So, you need to download the required webdriver into your system. [For Apple Safari, you don’t need to download the webdriver. Instead, there is an inbuilt webdriver called safaridriver you can use to do browser automation]
Links are given below:
|Webdriver||Link to Download|
The following example shows how to perform these steps.
- Launch Google Chrome browser.
- Maximize browser window.
- Open URL: https://www.wikipedia.org
- Check if the title is equal to Wikipedia
- Close browser
Create a test_1.py file in your project directory.
Note that your test file and your test case function name should always start with “test_”. That is how PyTest identifies your test files and test cases. executable_path is the path for your downloaded chrome driver.
Write the following code inside:
Save the file and open the terminal and cd to the current directory where your file is saved. Run the file by typing PyTest in the terminal. It will automatically detect test files and execute them:
It says collected 1 test item and finally, prints the success message “1 passed in 2.17s”. That means it has collected our test case and run it. It also shows how much time it took to execute.
Congratulations! You ran your first test case using Selenium and Pytest. What more can you do?
Well, you can do all types of web GUI automation like clicking a link, button or a checkbox, filling up and submitting a form. From a very simple test case that opens browser to the more complicated one, like End-to-End Automation Testing for a website that tests every single webpage and feature that’s been developed.
You can refer to our Web Automation blog series where we will be explaining different web elements, how to handle them, and so on. We will cover multiple test cases and scenarios required in automation testing.
Thank you! That’s all for now.
Stay tuned for more!