Automating Web UI with with Helium, Python & Selenium.
One of the toughest parts of writing good automated tests is finding the right selectors. Even with tools like ChroPath to assist in making the right decision it can still be very time consuming to locate the right selectors. Even when you have chosen the right selector, you’re often reliant on a descriptive variable name to make the code more readable.
Helium has a different, and novel, approach to test automation which aims to address some of the complications with using the right element selectors and make writing automated tests more simple.
What is Helium?
Helium is a python library which acts as a wrapper around selenium to automate web browsers. Helium’s Unique Selling Point is that it uses user visible text to locate elements rather than more traditional selectors. So, for example, instead of writing
you can write
Helium also has a nice feature which allows you to locate an element based on its location relative to other elements. for example name = Text(to_right_of=’Name:’).value gets the text of an element on the right of the name field.
Not only that, being a wrapper around selenium means all the familiar selenium commands are still available to use in conjunction with Helium. OK , now we know a bit about it we can look at a working example.
It’s super easy to get started with helium. Provided you have Python 3 installed it’s a simple pip install
Pip install helium
Writing your first test
In this demo we’re going to automate the registration page of this todo app. The registration has four boxes to input text and a Register button.
So, lets write our test. We’ve already installed helium so next we need to create a python file in our directory. I’ll call it test_registration.py. we can now import Helium into our file using the following line.
from helium import *
Now we’ve imported Helium, we’re ready to write our test steps. First we need to open the browser and navigate to our site. We can do that in a single line. In this example we’re using chrome but Helium comes prepackaged with both the ChromeDriver and geckodriver (Firefox) .
Next lets complete the four text input fields required like this
write('John', into='Username') write('email@example.com', into='Email') write('Password1', into='Password') write('Password1', into='Repeat Password')
In the above code we’re using the ‘write’ function which take two parameters the text we want to write and the filed we want to write to.
Note that the value for ‘into=’ is actually the visible text for each field.
so far so good. Now lets register our user by clicking on the Register Button using the click function, like so.
Cool! if we run our test now we we should see the values being input and the register button being clicked. Once registered the screen will look like this.
but, how can we verify that the user is registered, well lets write an assertion to confirm we can see the ‘Congratulations, you are now registered‘ text on our screen.
assert Text("Congratulations, you are now registered").exists()
Awesome! We now have our first Helium test which registers a user and validates the Congratulations message, Nice. Lets see it running.
I hope you found this useful. for more information visit these pages.