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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering Async Context Manager Mocking in Python Tests
  • Supercharging Pytest: Integration With External Tools
  • Exploring the Purpose of Pytest Fixtures: A Practical Guide
  • Automating Python Multi-Version Testing With Tox, Nox and CI/CD

Trending

  • Avoiding AI Debt as a Team
  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • AI Agents Need an Identity Plane: Rethinking Enterprise Authorization for Autonomous Software
  • YAGNI in the Age of AI Coding

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Languages
  4. 5 Tips Of GUI Tests With Python + Selenium

5 Tips Of GUI Tests With Python + Selenium

In this article, we go over some best practices for testing the performance of your web applications using Python and Selenium.

By 
Denny Zhang
·
Oct. 14, 17 · Analysis
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
15.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

I have been using Python + Selenium for years. Honestly, I'm far from a frontend expert or a QA expert. Here are my lessons learned from this journey.

If you're also a Python user and need GUI testing, check this out!

1. Use Docker to Setup Selenium Headless Environments
Setting up GUI testkit takes effort. Especially in the old days! Ever heard of something like Xvfb[1], Xephyr, PyVirtualDisplay[2], etc?

Too much information, isn't it? It will blow your mind. Not to mention the maintenance effort. And yes, the troubleshooting!

#1 Tip: Use Docker to Setup TestKit.

Check the dockerfile in GitHub.

# Start container
docker run -d --restart=always \
    -v /tmp/screenshot:/tmp/screenshot \
    -h selenium --name selenium denny/python-selenium:v1

# Run A Sample Test.
# With customized test scripts, you can do more!
docker exec selenium \
   python /home/seluser/selenium_load_page.py \
   --page_url https://www.dennyzhang.com --should_save_screenshot

# Check Screenshot
ls -lth /tmp/screenshot

With the above simple commands, your remote Xvfb server should be up-and-running now.

If it had crashed, your container will crash too. Consequently, it will be restarted automatically. Beautiful, isn't it?

2. Deal With Async Loading for JavaScript and CSS

contact$dennyzhang.com docker exec -it selenium ps -ef | grep -i xvfb
seluser     12     1  0 Sep04 ?        00:00:00 /bin/sh /usr/bin/xvfb-run -n 99
seluser     23    12  0 Sep04 ?        00:00:00 Xvfb :99 -screen 0 1360x1020x24

In web programming, async loading is quite common. Especially with Node.js. So before we check UI functionalities, we need to wait until for the elements to be shown.[3]

from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
driver.get("http://somedomain/url_that_delays_loading")
myDynamicElement = driver.find_element_by_id("myDynamicElement")

3. Raise Alerts, if Page Load Is Too Slow
Apparently, the first priority is making sure the major functionalities work.

But also remember to watch out for slowness. This helps people to detect performance issues.

import time

print("Open page: %s" % (page_url))
start_clock = time.clock()

gui_login_webpage(driver, page_url, username, password)

end_clock = time.clock()
elapsed_seconds = ((end_clock - start_clock) * 1000)
is_ok = True
if elapsed_seconds > max_load_seconds:
    print("ERROR: page load is too slow. It took %s seconds, more than %d seconds." \
          % ("{:.2f}".format(elapsed_seconds), max_load_seconds))
    is_ok = False
else:
    print("Page load took: %s seconds." % ("{:.2f}".format(elapsed_seconds)))

4. Raise Alerts if Any 5XX or 4XX Errors Are Detected.
One URL request will launch lots of HTTP sub-requests. If any requests run into issues, we need to notify the developers.

IGNORE_ERROR_LIST = ["favicon.ico"]

all_warnings = driver.get_log('browser')
critical_errors = []
for warning in all_warnings:
    if warning['level'] == 'SEVERE':
        has_error = True
        for ignore_err in IGNORE_ERROR_LIST:
            if ignore_err in warning['message']:
                has_error = False
                break
        if has_error is True:
            critical_errors.append(warning)

5. Wrap GUI Test as Jenkins Jobs
People only accept solutions which are easy to use. So let's use Jenkins to achieve visualOps.

Quick Python Selenium CheatSheet[4]

  • find element by name
  • driver.find_element_by_name("emailAddress")
  • find element by id
  • driver.find_element_by_id("password")
  • find element by css
  • driver.find_elements_by_class_name("f-launchpad")
  • find element by xpath
  • driver.find_elements_by_xpath(xpath="//div[@label='Here']")
  • save screenshot

  • driver.save_screenshot_as_file('/tmp/screenshot.png')

Python (language) Testing

Published at DZone with permission of Denny Zhang. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Async Context Manager Mocking in Python Tests
  • Supercharging Pytest: Integration With External Tools
  • Exploring the Purpose of Pytest Fixtures: A Practical Guide
  • Automating Python Multi-Version Testing With Tox, Nox and CI/CD

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook