DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Languages
  4. Web-Testing Automation in Python

Web-Testing Automation in Python

A description of different testing methods for Python.

Mark Bastawros user avatar by
Mark Bastawros
·
Jul. 25, 19 · Presentation
Like (4)
Save
Tweet
Share
4.80K Views

Join the DZone community and get the full member experience.

Join For Free

Python 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:

  1. 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.
  2. Once you think you found the element, you will usually run into an error.
  3. You will need to select that element, in our case the element is only "selectable" if the tag of the element is 'select'.
  4. 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 , or select_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!

  1. 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.
  2. Start by scrapping your web page for the elements that you will need, and then use your algorithm ton generate its xpath.
  3. 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'
unit test Python (language) Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • gRPC on the Client Side
  • Strategies for Kubernetes Cluster Administrators: Understanding Pod Scheduling
  • Building a REST API With AWS Gateway and Python
  • Tackling the Top 5 Kubernetes Debugging Challenges

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: