Using Selenium with Python Tutorial (Part 1)

The following  Selenium with Python tutorial will cover all the fundamentals in order to create a proper test automation project.

Python is an object oriented programming language, it runs on Linux, Windows and Mac which turns it into cross platform. In addition, Python is a scripting language. Hence, both compilation and linkage are not required.Python was built with an emphasis on readability and simplicity, it is perfectly scalable and in terms of performance, Python can grant high performance.

The popularity of Python derives from its shallow learning curve, its readability and the enormous supply of libraries. Among others things, Python can handle compressed documents, mathematical functions, regular expressions, data communications, graphic interfaces, working with OS services and more.

*Interestingly enough, Python isn’t named after a snake after all. But rather, a comic British line up!

Without a doubt, Selenium project couldn’t overlook this language when deciding to support different programming languages for Selenium WebDriver. Notwithstanding, it’s not popular as JavaScript or Java under Selenium’s domain, however it’s well known among test automation and API developers.

In this post we’ll discuss test automation using Selenium with Python. The article will consist of 2 parts:

Part 1 (Current): Setting up the development environment

  1. Downloading and installing Python’s environment
  2. Getting to know Python’s Interactive Shell
  3. IDE selection and installation

Part 2: Selenium examples (actions and test cases)

  1. An automatic action in Selenium and Python
  2. An automation test in Selenium and Python

1. Downloading and installing Python’s environment

In order to download Python, enter Python’s official website: https://www.python.org/downloads and download the relevant version. I used 7.2 (It’s a msi document).

 Selenium with Python tutorial: Setup window

 Selenium with Python tutorial : Setup window

 Selenium with Python tutorial: Setup window

Selenium with Python tutorial: Setup window

The next step is to define the Environment Variables in the OS for this language: right-click>My Computer>System Properties>Advanced System Settings and then choose the Environment Variables.

Using Selenium with Python tutorial: System properties

Under System variables, we’ll open a parameter named ‘Path’ by clicking on ‘Edit’ and insert 2 new paths: C:\Python27\Scripts and C:\Python27\Lib\site-packages (Remember to separate both values by  ‘ ; ‘).

Continuing on with 4 clicks on ‘OK’ to reach the main page.

For the sake of making sure the installation and configuration turned out successful, open the Command line and type: ‘python’, the message below will appear:

Selenium with Python tutorial: Command line message

2. Getting to know Python’s Interactive Shell

To get to know Python better, first open CMD and type ‘python’ or open the Interactive Shell of Python explicitly (choosing- ‘Python Command Line’).

Selenium with Python tutorial: Python Interactive Shell

From here on, we are ready to start writing in Python. First function we can start checking is a mathematical function:

Selenium with Python tutorial: Writing code in Python

 

We will print the ‘Hello word’ for the screen:

Selenium with Python tutorial: Writing code in Python

 

We’ll work with variables:

Selenium with Python tutorial: Writing code in Python

 

And use loops- a program that retrieves the values of Fibonacci’s series:

Selenium with Python tutorial: Writing code in Python

 

3. IDE selection and installation

Similar to other languages, Python owns an honorable variety of work environments: some commercial and the remain are open source environments. A few well known environments are: PyCharm by JetBrains, an open source project by NetBeans, the priced addon for Eclipse- LiClipse and the free addon to Eclipse- PyDev.

In this post I’ll demonstrate the work with PyDev for Eclipse IDE which is popular among a substantial number of Java test automation developers. If you have the Aptana plug-in installed, it’s certainly suitable here as well.

Now, all we’ve left with is configuring the Interpreter of Python in Eclipse: Windows> Preferences, PyDev> Interpreters> navigate to Python Interpreter:

Selenium with Python tutorial: Python Interpreter configuration

After clicking on ‘New’, a window will open to configure its path in the location where Python is installed:

Selenium with Python tutorial: Python Interpreter configuration

 

Next, click ‘OK’ and the following will appear:

Selenium with Python tutorial: Python Interpreter configuration

Once again click ‘OK’, to navigate you to the following:

Selenium with Python tutorial: Python Interpreter configuration

Click again on ‘OK’ and wait until the installation process is finished:

Selenium with Python tutorial: Python Interpreter configuration

Stay tuned for the next part of Selenium with Python tutorial, where we’ll open a Python project in Eclipse, install Selenium and begin writing tests in PyDev environment.


Reference