Selenium with Ruby Cucumber

A guide to running Selenium Webdriver tests with Ruby Cucumber on BrowserStack.

Note: Code samples in this guide can be found in the cucumber-browserstack sample repo on GitHub

Introduction

BrowserStack gives you instant access to our Selenium Grid of 2000+ real devices and desktop browsers. Running your Selenium tests with Ruby Cucumber on BrowserStack is simple. This guide will help you:

  1. Run your first test
  2. Integrating your tests with BrowserStack
  3. Mark tests as passed or failed
  4. Debug your app

Prerequisites

Before you can start running your Selenium tests with Ruby Cucumber, install Cucumber using gem

gem install cucumber

Run your first test

Note: Testing on BrowserStack requires username and access key that can be found in account settings.
If you have not created an account yet, you can sign up for a Free Trial or purchase a plan.

To run your first Ruby Cucumber test on BrowserStack, follow the steps below:

  1. Clone the cucumber-ruby-browserstack sample repo on GitHub using the following command:

    git clone https://github.com/browserstack/cucumber-ruby-browserstack.git
    cd cucumber-ruby-browserstack
    
  2. Install browserstack-automate gem to run your Ruby Cucumber tests on BrowserStack.

     gem install browserstack-automate
    
  3. Add browserstack-automate to your cucumber-ruby-browserstack/features/support/env.rb file:

     require 'browserstack-automate'
     BrowserStack.for "cucumber"
    
  4. Run your first test using the BS_USERNAME and BS_AUTHKEY environment variables:

     cucumber BS_USERNAME=YOUR_USERNAME BS_AUTHKEY=YOUR_ACCESS_KEY BS_AUTOMATE_OS=Windows BS_AUTOMATE_OS_VERSION=10 SELENIUM_BROWSER=Chrome
    

    Note: In the above snippet, we have used Chrome on Windows 10. You can choose any other environment in the above step.

Details of your first test

The sample test that you just ran can be found in cucumber-ruby-browserstack/features/sample.feature file. The test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”:

sample.feature
module.exports = {
  'Google\'s Search Functionality' : function (browser) {
    browser
      .url('https://www.google.com/ncr')
      .waitForElementVisible('body', 1000)
      .setValue('input[type=text]', 'BrowserStack')
      .click('input[name=btnK]')
      .pause(1000)
      .assert.title('BrowserStack - Google Search')
      .end();
  }
};

Integrating your tests with BrowserStack

In the sample repository, you can find cucumber-ruby-browserstack/features/support/env.rb file which is responsible for configuring your test to run on BrowserStack. The useful sections of the file are shown below which enable the tests to run on BrowserStack:

env.rb
require 'selenium/webdriver'
require 'capybara/cucumber'
require 'browserstack/local'

url = "http://#{ENV['BS_USERNAME']}:#{ENV['BS_AUTHKEY']}@hub.browserstack.com/wd/hub"

Capybara.register_driver :browserstack do |app|

  capabilities = Selenium::WebDriver::Remote::Capabilities.new
	if ENV['BS_AUTOMATE_OS']
		capabilities['os'] = ENV['BS_AUTOMATE_OS']
		capabilities['os_version'] = ENV['BS_AUTOMATE_OS_VERSION']
	else
		capabilities['platform'] = ENV['SELENIUM_PLATFORM'] || 'ANY'
	end

	capabilities['browser'] = ENV['SELENIUM_BROWSER'] || 'chrome'
	capabilities['browser_version'] = ENV['SELENIUM_VERSION'] if ENV['SELENIUM_VERSION']
	capabilities['browserstack.debug'] = 'true'
	capabilities['project'] = ENV['BS_AUTOMATE_PROJECT'] if ENV['BS_AUTOMATE_PROJECT']
	capabilities['build'] = ENV['BS_AUTOMATE_BUILD'] if ENV['BS_AUTOMATE_BUILD']      
  capabilities['browserstack.local'] = 'false'      

  if capabilities['browserstack.local'] && capabilities['browserstack.local'] == 'true';
    @bs_local = BrowserStack::Local.new
    bs_local_args = { "key" => "#{ENV['BS_AUTHKEY']}", "forcelocal" => true }
    @bs_local.start(bs_local_args)
  end
  Capybara::Selenium::Driver.new(app, :browser => :remote, :url => url, :desired_capabilities => capabilities)


end

Capybara.default_driver = :browserstack
Capybara.app_host = "http://www.google.com"
Capybara.run_server = false

at_exit do
  @bs_local.stop unless @bs_local.nil?
end

Mark tests as passed or failed

BrowserStack does not know whether your test’s assertions have passed or failed because only the test script knows whether the assertions have passed. Therefore, based on the assertions on your script, you have to explicitly inform BrowserStack whether your tests have passed or not and this document will help you in doing that exactly.

It is possible to mark tests as either a pass or a fail and also give a reason for the same, using the following snippet:

capabilities["javascriptEnabled"] = "true" #Additionally, include this capability in the env.rb file for JavaScript executors to work

# for marking the test as passed
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "Yaay! My test has passed!"}}')

# for marking the test as failed
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "Oops! My test has failed!"}}')
Note: The arguments passed in the JavaScript method for setting the status and the corresponding reason for the test are status and reason.
  • status accepts either passed or failed as the value
  • reason accepts a value in string datatype

Marking test as pass/fail is also possible using our REST API at any point in the test or also after the test has concluded. You can read more about marking test using REST API and use it if it fits your use case.

It is recommended that you use the snippet shown above in an After hook of you tests where you can check the assertion status of your tests and invoke the JavascriptExecutor with the appropriate status.

Debug your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests. Learn more about how to debug tests on BrowserStack using the Automate Dashboard.

Text logs

Text Logs are a comprehensive record of your test. They are used to identify all the steps executed in the test and troubleshoot errors for the failed step. Text Logs are accessible from the Automate dashboard or via our REST API.

Visual logs

Visual Logs automatically capture the screenshots generated at every Selenium command run through your Cucumber tests. Visual logs help with debugging the exact step and the page where failure occurred. They also help identify any layout or design related issues with your web pages on different browsers.

Visual Logs are disabled by default. In order to enable Visual Logs you will need to set browserstack.debug capability to true.

desiredCapabilities: {
  'browserstack.debug': true
}

Sample Visual Logs from Automate Dashboard: BrowserStack Automate Visual Logs

Video recording

Every test run on the BrowserStack Selenium grid is recorded exactly as it is executed on our remote machine. This feature is particularly helpful whenever a browser test fails. You can access videos from Automate Dashboard for each session. You can also download the videos from the Dashboard or retrieve a link to download the video using our REST API.

Note: Video recording increases test execution time slightly. You can disable this feature by setting the browserstack.video capability to false.

In addition to these logs BrowserStack also provides Raw logs, Network logs, Console logs, Selenium logs, Appium logs and Interactive session. You can find the complete details to enable all the debugging options.

Next steps

Once you have successfully run your first test on BrowserStack, you might want to do one of the following:

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