December 21, 2018

Basic Capybara-Gauge: Add Chrome and ChromeDriver logging capabilities

This is the sixth part of a series of blog posts. Care to go back to the beginning

Want to view JavaScript errors in Chrome? Or see if your Selenium WebDriver instance is throwing any errors? Check out the Logging Preference Capabilities from the Chromium project.

With this article, we will demonstrate how to get these logs set up in your Capybara-Ruby project, and what to add in the spec_helper.rb file we created.

What is Chromium?

"Chromium is an open-source browser project that aims to build a safer, faster, and more stable way for all Internet users to experience the web. This site contains design documents, architecture overviews, testing information, and more to help you learn to build and work with the Chromium source code".

What are Capabilities?

From Capabilities and Chrome Options, on the website:

"Capabilities are options that you can use to customize and configure a ChromeDriver session. This page documents all ChromeDriver supported capabilities and how to use them.

"The WebDriver language APIs provides ways to pass capabilities to ChromeDriver. The exact mechanism differs by the language, but most languages use one or both of the following mechanisms:

"Use the ChromeOptions class. This is supported by Java, Python, etc.
  • "Use the DesiredCapabilities class. This is supported by Python, Ruby, etc. While it is also available in Java, its usage in Java is deprecated.
  • "Using the ChromeOptions class
"You can create an instance of ChromeOptions, which has convenient methods for setting ChromeDriver-specific capabilities. You can then pass the ChromeOptions object into the ChromeDriver constructor"

Set Log Levels in Ruby Properties

For logs, we can choose from six different levels of logging:
  • "OFF"
  • "SEVERE"
  • "INFO"
  • "CONFIG"
  • "FINE"
  • "FINER"
  • "FINEST"
  • "ALL"
In Ruby.Properties, I added the properties and values:


... If there is event the slightest thing wrong displaying in the Chrome browser while my app is running, I want to know, Warnings and all!

... If there is anything with WebDriver, I only want to know the problems tagged with Severe.

Add Capabilities

Creating Capabilities and adding them to the browser in Ruby is easy (once you know how).

Instead of hardcoding the logging preferences for the browser or the webdriver, I will use what I have declared in my Ruby properties. 

  loggingPrefs: {  
   browser: ENV['CHROME_LOGGING'], # Capture JavaScript errors  
   driver: ENV['CHROMEDRIVER_LOGGING'] # Capture WebDriver errors  
 before_suite do  
  Capybara.register_driver(:chrome) do |app|  
   ..., browser: :chrome,  
                    desired_capabilities: CAPABILITIES,  
                    options: options)  

... What is before_suite? Gauge has before_suite and after_suite as an execution hook:

These commands will set up the logging level for every instance of Chrome that you spin up, headless or otherwise.

If there are any JavaScript errors displaying in Chrome, or any problems in ChromeDriver, you can take a look at the logs to diagnose what is wrong.

But what do you do with the information afterwards?

Save The Logs!

After the test suite has run, we can take the browser and driver logs, put that in variables called browser_errors and driver_errors, then create and open two files, chrome.log and chromedriver.log, saving the data to the logs directory.

Here is what I hacked together:
 after_suite do  
  browser_errors = page.driver.browser.manage.logs.get(:browser)  
  driver_errors = page.driver.browser.manage.logs.get(:driver)  
  open('logs/chrome.log', 'w') do |f|  
   f << browser_errors  
  open('logs/chromedriver.log', 'w') do |f|  
   f << driver_errors  

Run Tests, Examine Logs

Let's run our test suite again, and see what we find:
  • bundle exec gauge run specs -v
[#<Selenium::WebDriver::LogEntry:0x00007f8b121610a8 @level="SEVERE", @timestamp=1545326365292, @message=" - Failed to load resource: the server responded with a status of 404 (Not Found)">]

... Hrm... It looks like the Favorite icon, the icon that shows up when you Favorite or Bookmark a page, is missing. Someone should tell Dave Haeffner, the person who created The-Internet. 

Last, but not least, we will be adding to our project the code review tool, Rubocop.

Happy Testing!

-T.J. Maher
Sr. QA Engineer, Software Engineer in Test
Meetup Organizer, Ministry of Testing - Boston

Twitter | YouTubeLinkedIn | Articles

1 comment:

Kevin Dellinger said...

If you desire to obtain a good deal from this paragraph, then you have to apply these methods to your won blog. Look at here: How To Lock Files In 15 Minutes And Still Look Your Best