In this article, we will see how we can write automated tests with Python from scratch. Here is the list of our requirements to do that so:
- Python [obviously :)]
- Selenium WebDriver
- An IDE (Not a must but nice to have)
If you have all or some of them already you can move on to the next topic.
Though some IDEs might come with Python included, I find it useful to learn to install it separately, since you might some another time like to use it for another purpose.
To install Python on Windows, go to https://www.python.org/downloads/ and click on the latest version available.
You also can install Python2.x and use it in your test automation framework but I will be using the latest version throughout the tutorial.
After clicking on download save the file and run it once it is installed (preferably as administrator).
Check the ‘Add Python to Path’ option so you do not have to add it manually and then click ‘Install Now’ and python will be installed automatically. Click ‘Close’ to complete installation afterward.
If everything is ok until now, we can start Python on the command line.
Open up a new command line by typing ‘cmd’ (or ‘command prompt’) after clicking ‘windows’ button.
Type ‘python’, and Python should run.
You can type ‘exit()’ to close Python or close the command prompt directly.
Now we can install Selenium Webdriver.
Installing Selenium Webdriver
Though there are various ways for downloading Selenium Webdriver, in this tutorial I will download it via ‘pip’.
Pip is a package management system used to install and manage software packages written in Python.  We have installed pip while we were installing Python.
Open a command prompt again and type ‘pip install selenium’ and click enter.
That’s it! Pretty easy, isn’t it?
Let’s install an IDE to write our first automated test!
Although there are numerous IDEs for Python, I opted PyCharm.
PyCharm is an IDE developed by JetBrains and is one of the best IDEs for Python.
For downloading it go to https://www.jetbrains.com/pycharm/ and click ‘Download Now’.
Click on ‘Download’ under the ‘Community’ title since the community is the free version of this IDE.
Save the file and then run the installer. Click ‘Next’ until you end up with below screen:
If you want to add PyCharm to your desktop check either 32-bit or 64-bit version and you can also create associations with .py so all Python files will be opened with PyCharm. Once the installation is checked ‘Run PyCharm Community Edition’ and we can start writing our first test.
After PyCharm is opened, click on ‘Create New Project’.
Set the name of your project and click ‘Create’.
Right-click on project folder, then hover on new and click on ‘Python file’ to add new Python file to the project.
Name it as you wish and then click ‘OK’.
First, since we are going to write a test we need to import built-in unittest library by typing:
Then we need to import Selenium Webdriver:
“from selenium import webdriver”
Now we are ready to write our class. Our class needs to inherit the TestCase class.
Now we can write our first test method inside the class. Please note that there are no annotations for tests in Python like in Junit or Nunit. The name of your methods have to start with test so the interpreter will understand it as a test.
Run your test by selecting the ‘Run’ tab on the top menu and ‘Run Unittests’ option.
You just run your first test! You will see the results below.
We expand our test class by adding setup and teardown methods as below:
Congratulations! You just created your first test!