Selenium 101: Running Your Test Against Two Browsers at Once

  February 06, 2018

If you are familiar with automated testing but haven’t yet explored the possibilities of parallel testing, we can tell you right now that you’ve been missing out. Rather than testing sequentially, or running each browser test one after the other, parallel testing allows you to run cross-browser tests simultaneously.

This means if you’re running a test in just two browsers at once, you’re already doubling your speed and productivity. When time is a factor to every software development team, parallel testing is a no-brainer. Selenium is able to run tests in parallel, but CrossBrowserTesting makes it even simpler - allowing you to test across multiple browsers and mobile devices with just a few extra lines of code.

Our example below will be using the Selenium's Python library, and we'll run our test against two browsers at once in CrossBrowserTesting with Selenium testing.

First, you want to choose the two browsers you’ll be running the test on. There are a few ways to choose which configuration to test, but the best way to do it for this purpose is to choose the two most common browser versions and operating systems your customers use to visit your web application with Google Analytics.

For this example, we’ll use IE 10 and Chrome 50 on Windows operating systems. Our capabilities will look something like this:

browsers = [

{“platform”: “Windows 7 64-bit”, “browserName”: “Internet Explorer”,”version”: “10”, “name”: “Python Parallel”}, {“platform”: “Windows 8.1”, “browserName”: “Chrome”, “version”: “50”, “name”: “Python Parallel”},

]

Next, we'll actually create the test itself, printing out relevant information like browserName to make our debugging experience easier. Our test will simply open CrossBrowserTesting.com and just wait for 3 seconds.

browsers_waiting = [ ]

def get_browser_and_wait(browser_data):

print ("starting %s\n" % browser_data["browserName"])

browser = get_browser(browser_data)

browser.get("http://crossbrowsertesting.com")

browsers_waiting.append({"data": browser_data, "driver": browser})

print ("%s ready" % browser_data["browserName"])

while len(browsers_waiting) < len(browsers):

print ("working on %s.... please wait" % browser_data["browserName"])

browser.get("http://crossbrowsertesting.com")

time.sleep(3)

For parallel testing, we’re going to use something called thread dependency, which you’ll see below. Multithreading is the implementation of parallel execution that communicates multiple processes on the CPU.

threads = []

for i, browser in enumerate(browsers):

thread = Thread(target=get_browser_and_wait, args=[browser])

threads.append(thread)

thread.start()

for thread in threads:

thread.join()

print ("all browsers ready")

for i, b in enumerate(browsers_waiting):

print ("browser %s's title: %s" % (b["data"]["name"], b["driver"].title))

b["driver"].quit()

Parallel testing becomes really handy when trying to increase the speed of your testing or actual testing coverage without sacrificing quality. Running a test against two browsers is a really great place to start with this script and already allows you to test twice as fast as doing the same testing sequentially on different browsers.

Once you begin using this method of testing on multiple browsers to extend the reach of longer test cases, the value will be evident. A test suite that previously ran for 5 hours can now be affordably decreased to around 15 minutes, giving your development and testing teams quicker feedback. You can also take your testing suite and boost your coverage immediately, running it against the browsers and devices your customers are actually using. If you extend this over day, weeks, and months, you'll be benefiting from hours on top of hours of productivity.

For more of our Selenium 101 Series, take a look at the following:

To see more examples in other languages: