Selenium Tips: How to Coordinate Multiple Browsers in Sauce OnDemand

Posted by Santiago Suarez Ordoñez in Selenium Resources

Let's imagine your team is making a chat application. Nothing too fancy, just something simple in which you need to make sure multiple users get together, talk, and read each other. Doesn't sound that bad, huh? But how about coordinating those browsers? You need to have different sessions running at the same time in coordination and interacting with each other through your web app. Now, when we think about parallelization and multiple browsers, Sauce OnDemand is, of course, our first answer, but here, there can be a small limitation. Sauce OnDemand has a special timeout that waits for new commands for no more than 90 seconds*. If it doesn't get any new command, it will kill the job and assume something went wrong. This effects us, as we need to get multiple browsers running in parallel and in coordination. As we request them one after another, depending on the amount of browsers and the load our service is put under, the first ones might timeout while waiting for the others to come. To avoid this, the best solution is to write a multi-threaded script, in which browsers will send heartbeat commands while they are waiting for the rest. Multi-threading can be done with every programming language, but I decided to write my example in Python because I think it's understandable for almost anyone, even if you've never seen Python before. The complete example is in github as a gist, and here is a brief description of what each interesting part is doing:

def get_browser_and_wait(browser, browser_num):
    print "starting browser %s" % browser_num
    browser.start()"/") # if we get here, OnDemand already gave us the browser
    print "browser %s ready" % browser_num
    while len(browsers_waiting) < len(browsers):
        print "browser %s sending heartbeat while waiting" % browser_num"/")

get_browser_and_wait is the function that will take care of the 'start the browser' request in OnDemand and wait till it comes back. Once the server is ready, it will keep sending open commands as heartbeats and waiting until the other requested browsers are ready and waiting too.

thread_list = []
for i, browser in enumerate(browsers):
    t = Thread(target=get_browser_and_wait, args=[browser, i + 1])

for t in thread_list: t.join()

This is the magic multithreading part, in which we iterate over every Selenium instance and call get_browser_and_wait with it. Once we send each browser to a thread, the main thread will continue and get to the t.join() part. By this method, the main thread will wait for every browser thread to complete in order to proceed with the rest of the code. Again, the full example is up in github: If you're interested in learning what you can use this kind of test for, stay tuned! * This helps avoid running (and charging) tests that get disconnected or crash.

Discuss: Selenium Tips: How to Coordinate Multiple Browsers in Sauce OnDemand

Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.