CrossBrowserTesting Support

Support

  • Product
    • Live Testing
    • Automated Testing
    • Record & Replay
    • Visual Testing
  • API Docs
  • Blog
  • Live Q&A
  • Start Testing
CrossBrowserTesting Support » Screenshots » Tutorials » Automating Screenshot Tests using Ruby and cbt_ruby

Automating Screenshot Tests using Ruby and cbt_ruby

You may find that you have a need to test multiple URLs or repeat the same tests several times over the development of your application, and manually starting screenshot tests can take some time, depending on what you need to do each time.

If your plan supports automated testing, you can use our API to start a screenshot test.  In addition, if you are comfortable using Ruby, you can use the cbt_ruby gem to make the API interactions easier.

Installing cbt_ruby

cbt_ruby is a standard Ruby gem, available through RubyGems.org, so it can be installed using
gem install cbt_ruby

Using cbt_ruby

Let’s start out with an example of how to use cbt_ruby.

require 'cbt_ruby'
# create client
client = CBTRUBY::CbtClient.new("you@yourDomain.com", "yourActualAuthKey")


#create browser objects
browser = CBTRUBY::CbtClient::Browser.new(browser:"Chrome", version:"57", platform:"Win10")

mobileBrowser = CBTRUBY::CbtClient::Browser.new(browser:"MblChrome", version:"70", platform:"Nexus6P-And70")

#create a new browser list
browsers = CBTRUBY::CbtClient::Browsers.new

#add our browser to the list
browsers.add(browser)
browsers.add(mobileBrowser)

screenshot = CBTRUBY::CbtClient::ScreenshotTest.new(client: client, params:{"url":"http://google.com", browsers: browsers})

In the above example, I take the following steps to start the test:

  1. Import the cbt_ruby gem
  2. Create a new CbtClient object, giving it my CBT username and authkey (don’t know your authkey?  You can find it under Manage Account)
  3. Create a new Browser object.  This will take browser, version, platform, and resolution parameters, and create the object.
  4. Create a new Browsers object.  Note that this is plural.  This is basically a specialized list that the cbt_ruby gem knows how to handle in special ways to make it easier to work with.
  5. Add the browser we created to our browsers object.
  6. Create a ScreenshotTest.  We pass in the client object, and a set of parameters.  The only required parameters are “url” and a way to know what browsers are to be tested (browsers, or browser_list_name).

Other Uses

Using the CSV parser

cbt_ruby includes a CSV to Browsers-object parser.  Instead of creating browser objects in the script and adding them one at a time, you can use a CSV file, with browser, version, platform, and resolution headers (platform and resolution are optional) to create a larger or more accessible test setup.

browsers = CBTRUBY::csv_to_browsers(filename: "example.csv")

The CSV parser will return a Browsers object populated with the browsers specified in the CSV.

Looping through URLs

By putting the ScreenshotTest inside an iterator (such as each) you can easily cycle through a list of URLs.  By default, cbt_ruby will block execution of the next test until the current test has completed on CrossBrowserTesting.


urls = ["http://google.com", "http://reddit.com", "http://news.ycombinator.com"]
urls.each do |page|
screenshot = CBTRUBY::CbtClient::ScreenshotTest.new(client: client, params:{"url":page, browsers: browsers})
end

More information

cbt_ruby is available on GitHub , along with more documentation.

Related

All Topics

  • Billing and Plans
  • Frequently Asked Questions
  • Integrations
  • Live Testing
  • Local Connection
  • Screenshots
  • Selenium Testing
  • Teams and Admins
  • Team Guides



Think you need a human?

Our customer success team will be glad to help you with your question.

Contact Support

Still need a free trial?

Try CrossBrowserTesting free for 7 days and see how we make testing easier.

Start Today

Want the latest tips?

Enter your email and we’ll send you tutorials and browser testing strategies right to your inbox.


Product

  • Live Testing
  • Automated Testing
  • Visual Testing
  • Local Testing
  • Integrations

Resources

  • Browsers & Devices
  • Blog
  • Webinars
  • Security
  • ROI Calculator

Support

  • Help Center
  • CBT Community
  • API Docs
  • Automation Docs
  • Schedule A Demo
  • Enterprise Request

Company

    • About Us
    • Contact Us
    • Careers
    • Terms of Use
    • 1-888-927-6973

© 2019 CrossBrowserTesting.com, LLC. All rights reserved.