Selenium for Ruby Tutorial

Getting Started with Applitools

Want to learn more about the Applitools platform and how it works? First get started with one of the following:

Running Tests with Applitools

Prerequisites
  1. A free Applitools account and Applitools API KEY

Tip: Unsure how to set up your API key?

Learn how to get started at Setting Up

  1. Ruby https://www.ruby-lang.org/en/documentation/installation/

  2. Git https://git-scm.com​

Note: Installing `git` is optional

Installing git is used to clone the demo project from the Github repository. Instead of installing git, you can simply download the Zip file from the repository. Further, if you are Mac, you already have git.

  1. Google Chrome Browser https://www.google.com/chrome/

  2. ChromeDriver https://chromedriver.chromium.org/getting-started

ChromeDriver must be installed and in your `PATH`

Below are some resources from the internet that'll help you

On Mac, place the chromedriver executable in /usr/local/bin folder so Eclipse and IntelliJ can find it.

Option 1 - Run With The Ultrafast Grid
  1. Clone or download the repository and navigate to that folder
git clone https://github.com/applitools/tutorial-selenium-ruby-ultrafastgrid.git
cd tutorial-selenium-ruby-ultrafastgrid
1
2

Note: you can alternatively download the project as a Zip file and extract it

  1. Install the dependencies
gem install bundler && bundle install
1
  1. Run the example test
APPLITOOLS_API_KEY="[Your API Key]" bundle exec ruby simple_test_script.rb
1

This will first set your APPLITOOLS_API_KEY into the node process then run bundle exec.

Adding Applitools Eyes to an Existing Project

  1. Install Applitools Eyes dependencies
gem install eyes_selenium
1
  1. Add an example test
require 'eyes_selenium'

# Create a new chrome web driver
options = Selenium::WebDriver::Chrome::Options.new
web_driver = Selenium::WebDriver.for :chrome, options: options

# Create a runner with concurrency of 1
visual_grid_runner = Applitools::Selenium::VisualGridRunner.new(1)

# Create Eyes object with the runner, meaning it'll be a Visual Grid eyes.
eyes = Applitools::Selenium::Eyes.new(runner: visual_grid_runner)

# Initialize eyes Configuration
eyes.configure do |conf|
  #  You can get your api key from the Applitools dashboard
  conf.api_key = ENV['APPLITOOLS_API_KEY']
  # create a new batch info instance and set it to the configuration
  conf.batch = Applitools::BatchInfo.new("Demo Batch - Ultrafast - Selenium for Ruby")
  conf.app_name = 'Demo App - Ultrafast - Selenium for Ruby'
  conf.test_name = 'Smoke Test - Ultrafast - Selenium for Ruby'
  conf.viewport_size = Applitools::RectangleSize.new(800, 600)
  # Add browsers with different viewports
  conf.add_browser(800, 600, BrowserType::CHROME)
      .add_browser(700, 500, BrowserType::FIREFOX)
      .add_browser(800,600, BrowserType::SAFARI)
      .add_browser(1600,1200, BrowserType::IE_11)
      .add_browser(1024,768, BrowserType::EDGE_CHROMIUM)
  #  Add mobile emulation devices in Portrait mode
  conf.add_device_emulation(Devices::IPhoneX, Orientation::PORTRAIT)
      .add_device_emulation(Devices::Pixel2, Orientation::PORTRAIT)
end

 # ⭐️ Note to see visual bugs, run the test using the above URL for the 1st run.
 # but then change the above URL to https://demo.applitools.com/index_v2.html
 # (for the 2nd run)
begin

  # Call Open on eyes to initialize a test session
  driver = eyes.open(driver: web_driver)

  # Navigate to the url we want to test
  driver.get('https://demo.applitools.com/index.html')

  # check the login page with fluent api, see more info here
  # https://applitools.com/docs/topics/sdk/the-eyes-sdk-check-fluent-api.html
  eyes.check('Login page', Applitools::Selenium::Target.window.fully)

  # Click the 'Log In' button
  driver.find_element(:id, 'log-in').click

  # Check the app page
  eyes.check('App Page', Applitools::Selenium::Target.window.fully)

  # Call Close on eyes to let the server know it should display the results
  eyes.close
ensure
  # Close the browser
  driver.quit
  # If the test was aborted before eyes.close / eyes.close_async was called, ends the test as aborted.
  eyes.abort_async

  # we pass false to this method to suppress the exception that is thrown if we
  # find visual differences
  results = visual_grid_runner.get_all_test_results
  puts results
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Option 2 - Run Locally
  1. Clone or download the repository and navigate to that folder
git clone https://github.com/applitools/tutorial-selenium-ruby-basic.git
cd tutorial-selenium-ruby-basic
1
2

Note: you can alternatively download the project as a Zip file and extract it

  1. Install the dependencies
gem install bundler && bundle install
1
  1. Run the example test
APPLITOOLS_API_KEY="[Your API Key]" bundle exec ruby simple_test_script.rb
1

This will first set your APPLITOOLS_API_KEY into the node process then run bundle exec.

Adding Applitools Eyes to an Existing Project

  1. Install Applitools Eyes dependencies
gem install eyes_selenium
1
  1. Add an example test
require 'eyes_selenium'

options = Selenium::WebDriver::Chrome::Options.new

runner = Applitools::ClassicRunner.new
eyes = Applitools::Selenium::Eyes.new(runner: runner)
web_driver = Selenium::WebDriver.for :chrome, options: options

eyes.batch = Applitools::BatchInfo.new("Demo Batch - Classic - Ruby")

eyes.configure do |conf|
  conf.app_name = 'Demo App - Classic - Selenium for Ruby'
  conf.test_name = 'Smoke Test - Classic - Selenium for Ruby'
  conf.viewport_size = Applitools::RectangleSize.new(800, 600)
end

begin
  # Call Open on eyes to initialize a test session
  driver = eyes.open(driver: web_driver)

  # Navigate to the url we want to test
  driver.get('https://demo.applitools.com')

  # Note to see visual bugs, run the test using the above URL for the 1st run.
  # but then change the above URL to https://demo.applitools.com/index_v2.html (for the 2nd run)

  # check the login page
  eyes.check(name: 'Login window', target: Applitools::Selenium::Target.window.fully)

  # Click the 'Log In' button
  driver.find_element(:id, 'log-in').click

  # Check the app page
  eyes.check(name: 'App window', target: Applitools::Selenium::Target.window.fully)
  eyes.close
ensure
  # Close the browser
  driver.quit
  #  If the test was aborted before eyes.close / eyes.close_async was called, ends the test as aborted.
  eyes.abort_if_not_closed
  # Get and print all test results
  puts runner.get_all_test_results
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

More information

Resources




Terms & Conditions Privacy Policy GDPR© 2021 Applitools. All rights reserved.

Last Updated: 6/24/2021, 10:09:59 AM