Reading Time: 10 mins

Selenium + Python: A How-to

Selenium + Python: A How-to

Automation testing has changed the way in which the software industry gets things done. Selenium automation testing has become one of the most sought-after technologies in automated testing. In this blog, we will walk you through the basics of Selenium, getting started with Selenium, prerequisite to learn Selenium, Python, why Python with Selenium is the best option for automation testing, and delve a bit into the Behave BDD tool as well.

A quick overview on:

  • What is Selenium?
  • What is Python?
  • Why is Selenium with Python an easy choice for most testers?
  • Example of a Python test script
  • How to get started with Selenium?
  • Prerequisites to learn Selenium
  • Python’s Behave framework
  • Advantages of BDD

What is Selenium?

Selenium is an open-source tool that has a suite of browser automation tools that automate web browser interactions. It lets you write the test scripts in programming languages such as Ruby, Java, Python, Perl, PHP, NodeJS, among many others. Selenium-enabled scripts simulate user interaction with web pages using user behavior sequences which represents a test case that includes selecting options, inputting data, specifying wait times, etc.  

The software also provides Selenium IDE, a tool that is used for creating and testing Selenium scripts, which is implemented as Chrome and Firefox browser extensions. It allows developers to record, edit and debug tests.  

What is Python? 

It is a high-level programming language which helps coders write clear and logical code for projects of any size and kind. While it is not specified for any specific problems, it is widely used to build websites, software, automate tasks, and conduct data analysis. This object-oriented language has an easy-to-learn syntax which increases readability and reduces the cost of program maintenance. Thanks to its easy understandability, Python is used to make machine learning models. 

Python is used in multiple 3D animation packages such as Houdini, 3ds Max, Maya, Cinema 4D, Blender, and Lightwave.  

Why is Selenium with Python an easy choice for most testers?  

Selenium supports a number of programming languages for performing test automation, but two of the most popular ones are Java and Python. In this article, we will discuss why programmers should use Selenium with Python for automation testing purposes.  

Python is one of the easiest programming languages to learn, is open-source, and a programming language that has seen widespread adoption. Python is used in the following: web application development, mobile apps, GUI development, data science, network programming, game development and so on.  

Let us look at some of the reasons why Selenium with Python should be your go-to choice for testing. 

  • Since Python is a scripted language, you do not have to worry about running a compiler for converting code from multiple lines of code to something that can be implemented 
  • Python language has a huge community, and it offers solutions for any kind of issues that you face. For those who are only stepping into the world of Python, they will feel ably supported by the community of users 
  • It is easy to code and also easy to read 
  • A large number of programmers are accustomed to using Python for various projects 
  • Python’s API allows you to connect to Selenium through the browser. The binding of both of these help write functional tests by utilizing Selenium WebDriver  
  • Selenium can easily send standard commands of Python to a number of browsers irrespective of variation in the browser’s design 
  • Python is simple and compact when compared to other programming languages 
  • It runs faster and uses indentation to initiate and end blocks 

Apart from all the above reasons, one more reason why Selenium with Python is the best choice is because of the number of tools present to extend it. But make sure that you select Python for the right reasons. When you are about to choose the tool with Selenium, ensure that you decide it based on factors such as the team’s situation, use of language at the organizational level, user-friendliness, support, etc.  

Watch a quick nugget-sized video from our SDET Lead on "Why Python is a great companion to Selenium test automation" below.

Example of a Python test script

Let's look at an example of Py test script for 'log-in' functionality

import time
from datetime import datetime

from src.main.framework.webAction import WebAction as web

class LoginPage(web):

user = "User" + datetime.now().strftime('%Y-%m-%d-%H:%M:%S')

def enter_username_and_password(self, username, pwd):
try:
web.enter_text_on(self, pageName="loginpage", elementName="txtUserName", value=username)
web.enter_text_on(self, pageName="loginpage", elementName="txtPassword", value=pwd)
except BaseException:
web.take_screenshot(self, web.com_direct1)
self.fail("Failed to enter username and password")

How to get started with Selenium?  

If you are new to Selenium, you can take heart for the fact that it supports automation of all major browsers with the help of WebDriver. WebDriver is an API that provides a natural-language interface to control web browser behavior. Each of the browsers is backed by a specific WebDriver implementation called a Driver.  

Selenium provides compatibility with a number of programming languages such as C#, Java, JavaScript, Python, Ruby, PHP, etc. The testers can use the language that they want to design test cases in, a true testament to its flexibility.  

To use Selenium in your automation, you have to install the language bindings libraries that you want. Here is how you can install Selenium. 

  1. Install a Selenium library for the programming language that you choose 
  2. Set up the browser driver to automate your browser 
  3. Set up and configure Selenium Grid to scale your tests 

If you want to start Selenium with a low-code and playback tool, then you should use Selenium IDE.  

Prerequisites to learn Selenium: 

  1. You need to be aware of the software testing basics, automation basics, Core Java and also be familiar with web technologies such as CSS, HTML, XML, XPATH, DOM and JavaScript.  
  2. There should at least be a basic-level understanding of database, programming logic, data structure, and front-end development.  
  3. Some of the software testing basics that you need to be aware of are defects, understanding of test cases, manual testing, regression testing, and automation testing. 
  4. You should also have a deep knowledge of automation testing, understand the automation testing tools, applications that are suitable for automation testing, most popular automation testing frameworks, tests that can be automated, and so on. 
  5. Knowing how to use the right Locators. Locators are building blocks of a Selenium script, and knowing which one to use is pivotal.  

Python’s Behave framework

Behave is a behavior-driven test framework that is similar to other BDD frameworks such as SpecFlow, Cucumber-JVM, and Cucumber. Despite being a BDD test framework, it is significantly different from the other Selenium Python test frameworks.  

Even though Selenium is easy to understand, as the technology becomes more complex, not every stakeholder is able to grasp the logic properly. Those with a non-technical background might find it difficult to get more insights on the use cases and what the customers want. This is where the Python Behave framework, which is written in plain language, helps stakeholders understand the logic in the test scripts.  

Advantages of BDD: 

  • Since the tests are written in Gherkin, which is a simple language, all the members can participate in test creation 
  • BDD tests are more reusable and modular than TDD tests 
  • BDD makes sure that all stakeholders understand the scenarios and help create better products with the help of testing 
  • Since there is a lot more emphasis on business and feature specifications, BDD tests have more value to the customer 

 Click here to read our blog on "Effective shift-left with BDD"

Conclusion: 

Python is the best fit for Selenium testing because of how easy it is to set it up. Thanks to the former’s simple programming syntax, most businesses prefer it. Python also helps to write the Selenium scripts with a lot of ease, unlike other programming languages. For complex functional tests, PyTest, a Python Selenium framework makes it the best choice.   

If you are a business looking for automation testing services, the team at Zuci will be able to help you with it. No matter what kind of testing automation framework that you want to use, we are sure that we can offer the right direction.

Related read/watch:

Keerthika

Keerthi Veerappan

An INFJ personality wielding brevity in speech and writing. Marketer @ Zucisystems.