## Running Tests with Applitools

### Prerequisites

1. A [free Applitools account](/content/users/register/index.html) and Applitools API KEY

Tip: Unsure how to find your API key?
   Read [Retrieving your API key](/content/docs/eyes/getting-started/retrieve-api-key/index.html) to find your API key.

2. [Ruby](https://www.ruby-lang.org/en/documentation/installation/)

3. [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`.

4. [Google Chrome Browser](https://www.google.com/chrome/)

5. [ChromeDriver](https://chromedriver.chromium.org/eyes/getting-started)

ChromeDriver must be installed and in your `PATH`. Below are some resources from the internet that'll help you.
   - [Install on Mac](https://www.kenst.com/2015/03/installing-chromedriver-on-mac-osx/)
   - [Install on windows](https://www.youtube.com/watch?v=dz59GsdvUF8)
   
   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
   ```text
   git clone https://github.com/applitools/tutorial-selenium-ruby-ultrafastgrid.git
   cd tutorial-selenium-ruby-ultrafastgrid
   ```
   _Note: you can alternatively download the project as a Zip file and extract it_

2. Install the dependencies
   ```text
   gem install bundler && bundle install
   ```

3. Run the example test
   ```text
   APPLITOOLS_API_KEY="[Your API Key]" bundle exec ruby simple_test_script.rb
   ```
   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
   ```sh
   gem install eyes_selenium
   ```

2. Add an example test
   ```ruby
   require 'eyes_selenium'

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

# Create a new chrome web driver
   options = Selenium::WebDriver::Chrome::Options.new
   options.add_argument('--headless') if ENV['CI'] == 'true'
   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|
     conf.api_key = ENV['APPLITOOLS_API_KEY']
     conf.batch = Applitools::BatchInfo.new("Demo Batch - Selenium Ruby - Ultrafast")
     conf.app_name = 'Demo App - Selenium Ruby - Ultrafast'
     conf.test_name = 'Smoke Test - Selenium Ruby - Ultrafast'
     conf.viewport_size = Applitools::RectangleSize.new(800, 600)
     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)
     conf.add_device_emulation(Devices::IPhoneX, Orientation::PORTRAIT)
         .add_device_emulation(Devices::Pixel2, Orientation::PORTRAIT)
   end

begin
     driver = eyes.open(driver: web_driver)
     driver.get('https://demo.applitools.com/index.html')
     eyes.check('Login page', Applitools::Selenium::Target.window.fully)
     driver.find_element(:id, 'log-in').click
     eyes.check('App Page', Applitools::Selenium::Target.window.fully)
     eyes.close
   ensure
     driver.quit
     eyes.abort_async
     results = visual_grid_runner.get_all_test_results
     puts results
   end
   ```

## Option 2 - Run Locally

1. Clone or download the repository and navigate to that folder
   ```text
   git clone https://github.com/applitools/tutorial-selenium-ruby-basic.git
   cd tutorial-selenium-ruby-basic
   ```
   _Note: you can alternatively download the project as a Zip file and extract it_

2. Install the dependencies
   ```text
   gem install bundler && bundle install
   ```

### Adding Applitools Eyes to an Existing Project

1. Install Applitools Eyes dependencies
   ```sh
   gem install eyes_selenium
   ```

2. Add an example test
   ```ruby
   require 'eyes_selenium'

options = Selenium::WebDriver::Chrome::Options.new
   options.add_argument('--headless') if ENV['CI'] == 'true'

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 - Selenium Ruby - Classic")

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

begin
     driver = eyes.open(driver: web_driver)
     driver.get('https://demo.applitools.com')
     eyes.check(name: 'Login window', target: Applitools::Selenium::Target.window.fully)
     driver.find_element(:id, 'log-in').click
     eyes.check(name: 'App window', target: Applitools::Selenium::Target.window.fully)
     eyes.close
   ensure
     driver.quit
     eyes.abort_if_not_closed
     puts runner.get_all_test_results
   end
   ```

## Taking the next steps with Applitools Eyes

Congratulations on completing this quickstart! There's still so much to learn about visual testing with Applitools, but you're off to a great start.

- [↔️ Setting match levels for visual checkpoints](/content/docs/eyes/concepts/best-practices/match-levels/index.html)
- [Organizing Tests Into Batches](/content/docs/eyes/concepts/best-practices/batching/index.html)
- [Request a demo](/content/contact/product-demo-request/index.html)
