Skip to main content
Looking to test on every commit or PR? Introducing plans for high-scale testing on Chrome and Firefox. Read more

Run Tests in Parallel

A guide to running Ruby tests in parallel to achieve faster execution of builds.

On BrowserStack, you can run multiple Selenium Webdriver tests at the same time across various browser, device and OS combinations. This is called Parallel Testing. Parallel Testing gives you the same benefits as running a multi-threaded application.

With Parallel Testing, you can run the same test on different browser/device combinations i.e. cross-browser testing, or run different tests on the same or different browser/device combinations. Parallel Testing will help you reduce the run time of your test suite, resulting in faster build times and faster releases.

You can start testing in parallel using one of the popular test frameworks which work with Ruby and Selenium or you can also choose to run tests in parallel without a framework. We have provided getting started guides on some of the popular frameworks below:

Run tests in parallel without a framework

You can achieve parallel testing in multiple ways even if you are not using one of the popular test frameworks which has feature for enabling parallel testing. We show two such ways below but this is not an exhaustive list:

Using a multi-threaded program

The following sample script shows a multi-threaded Ruby program. These are the salient points of the script:

  • The same script is run across five different browser and OS combinations viz. Chrome on Win10, Firefox on Win10, Safari on MacOS, Edge on Win10, Chrome with the latest available beta version on Win10.
  • The run_session function takes capabilities as arguments and executes the same test script on each set of the browser/OS combinations.
require 'rubygems'
require 'selenium-webdriver'

def run_session(browser, version, os, os_version, test_name, build_name)
	caps = Selenium::WebDriver::Remote::Capabilities.new
	caps['browser'] = browser
	caps['os_version'] = os_version
	caps['os'] = os
	caps['browser_version'] = version
	caps['name'] = test_name # test name
	caps['build'] = build_name # CI/CD job or build name

	driver = Selenium::WebDriver.for(:remote,
	  :url => "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
	  :desired_capabilities => caps)
	# Searching for 'BrowserStack' on google.com
	driver.navigate.to "http://www.google.com"
	element = driver.find_element(:name, "q")
	element.send_keys "BrowserStack"
	element.submit
	wait = Selenium::WebDriver::Wait.new(:timeout => 5) # seconds
  begin
    wait.until { !driver.title.match(/BrowserStack/i).nil? }
    driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Yaay! Title matched!"}}')
  rescue
    driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Oops! Title did not match"}}')
  end
  driver.quit
end

t1 = Thread.new{run_session("Chrome", "latest", "Windows", "10", "Sample test on Chrome", "Parallel-build-ruby")}
t2 = Thread.new{run_session("Firefox", "latest", "Windows", "10", "Sample test on Firefox", "Parallel-build-ruby")}
t3 = Thread.new{run_session("Safari", "latest", "OS X", "Catalina", "Sample test on Safari", "Parallel-build-ruby")}
t4 = Thread.new{run_session("Edge", "latest", "Windows", "10", "Sample test on Edge", "Parallel-build-ruby")}
t5 = Thread.new{run_session("Chrome", "latest-beta", "Windows", "10", "Sample test on Chrome-latest-beta", "Parallel-build-ruby")}

t1.join()
t2.join()
t3.join()
t4.join()
t5.join()

Using a multi-threaded program and using ThreadPoolExecutor to manage threads

In case of the above implementation, all the threads are dispatched at the same time. You can also manage the dispatching of threads by applying additional conditions, but, in that case, you would have to manually keep track of which thread has completed its execution and depending on that, you will be required to spawn new threads keeping in mind you do not exceed the limit of parallel tests that is supported by your BrowserStack account.

You can choose to not manage the threads manually and use the Ruby ThreadPool to do the same for you by following the steps mentioned in the ruby thread pool guide

Conclusion

You can run parallel tests on BrowserStack even if you are not using a testing framework by choosing one of the above-shown methods or even build your own logic in a shell script that spawns multiple parallel processes and run tests in parallel in those processes.

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Talk to an Expert