Selenium can be one of the most powerful tools in a QA Engineer’s arsenal. Unfortunately, it can be time consuming to write and a nightmare to maintain. That’s why I’m here to give you some quick tips to help your testing run as smooth as possible with Selenium and Python.
What Are Some Common Problems?
There can be a wide variety of problems when trying to write automated tests. These can be unique or widespread dependent on what you are trying to achieve. Here are some of the most common ones that most, if not all, developers and QA professionals will cross paths with in their lifetime:
- Dynamic Content– Testing a website or a web application that has static content poses a minimal number of challenges in Selenium automation. However, in this day and age, most websites contain content that may vary from one visitor to another. That implies that the content is dynamic in nature (AJAX based apps).
- Pop-up Windows– Pop-up windows can be annoying for users. For Selenium testing, they can be major test killers.
- Reporting– An important aspect of any test automation environment is getting a detailed and easily understandable report for each test execution. When you use Selenium WebDriver, there are several possibilities to achieve a reporting mechanism. All of which, however, must be implemented in code using third-party integrations.
A better approach to handle this challenge with Selenium WebDriver dynamic content would be to use Implicit Wait or Explicit Wait.
An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme use case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only if required. WebDriverWait in combination with ExpectedCondition is one way that this can be accomplished.
Some of the common Expected Conditions are below:
An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available.
Handling Pop-up Windows
Humans can handle pop-up windows easily by identifing them and then reacting with them as needed. The task itself is very easy for us; but for our automated tests, it can been a real nuisance. Luckily Selenium has built in support for handling browser pop-ups.
switch_to_alert() allows you to change the focus to the newly opened browser window. You can then interact with it as you see fit, fill out information, accept a term, or close the window.
Typically, the main reason for creating these automated tests would be to gather reports about your product, locate bugs, and improve the overall performance. But Selenium does not have a detailed reporting tool included! Luckily for us, Python can provide us with a variety of testing modules that can be used in tandem with Selenium.
- unittest – A built-in module that is included with every package of Python. Read about unittest
- Pytest – A more powerful module for those looking for more options and abilities. Read about Pytest
There are many other testing modules/frameworks that you could use to validate your tests, each with their own strengths and weaknesses. Here is a full list of Python testing tools that you may want to look through if the above options don’t satisfy your needs.
While Selenium testing can be difficult at times and present challenges that you may have never thought of, I hope the tips above will help make it a little easier. The possibilities with Selenium are nearly endless and the better you become, the more extensive your tests can become!
Looking to get started with Selenium and Python? Check out our help documentation!