Transcripted Summary

Let's say we're debugging something, and we want to actually see what's happening in the browser.

In our advanced topics folder of TAU Capybara, let's go into the Chrome Logging folder and run a test.



The Chrome browser opened up. It went to the homepage and then checked the title, subtitle, current URL, et cetera.

What if a human being wasn't looking at the test? Would we really need to have the browser open up or could we just have the test run and checking which pass, and which fail?

Running the test that way, without the browser user interface is called a headless test.

Capybara has a method that's built in, that allows you to do that. With the Capybara default_driver property, let's change it from Selenium Chrome to Selenium Chrome Headless.


# Capybara.default_driver = :selenium_chrome
Capybara.default_driver = :selenium_chrome_headless

Now when we run our test again, all the test scenarios are executed without the browser actually opening up.



As you can see, this finished in 3.04 seconds, while with the browser-based test, it took 4.56 seconds.

Speed Matters with Testing

If you're running hundreds of tests, these seconds matter. With the larger automation suites, you need to have the tests be as fast as possible.

The next trick we are going to look at it with Chrome is logging.

Here we have our spec_helper file.


# Example Test with Capybara + RSpec – ChromeDriver Logging: spec_helper.rb

require 'bundler'
require 'capybara/dsl'
require 'capybara/rspec'

Bundler.setup(:default)
Bundler.require

Capybara.default_driver = :selenium_chrome

# Capybara.default_driver = :selenium_chrome_headless
Capybara.app_host = 'https://the-internet.herokuapp.com'
Capybara.default_max_wait_time = 10

RSpec.configure do |config|
  config.formatter = :documentation

  config.before(:suite) do
    Capybara.register_driver(:chrome) do |app|
      options = Selenium::WebDriver::Chrome::Options.new
      options.add_argument('--window-size=1240,1400')

      # Loggers Values: "OFF", "SEVERE", "WARNING", "INFO", "CONFIG", "FINE",
      #   "FINER", "FINEST", "ALL"
      # https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities

    CAPABILITIES = Selenium::WebDriver::Remote::Capabilities.chrome(
      loggingPrefs: {
        browser: 'INFO', # Capture JavaScript errors in Browser
        driver: 'INFO' # Capture WebDriver severe errors
      }
    )

    Capybara::Selenium::Driver.new(app, browser: :chrome,
                                    desired_capabilities: CAPABILITIES,
                                      options: options)
    end
  end

  config.after(:suite) do
    browser_errors = Capybara.page.driver.browser.manage.logs.get(:browser)
    driver_errors = Capybara.page.driver.browser.manage.logs.get(:driver)

    open('logs/chrome.log', 'w') do |f|
      f << browser_errors
    end

    open('logs/chromedriver.log', 'w') do |f|
      f << driver_errors

    end
  end
end

We're going to add to our configuration of RSpec.

What we're doing is configuring RSpec tests to run in Capybara with certain options and browser capabilities.

Before the test is run, we're going to register a driver, set up the options to be Chrome, and we're going to add the argument for window size.

We're going to be setting up capabilities for Chrome in order to get information from the logs.

Log Types

There are 2 types of logs: the browser-based log, which shows JavaScript errors in the browser and the driver log, which will capture any WebDriver errors. By default, they're off, but you can set them to catch any severe errors, any warnings, info which shows everything.

For the Chrome Logging project, we created a folder called “logs”.

After the test run, we're going to collect all the browser logs and put it in a variable called “browser_errors”, and we're going to collect all the driver logs, put it in a variable called “driver_errors”, and we're going to open a log file and shove both the browser errors into a file called logs/chrome.log and all the driver errors in logs/chromedriver.log.

Let's run the test again.



Under “logs”, we can see that there's 2 new files: chromedriver.log, which is empty, and chrome.log, which came up with a message with a level of severe.



According to this message, it seems that the-internet is supposed to have a favorite icon.

Something that shows when it's favorited in your browser, but this resource failed to load. Responded with a 404 error. I want you to really tell Dave Haeffner this.

In the next section, we will review a new browser that team Capybara has been writing called Apparition.



Resources



Quiz

The quiz for this chapter can be found in section 7.4

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR