5 Tips Of GUI Tests With Python + Selenium

I have been using Python + Selenium for years. Honestly speaking, 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!

5 Tips For GUI Automation Test In Python

1. Use Docker To Setup Selenium headless Environments
Setup GUI testkit takes effort. Especially in 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 trouble shooting!

Remember Using Docker To Setup Your GUI TestKit

Check dockerfile in github. 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 above simple command, your remote Xvfb server would be up and running now.

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

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

Add your own test script

Inside the container, we have /home/seluser/seleniumloadpage.py. It’s the GUI test script.

Definitely you will need to implement your own GUI test coode, right? Once it’s done, you can mount your script as docker volume.

Then start the container with the volume, by docker/docker-compose/kubernets.

So it’s like GUI testkit(container) + test scripts(docker volume).

2. Deal with async loading for javascripts and css

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

from selenium import webdriver

driver = webdriver.Firefox()
driver.implicitly_wait(10) # seconds
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 the slowness. This helps people to detect the 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
    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 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
        if has_error is True:

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]

Name Summary
find element by name driver.findelementbyname(“emailAddress”)
find element by id driver.findelementbyid(“password”)
find element by css driver.findelementsbyclassname(“f-launchpad”)
find element by xpath driver.findelementsbyxpath(xpath=”//div[@label=’Here’]”)[5]
save screenshot driver.savescreenshotasfile(‘/tmp/screenshot.png’)[6]

[1] https://en.wikipedia.org/wiki/Xvfb
[2] https://pypi.python.org/pypi/PyVirtualDisplay
[3] http://selenium-python.readthedocs.io/waits.html
[4] http://selenium-python.readthedocs.io
[5] http://selenium-python.readthedocs.io/locating-elements.html
[6] http://jjbohn.info/blog/2013/07/31/how-to-take-a-screenshot-with-rubys-selenium-web-driver/

More Reading:


PRs Welcome

Blog URL: https://www.dennyzhang.com/python_selenium

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.