Browser automation with Helium

When assessing the tools to take care of browser automation to test your new web app. You might instinctively be drawn to Selenium, Cypress or maybe alternative solutions like Puppeteer.

These established libraries are perfect for building frameworks that test the whole of your web application. Whether it’s an eCommerce site, CRM tool, or SaaS product. These tools have been tried and tested by many people, in many situations. And have been proven to be the right solution for most peoples needs.

One of the problems that Selenium has is there is a learning curve to using it well. A learning curve that can be quite steep if you don’t know your way around the DOM. Or know your CSS selector from your XPATH.

Selenium is also prone to flaky tests if they are not coded well. Leading to tests failing and lost time in trying to debug the cause of your newest error message.

When building a new automation framework. Your goal needs to be making a quick ROI on the investment you are making. Time spent trying to solve issues with your framework is a barrier to a positive result.

Open-source rocks

I adore open-source software. I find it allows people to be expressive and creative with their programming abilities. Often working with only the recognition of the community to appreciate their efforts. Finally, releasing their project for free on Github for us all to consume, modify and learn from.

One of my favourite places to go to find out about new open-source projects is Hacker News. Which I’m sure many people reading this blog are familiar with.

But in case you’re not, Hacker News is what is known as a news aggregator. It collects news articles from peoples own submissions and displays them to visitors. It’s fairly easy to lose hours at a time reading the various posts on the site and discovering new and exciting projects.

This is what led me to discovering Helium.

What is Helium?

Helium describes itself as ‘Lighter web automation based on Selenium’.

It’s important to note here that the Helium is only for Python. There was a paid version that included Java code. But that stopped being offered at the end of last year and the Python code was open-sourced.

From Heliums Github:

Helium is a Python library for automating browsers such as Chrome and Firefox. It wraps around Selenium-python to provide an API that is much easier to use. Your automation scripts become 50% shorter, easier to read and more stable. At the same time, you don’t lose anything by using Helium, because you can freely mix it with Selenium.

Essentially, Helium is a wrapper around Selenium’s inbuilt functions and methods. Allowing users to write code that is much more high-level, shorter and easier to read.

Using Helium

Here is a sample script which I wrote to test the menu structure for Docket.

It accesses the application and ensures that the top menu text contains the ‘Register’ text option. Please note that if you want to run the example, you will need pytest.

from helium import *

def docket_page():
    start_chrome('https://docket-test.herokuapp.com/')
    menu_text = Text(to_right_of='Login').value
    kill_browser()
    return menu_text

def test_docket_should_have_register_text():
    result = docket_page()
    assert 'Register' in result

Easy huh?

You can also easily change the browser which is used by entering:

start_firefox

This gives you the option to change browers without having to use configuration files or modify your settings. Enabling the ability to test in multiple browsers very easily.

Go headless

Headless browsers have many advantages over the regular UI based applications that we interact with every day.

Not having to deal with a clunky interface that can slow down your test scripts, or consume those valuable resources that you need. Headless browsers, as a result, are much faster than their traditional counterparts.

They allow you to run your tests, perform actions like web scraping, and monitor network performance in no time at all.

However, if you wish to monitor your tests running or perform visual checks, using a headless option might not be the best solution for your needs.

If you do want to turn on headless browsing in Helium though. All you need to add to the start_chrome() call is:

headless=True

Firefox works for this as well!

Use Selenium’s own API

As mentioned, Helium is a wrapper around the core methods of Selenium. Which enables users of Helium to mix Selenium’s own API calls with Helium. For example:

# A Helium function:
driver = start_chrome()
# A Selenium API:
driver.execute_script("alert('Hello World!');")

This allows users of Helium to take advantage of the various methods built-into the library. As well as make low-level calls to Selenium’s own API if they so wish.

Conclusion

Being able to use Helium code as well as Selenium’s own is probably what makes this such an interesting library. It reminds me of the goal of the page object model which I use in all of my Selenium frameworks. Containing each function call in its own page object. Cutting down on the number of lines of code in each test, making each one much more readable.

One negative that I can see is that Helium only has support for Chrome & Firefox built-in. But I assume this can be changed by using Selenium’s own API calls.

You can read more about Helium on the Github page for the project. As well as download the above test code here.

Posted by Kevin Tuck

Kevin Tuck is an ISTQB qualified software tester with nearly a decade of professional experience. Well versed in creating versatile and effective testing strategies. He offers a variety of collaborative software testing services. From managing your testing strategy, creating valuable automation assets, or serving as an additional resource.

Leave a Reply