Web-Testing Automation in Python
A description of different testing methods for Python.
Join the DZone community and get the full member experience.
Join For FreePython is becoming the most widely used programming language.
Different Types of Testing
- Unit Test — you can picture this being at the bottom of the pyramid. It is mostly used to pinpoint bugs in your code. It cannot be used for integration testing, unit tests should essentially just give the function that’s tested some inputs, and then check what the function outputs are correct.
- UI Testing — the user interface testing, on the other hand, it can be pictured at the top of the pyramid. The purpose is to test UI elements and features, to replicate a user experience. Usually, this is the most time consuming and expensive type of testing on your web application.
- Service/API Layer Testing — the aim is to split UI testing from Unit testing and test functionality in terms of services.
Efficient UI testing
I'm going to go ahead and assume that you know what Selenium is and that you are familiar with "Unit Test" or "Pytest" in python. If you are not, I would suggest that you read an article about either, or try them out yourself!
Selenium is Slow
If you conduct a full UI testing with the basic actions and functionality directly given by selenium, you will be writing lots and lots of code.
Sure it can be useful and worth it if you do end-to-end testing because you will be writing the code once and reusing it as much as you need. That doesn't mean that it is an efficient approach.
Imagine you want to perform a drop-down selection from a menu on a web application to make sure the options that you intend to select are included. Here are the steps:
- You first need to find the element, in selenium, this done either by finding its xpath or by using elements attributes being, 'name', 'id', 'class'. If you start to look for the element's xpath by browsing in the page source.
- Once you think you found the element, you will usually run into an error.
- You will need to select that element, in our case the element is only "selectable" if the tag of the element is 'select'.
- Once you have selected it, you will need to pick which option you want to pick. Usually, this is done by
select_by_visible_text
, orselect_by_index
Here is what the code would look like:
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.get('url')
# once you know what is the xpath
xpath = '//main/div/div[2]/section/div/select'
element = driver.find_element_by_xpath(xpath)
select_elem = Select(element)
# what ever you will select will be in an option tag
select_elem.select_by_visible_text('option')
You can see that quickly it will get messy, and you are prompted to so many errors, and you will be rewriting the code over and over again if you want to find the element using a tag's attributes or if you want to select another option.
A new web page will result in you having to write everything again from scratch.
Making the use of selenium efficiently
Copy-pasting is bad practice. If you are a developer, you have heard that over and over again. If we follow what we have in the code above, then we will be doing lots of copy-pasting.
By creating a class that can group the basic functionality of selenium, things can be much more efficient. We want to almost write a code that would be a layer higher than then functionalities offered by selenium.
For example, we want a method that can perform a drop_down_selection
, or perform a click. Let's see how this can be done!
- Try to write an algorithm that allocates all your select tags in your web app, or the current source that you are testing. That way, if you go to a new web page in your web application, you don't need to spend time trying to find you select tags and their xpaths.
- Start by scrapping your web page for the elements that you will need, and then use your algorithm ton generate its xpath.
- Write executable actions for example:
drop_down_selection() , perform_click()
Let's see how we can use this. I will use BeautiflSoup to scrape the elements:
from bs4 import BeautifulSoup
class ScrapinTools():
def get_all_select_tags(soup):
# this dictionary can hold you element id or name as a key and an xpath as its value
data = {}
for select in soup.findAll('select'):
xpath = xpath_soup_find(select)
if 'id' in select.attrs:
data[select.attrs['id']] = xpath
else:
data[select.attrs['name']] = xpath
return data
Now let's combine selenium functionality to create a higher functional layer based on it.
class WebINteraction():
def perform_dorpdown_selection(_name, _id, _xpath, **kwargs):
_name = kwargs.get(name,None)
_id = kwargs.get(elem_id, None)
_xpath = kwargs.get(xpath, None)
for key, value in kwargs.items():
if value is not None:
element = driver.find_element_by'
Opinions expressed by DZone contributors are their own.
Comments