Get Web Automation off the Ground with the Helium API
So, you need to automate, and you have never written a line of code before or it’s been a while. Fortunately, this isn’t a problem. The Helium API gives you simple commands and straightforward documentation to help get you started. It all begins and ends with Selenium, and if you need quick results this is the way to go.
One of the benefits of Selenium is that you can pick from a few languages to work with. I decided to go with Python, as I was familiar with it, and it’s easy to pick up if you don’t code often or at all. You’ll find it’s actually more difficult to setup your development environment than it will be to get your first script running.
So let’s get started…
Install Python, Eclipse, JDK and PyDev Recently, Android Studio replaced Eclipse as the official IDE for Android so you won’t be able to get Eclipse from the usual place.
-
Install Python from https://www.python.org/downloads/
-
For Windows, make sure you check the option to install it in the PATH
-
Install Eclipse for Java Developers from http://www.eclipse.org/downloads/
-
For Mac, install the 64-bit version
-
For Windows, install the 32 or 64 bit version depending on your OS
-
Install the latest version of the JDK from http://www.oracle.com/technetwork/java/javase/downloads/index.html
-
For Windows, install the x86 or 64 bit version depending on your OS
-
Edit eclipse.ini.
-
For Mac:
-
Right click on the Eclipse application, choose Show Package Contents and navigate to /Applications/eclipse/Eclipse.app/Contents/MacOS/eclipse.ini and open it
-
Append the following lines to the bottom of the file (be sure to replace the version number with yours):
-
-vm
-
/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home/bin/java
-
For Windows
-
Navigate to the same folder as the Eclipse application and open eclipse.ini
-
-vm
-
C:\Program Files (x86)\Java\jdk1.8.0_25\bin\javaw.exe
-
Download the latest version of PyDev from SourceForge at http://sourceforge.net/projects/pydev/files/pydev/.
-
Unzip its contents to /Applications/eclipse/dropins (or wherever you have Eclipse installed) and restart Eclipse, if opened.
Helium Setup
-
Download the Helium library from http://heliumhq.com/download
-
Extract the contents of the download
-
Place the extracted folder where you want to keep it
-
Open a terminal window and set your PYTHONPATH as described here: http://heliumhq.com/docs/getting_started
-
So, the only catch with Helium is that it is not directly compatible with Safari. You have to do some magic to get it all working, but here is a great article out there that walks you through the process:
-
For Windows / IE 11:
-
Follow the instructions for installing a registry key here: http://heliumhq.com/docs/internet_explorer
Install Selenium I realize that this post is about Helium, but I also quickly realized that I was going to have to learn Selenium. Don’t let this stop you though. Use Helium to get some traction and you will find yourself writing Selenium in no time.
-
For Mac:
-
Open Terminal
-
Run “pip install selenium.” If you don’t have full permission on your Mac, type “sudo pip install selenium”
-
For Windows
-
Open a command prompt
-
Navigate to C:\Python34\Scripts
-
Run “pip.exe install selenium”
-
Download the latest version of Selenium Server (formerly the Selenium RC Server) form: http://www.seleniumhq.org/download/
-
For Internet Explorer:
-
Download the 32 or 64 bit driver for IE from: http://www.seleniumhq.org/download/
-
Extract it
-
Complete the “Required Configuration” as described here: https://code.google.com/p/selenium/wiki/InternetExplorerDriver
-
For Chrome, install the ChromeDriver:
-
For Mac:
-
https://sites.google.com/a/chromium.org/chromedriver/downloads - download the latest version with the link noted with “Mac32”
-
Unzip and put the file in /Users/*name*/local/lib/node_modules/appium/build/chromedriver/windows
-
For Windows:
-
https://sites.google.com/a/chromium.org/chromedriver/downloads - download the latest version with the link noted with “Win32”
-
Put the chromedriver.exe file in C:\Python34\Scripts folder.
Environment Setup
-
Launch Eclipse
-
Choose your working folder (if first time launching it)
-
Choose PyDev in the top right of the window or…
-
Click Window
-
Click Open Perspective - Other…
-
Click PyDev
-
Click “File - New… - PyDev Project”
-
Complete the following on the form:
-
Project Name
-
Project Type
-
Grammar Version
-
Interpreter
-
You will have to click the link under this drop down and choose automatic setup if you have not set up a project before
-
Click "Create ‘src’ folder and add it to the PYTHONPATH
-
Working sets
-
Check “Add project to working sets”
-
Choose one or add one
-
You have to do this or you won’t be able to access your files in the Eclipse IDE
-
Click Finish
-
Right Click the project you just created
-
Click Properties
-
Click PyDev - PYTHONPATH
-
Click External Libraries
-
Click Add source folder
-
Navigate to and click the “helium-python-X.X.X\heliumlib” folder
-
Click OK
-
Click Add zip/jar/egg
-
Navigate and click the “selenium-server-standalone-2.44.0.jar” file
-
Click “Force restore internal info”
-
Click OK Create a package (just a directory for your source)
-
Expand your project tree
-
Right Click on the “src” folder
-
Click New - PyDev Package
-
Enter a name
-
Click Finish Create a module (just a source file, in this case a “.py” file)
-
Right Click on a package
-
Click New - PyDev Module
-
Enter a name
-
Click Finish
-
Click OK
Now you are ready to code! If you find yourself asking where you should go from here, reference the Helium API docs and the many Python resources on the internet. The Helium site provides a few examples that you can actually run and find your way.
Here a few pointers as you figure all of this out…
-
StackOverflow is your best friend - what a great community of developers
-
Do not search for “helium” unless you want to fill some balloons. Search for “selenium” and work through the information that is out there. The Helium commands correlate to Selenium commands and it’s a great way to start picking up Selenium.
-
Use the browser developer console and pull the objects “CSS Path” for reference in your code.
-
How to use CSS Path with Helium:
-
Open your browser and go to webpage
-
Right click on an element, i.e. button, clickable text, etc…
-
Click Inspect Element
-
Right click the highlighted section in the developer console
- You may need to do some refinement of the click area here - as you drag your mouse over the code you will see the highlight change
-
Click Copy CSS Path
-
Enter it as follows in you code: click(S(csspath))
-
-
A step further, make a variable for each CSS Path so you can easily reference them anywhere in the code
-
-
Make your functions small - don’t do too much at once so that you can reuse code as much as possible
Launching Safari This is all you need to launch Safari after completing the setup instructions above. You cannot use Helium commands, however, the last line “set_driver(driver)” passes the driver to Helium so you can execute the rest of your scripts in Helium.
import OS
from selenium import webdriver
os.environ["SELENIUM_SERVER\_JAR"] = "// _your path_ /selenium-server-standalone-2.44.0.jar"
driver = webdriver.Safari()
driver.get(" [https://www.google.com/](https://www.google.com/ "https://www.google.com/")")
set_driver(driver)
Launching IE I was having issues launching IE with helium, although it should work, so I did it with Selenium. Same as Safari above, the last line “set_driver(driver)” passes the driver to Helium so you can execute the rest of your scripts in Helium.
from selenium import webdriver
driver = webdriver.Ie()
driver.get(" [https://www.google.com/](https://www.google.com/ "https://www.google.com/")")
set_driver(driver)