Automating your script with Python in just 20 mins

Paul Zhao
Paul Zhao Projects
Published in
5 min readMay 15, 2020

Let’s begin our project by understanding the word of automation. Automation, or Labor-saving technology is the technology by which a process or procedure is performed with minimal human assistance.[1] Automation[2] or automatic control is the use of various control systems for operating equipment such as machinery, processes in factories, boilers and heat treating ovens, switching on telephone networks, steering and stabilization of ships, aircraft and other applications and vehicles with minimal or reduced human intervention.

Here we’d like to dive deep in automation in DevOps. Automation is the ultimate need for DevOps practice and ‘Automate everything’ is the key principle of DevOps. In DevOps, automation kick starts from the code generation on Developers machine till the code is pushed to the code and even after that to monitor the application and system in production. For instance, we are tasked to build up 300 servers. In the old-fashioned manner, we may need to manually do so. However, automation provides us with edge to make Iac (Infrastructure as code) available on daily basis. This only gets easier with Python, a human and machine readable, high level programming language.

At the end of the day, the rule of thumb is that we need good, quality automation tests, not just automation tests. We need tests which are reliable, robust, easy to code, debug, scale and can run in parallel on distributed environment.

With all this in mind, we will now get our hands dirty!

Shall we introduce Selenium?

Since, Selenium is an open source testing tool it serves the need for cost-effective automation testing and the community always support the cost effective tool with required features.
It’s capability to operate on almost every OS and multiple language support i.e. Pearl, PHP,Python, Java, .NET(C#) and Ruby provides the flexibility that every user wants.
The language which is mostly used for building the program is independent of the language that is being used by the web application or website. Which implies that you can develop the test script in any of the mentioned languages that the Selenium supports.

There are several ways to quickly start automating a GUI flow using selenium. Let’s do it using most human and machine readable, high level progmrming language — yeap, you got it right — python.

Pre-Req : We need python and selenium binding to be installed in the machine. If you don’t have already , follow the below steps.

  1. Download and install Python :

2. Install selenium libraries for python.

Installation of Selenium libraries for Python can be done using pip:

$ pip install selenium
Collecting selenium
Downloading selenium-3.141.0-py2.py3-none-any.whl (904 kB)
|████████████████████████████████| 904 kB 3.1 MB/s
Requirement already satisfied: urllib3 in /usr/local/lib/python3.7/site-packages (from selenium) (1.24.3)

3. Install geckodriver

Since we will use latest version of selenium(3.0) with latest firefox bindings , download geckodriver :- https://github.com/mozilla/geckodriver/releases

OR

Sounds pretty complicated? Why not do it with Homebrew?

Installing Homebrew

To install Linuxbrew on your Linux distribution, first you need to install following dependencies as shown. (This Linuxbrew installation applies to MacOS as well, you may make adjustment in regards to name)

--------- On Debian/Ubuntu --------- 
$ sudo apt-get install build-essential curl file git
--------- On Fedora 22+ ---------
$ sudo dnf groupinstall 'Development Tools' && sudo dnf install curl file git
--------- On CentOS/RHEL ---------
$ sudo yum groupinstall 'Development Tools' && sudo yum install curl file git

Once the dependencies installed, you can use the following script to install Linuxbrew package in /home/linuxbrew/.linuxbrew (or in your home directory at ~/.linuxbrew) as shown.

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

Next, you need to add the directories /home/linuxbrew/.linuxbrew/bin (or ~/.linuxbrew/bin) and /home/linuxbrew/.linuxbrew/sbin (or ~/.linuxbrew/sbin) to your PATH and to your bash shell initialization script ~/.bashrc as shown.

$ echo 'export PATH="/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin/:$PATH"' >>~/.bashrc
$ echo 'export MANPATH="/home/linuxbrew/.linuxbrew/share/man:$MANPATH"' >>~/.bashrc
$ echo 'export INFOPATH="/home/linuxbrew/.linuxbrew/share/info:$INFOPATH"' >>~/.bashrc

Then source the ~/.bashrc file for the recent changes to take effect.

$ source  ~/.bashrc

Check the version to confirm if it is installed correctly.

$ brew --version
$ Homebrew 2.2.16
$ Homebrew/homebrew-core (git revision a59d5e; last commit 2020-05-13)
$ Homebrew/homebrew-cask (git revision d25f8; last commit 2020-05-13)

Notes: you may have older version of python pre-installed, which you may need to upgrade. Please follow the instruction below. (for macOS)

You installed previously as instructed, but your python version remains the same as shown below.

$ python -V
Python 2.7.16

Install python3.7

$ sudo install python3.7

sudo vim /etc/bashrc and add below command lines

alias python="python3" # to use python3 rather than python2.7
alias idle="idle3" # to use python3 idle rather than 2.7

Verify your python version now

$ python -V
Python 3.7.7

Notes: you may find that python2.7 may still link to pip, in order to link pip to python3.7, you may need to follow the instructions below

$ pip --version
pip 20.1 from /Library/Python/2.7/site-packages/pip-20.1-py2.7.egg/pip (python 2.7)
$ sudo rm -rf 2.7

Then, under the folder where you’d like to save your python file

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python3.7 get-pip.py

Verify pip version

$ pip --version
pip 20.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

up to this point, we should have installed python3.7, pip 20.1

Now let’s install geckodriver

$ brew install geckodriver

To verify geckodriver

$ geckodriver --version
geckodriver 0.26.0

The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.

This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.

We are all set to write our first test!

Let’s do a sample python.org page test.

First make a directory

$ mkdir pythontest.py

Then, Vim pythontest.py

Notes: Keep in mind, for a file to be executed by Python, you must provide with .py file

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class PythonOrgSearch(unittest.TestCase):

def setUp(self):
self.driver = webdriver.Firefox()

def test_search_in_python_org(self):
driver = self.driver
driver.get("http://www.python.org")
self.assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source


def tearDown(self):
self.driver.close()

if __name__ == "__main__":
unittest.main()

The outcome of this test will be

$ python pythontest.py 
.
-------------------------------------------------------------------
Ran 1 test in 7.683s
OK

Voila, c’est fini!

Conclusions:

This is a quick demo of an automation project. However, it did showcase the true power of automation — we are able to do a whole python page search just simply by executing a python file. In good old days, we may need to manually search for items we expect among tons of books in library, which would potentially take few days or even few months. Now automation basically scale up the capacity in our society. As we discussed at the very beginning our project, automation plays a vital role in our DevOps work since a whole bunch of identical tasks could be done with a simple script, which saves tremendous amount of time and efforts.

Apart from this, Python with its simplicity and functionality takes up a big room in the world of IT, especially in DevOps, Machine Learning and more. If you’re interested more about Python and automation, why not check out this course on Udemy, you would gain more of it.

--

--

Paul Zhao
Paul Zhao Projects

Amazon Web Service Certified Solutions Architect Professional & Devops Engineer