Think Labs is an ongoing effort by Seven2 to provide research and educational opportunities in the web development and mobile field. To see what we’ve been cookin’ up, check out our blog postings.

Created by
Seven2 Login

Team Think Labs | Selenium Browser Automation
single,single-post,postid-4983,single-format-standard,ajax_fade,page_not_loaded,,,wpb-js-composer js-comp-ver-4.2.3,vc_responsive

Selenium Browser Automation

Ever wanted to punch a hole in your monitor while performing countless, repetitive, mind-numbing tests to make sure your website is working properly? Or perhaps you just wish you could create thousands of drone twitter followers to make yourself feel popular.


Selenium Browser Automation

Selenium is a nifty little browser automation framework that can be used as an effective tool in web development by creating automated tests to assure your website works as expected. This can cut down on time spent drudging through manual tests to verify everything works after an update.

Selenium WebDriver can be implemented in a variety of scripting languages, including Java, Python, and PHP. Selenium IDE offers a lightweight alternative, via a plugin for Firefox that requires no programming knowledge. Either option can be an effective choice, with the WebDriver offering a more comprehensive testing framework, with the ability to include logic and more in-depth analysis.


Selenium IDE

Selenium IDE is a practical tool for developing lightweight browser automation scripts with just a few simple clicks. Simply install the plugin from the Selenium downloads page and launch it directly from your Firefox browser.


Simply click the red ‘Record’ button in the top right of the IDE window to start recording actions. Every action performed in the browser during recording will create a new command in the test script, from loading a new page to clicking an element or entering text.


Click the button (now green) again to stop recording. Then click the ‘Play’ button in the upper left to run the script. You should see your browser quickly perform each command in the script in quick succession – it’s that easy!

More options to create commands can be viewed by right-clicking any element on the page.


For commands such as ‘Assert’ and ‘Verify’ the script will terminate and report an error if the assertion or verification fails. Additionally, waits will terminate the script if they time-out, and any other actions will terminate the script if they are unable to be carried out – such as attempting to click an element that doesn’t exist on the page. This is where Selenium becomes useful for developers: failed tests can turn up unexpected problems with a site.

The Selenium IDE has a number of other features, including the ability to change the speed at which the script is run, in order to either blaze through it, or deliberately perform each command to allow for a better look at each step. Test scripts can also be grouped together and run consecutively, which can be useful to avoid redundancies. Finally, your script can be exported as code in any of the languages supported by Selenium WebDriver.


Selenium WebDriver

The Selenium WebDriver is a much more effective tool for those with programming experience who want to create more dynamic test scripts. It supports a wide variety of languages, but Python is by far the easiest one to get your project off the ground. Python also lends itself well to the types of scripts you’ll be writing with Selenium.

*A quick note – Selenium WebDriver allows you to run your test scripts on Chrome & IE as well as Firefox, with just a bit of extra work

To install the Selenium WebDriver framework with Python:

- Download and install Python

- Install ‘pip’ by opening your terminal and executing the following command:

python get-pip.py

- Ensure pip is up-to-date:

pip install -U pip

- Finally, install Selenium:

pip install -U selenium

That’s it! You’re ready to start writing test scripts. For those not familiar with Python syntax, this is a good place to start. It’s very user-friendly and easy to learn. The Selenium WebDriver documentation is also a useful resource, and perhaps even more so is the actual Selenium API.

Below is a simple sample test script written in Python. It simply starts a new instance of the Firefox WebDriver, does a quick google search and verifies that a specific result exists on the page, then opens the link to that search result.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#Create an instance of the Firefox WebDriver
driver = webdriver.Firefox()

#Navigate to Google

#Locates the Google search box
search_box = driver.find_element_by_name("q")

#Searches for 'Seattle Mariners'
search_box.send_keys("Seattle Mariners")

#Clicks the search button (a bit redundant since the results appear as you type)

#Waits for the desired link to appear, times out at 3 seconds
link = WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.PARTIAL_LINK_TEXT, "The Official Site of The")))

#Finally, clicks the link to open the Seattle Mariners home page!


Useful Resources

Selenium WebDriver API

Quick Intro to Selenium

Selenium Page Object Design Pattern

Python for Java Programmers