Selenium with Ruby

Your guide to running Selenium Webdriver tests with Ruby on BrowserStack.

Introduction

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

  1. Run your first test
  2. Mark tests as pass / fail
  3. Debug your app

Prerequisites

Before you can start Selenium testing in Ruby, you have to first install the Selenium bindings for Ruby. Use this Ruby gem to install the selenium-webdriver bindings from your command line:

# Install using rubygems

gem install selenium-webdriver

Run your first test

Note: Running your Selenium tests on BrowserStack requires a username and an access key.

To obtain your username and access keys, sign up for a Free Trial or purchase a plan.

To get started, let’s run a simple Selenium Webdriver test. The Ruby script below will open a URL, input a string, submit the form, and return the page title.

First, select the OS and Device/Browser combination you’d like to test on using the drop-down menus below. This will automatically update the Ruby code sample below:

Note: Testing on real devices requires the Automate Mobile plan

Look for the icon to select a real device.

 

Warning: The driver.quit statement is required, otherwise the test continues to execute, leading to a timeout.

Second, copy-and-paste the code sample into your code editor, save it as a .rb file, and execute the test from your command line.

Third, verify the results. The Selenium Webdriver test should have opened a URL, inputted a string, submitted the form, and returned the page title. Your results will be displayed on the command-line interface and on the Automate dashboard, where you can see Text Logs, Screenshots of every Selenium command, and a Video Recording of your entire test.

Mark tests as pass / fail

It is possible to mark tests as either a pass or a fail, using the following snippet:

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

The two potential values for status can either be passed or failed. Optionally, a reason can also be passed.

Debug 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 Ruby script. 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:

caps = Selenium::WebDriver::Remote::Capabilities.new
caps["browserstack.debug"] = "true"
  • 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.

caps = Selenium::WebDriver::Remote::Capabilities.new
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. Complete details to enable all the debugging options can be found here.