Your guide to running Selenium Webdriver tests with Capybara on BrowserStack.
Note: Refer our sample repo on Github: Capybara repo on Github
BrowserStack gives you instant access to our Selenium Grid of 2000+ real devices and desktop browsers. Running your Selenium tests with Capybara on BrowserStack is simple. This guide will help you:
Before you can start running your Selenium tests with Capybara, ensure you have the Capybara libraries installed:
# Install using gem
gem install capybara
To understand how to integrate with BrowserStack, we will look at two things:
The sample Capybara test case below searches for the string “BrowserStack” on Google, and checks if the title of the resulting page is “BrowserStack - Google Search”
# Google Feature
Feature: Google Search Functionality
Background:
Given I am on https://www.google.com/ncr
Scenario: Can find search results
When I fill in "q" found by "name" with "BrowserStack"
And I submit
Then I should see title "BrowserStack - Google Search"
# Google Steps
Given /^I am on (.*)$/ do |url|
visit url
end
When /^I fill in "([^\"]*)" found by "([^\"]*)" with "([^\"]*)"$/ do |value, type, keys|
fill_in(value, :with => keys)
end
When /^I submit$/ do
find_field('q').native.send_key(:enter)
end
Then /^I should see title "([^\"]*)"$/ do |title|
expect(page).to have_title title
end
Once we have defined the test case, we are ready to integrate this Capybara test case into BrowserStack.
Note: Running your Selenium tests on BrowserStack requires a BrowserStack Username and Access Key.
To obtain your username and access keys, sign up for a Free Trial or purchase a plan.
We can now integrate our Capybara test case into BrowserStack. Integration of Capybara with BrowserStack is made possible by use of following module:
require 'yaml'
require 'selenium/webdriver'
require 'capybara/cucumber'
require 'browserstack/local'
# monkey patch to avoid reset sessions
class Capybara::Selenium::Driver < Capybara::Driver::Base
def reset!
if @browser
@browser.navigate.to('about:blank')
end
end
end
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']
Capybara.register_driver :browserstack do |app|
@caps = CONFIG['common_caps'].merge(CONFIG['browser_caps'][TASK_ID])
# Code to start browserstack local before start of test
if @caps['browserstack.local'] && @caps['browserstack.local'].to_s == 'true';
@bs_local = BrowserStack::Local.new
bs_local_args = {"key" => "#{CONFIG['key']}"}
@bs_local.start(bs_local_args)
end
Capybara::Selenium::Driver.new(app,
:browser => :remote,
:url => "https://#{CONFIG['user']}:#{CONFIG['key']}@#{CONFIG['server']}/wd/hub",
:desired_capabilities => @caps
)
end
Capybara.default_driver = :browserstack
# Code to stop browserstack local after end of test
at_exit do
@bs_local.stop unless @bs_local.nil?
end
The module reads from config file where you need to put the BrowserStack Hub URL and credentials.
server: "hub-cloud.browserstack.com"
user: "YOUR_USERNAME"
key: "YOUR_ACCESS_KEY"
common_caps:
"browserstack.debug": true
"name": "Bstack-[Capybara] Sample Test"
browser_caps:
-
"browser": "chrome"
We are now ready to run the test on BrowserStack, using the following command:
# Run using rake
bundle exec rake single
BrowserStack provides a comprehensive REST API 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 Capybara test cases.
require 'rest_client'
RestClient.put 'https://YOUR_USERNAME:YOUR_ACCESS_KEY@api.browserstack.com/automate/sessions/<session-id>.json', {"status"=>"completed", "reason"=>""}, {:content_type => :json}
The two potential values for status can either be completed or error. Optionally, a reason can also be passed. A full reference of our REST API can be found here.
BrowserStack provides a range of debugging tools to help you quickly identify and fix bugs you discover through your automated tests.
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 automatically capture the screenshots generated at every Selenium command run through your PHP 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"
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.
Contact our Support team for immediate help while we work on improving our docs.
Contact our Support team for immediate help while we work on improving our docs.