Selenium with RSpec

A guide to running Selenium Webdriver tests with RSpec on BrowserStack.

Note: Code samples in this guide can be found in the rspec-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 RSpec on BrowserStack is simple. This guide will help you in:

  1. Running your first test
  2. Integrating your tests with BrowserStack
  3. Marking tests as passed or failed
  4. Debugging your app

Prerequisites

Before you can start running your Selenium tests with RSpec, install RSpec:

gem install rspec

Running 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 RSpec test on BrowserStack, follow the steps below:

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

    git clone https://github.com/browserstack/rspec-browserstack.git
    cd rspec-browserstack
    
  2. Install the dependencies using bundle install
  3. Setup your credentials in the rspec-browserstack/config/single.config.yml file as shown below:

    single.config.yml
     server: "hub-cloud.browserstack.com"
     user: "YOUR_USERNAME"
     key: "YOUR_ACCESS_KEY"
    
     common_caps:
       "build": "rspec-browserstack"
       "browserstack.debug": true
    
     browser_caps:
       -
         "browser": "chrome"
    
  4. Run your first test using the following command:
    bundle exec rake single
    
  5. You can visit BrowserStack Automate Dashboard and see your test there once it has successfully completed.
Protip: You can use our capability builder and select from a wide range of custom capabilities that BrowserStack supports.

Details of your first test

The sample test that you just ran can be found in the sample repo’s file rspec-browserstack/spec/single_test.rb. The test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”:

single_test.rb
describe "Google's Search Functionality" do
  it "can find search results" do
    @driver.navigate.to "https://www.google.com/ncr"
    element = @driver.find_element(:name, 'q')
    element.send_keys "BrowserStack"
    element.submit
    sleep 5
    expect(@driver.title).to eql("BrowserStack - Google Search")
  end
end

Integrating your tests with BrowserStack

In the sample repository, you can find rspec-browserstack/scripts/browserstack.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:

require 'yaml'
require 'rspec'
require 'selenium-webdriver'
require 'browserstack/local'

TASK_ID = (ENV['TASK_ID'] || 0).to_i
CONFIG_NAME = ENV['CONFIG_NAME'] || 'single'

CONFIG = YAML.load(File.read(File.join(File.dirname(__FILE__), "../config/#{CONFIG_NAME}.config.yml")))
CONFIG['user'] = ENV['BROWSERSTACK_USERNAME'] || CONFIG['user']
CONFIG['key'] = ENV['BROWSERSTACK_ACCESS_KEY'] || CONFIG['key']


RSpec.configure do |config|
  config.around(:example) do |example|
    @caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID])
    @caps["name"] = ENV['name'] || example.metadata[:name] || example.metadata[:file_path].split('/').last.split('.').first
    enable_local = @caps["browserstack.local"] && @caps["browserstack.local"].to_s == "true"

    # Code to start browserstack local before start of test
    if enable_local
      @bs_local = BrowserStack::Local.new
      bs_local_args = { "key" => CONFIG['key'], "forcelocal" => true }
      @bs_local.start(bs_local_args)
      @caps["browserstack.local"] = true
    end

    @driver = Selenium::WebDriver.for(:remote,
      :url => "https://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub",
      :desired_capabilities => @caps)

    begin
      example.run
    ensure
      @driver.quit
      # Code to stop browserstack local after end of test
      @bs_local.stop if enable_local
    end
  end
end

Marking tests as passed or failed

BrowserStack provides a comprehensive set of REST APIs to access and update information about your tests. Shown below is a sample code snippet which allows you to mark your tests as pass or fail based on the assertions in your RSpec test cases.

require 'rest_client'
RestClient.put 'https://YOUR_USERNAME:YOUR_ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json', {"status"=>"<passed/failed>", "reason"=>"<string reason goes here>"}, {:content_type => :json}

You can find the full reference to our REST API.

Debugging your app

BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.

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 JUnit 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.

common_caps:
  "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.
common_caps:
  "browserstack.video": 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