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 Chromium.org 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"
  • "WARNING"
  • "INFO"
  • "CONFIG"
  • "FINE"
  • "FINER"
  • "FINEST"
  • "ALL"
In Ruby.Properties, I added the properties and values:

env/default/ruby.properties:
CHROME_LOGGING = WARNING
CHROMEDRIVER_LOGGING = SEVERE

... 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. 

spec_helper.rb
 CAPABILITIES = Selenium::WebDriver::Remote::Capabilities.chrome(  
  loggingPrefs: {  
   browser: ENV['CHROME_LOGGING'], # Capture JavaScript errors  
   driver: ENV['CHROMEDRIVER_LOGGING'] # Capture WebDriver errors  
  }  
 )  
   
 before_suite do  
  Capybara.register_driver(:chrome) do |app|  
   ...  
   ...  
   
  Capybara::Selenium::Driver.new(app, browser: :chrome,  
                    desired_capabilities: CAPABILITIES,  
                    options: options)  
  end  

... What is before_suite? Gauge has before_suite and after_suite as an execution hook: https://docs.gauge.org/language.html#execution-hooks

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  
  end  
   
  open('logs/chromedriver.log', 'w') do |f|  
   f << driver_errors  
  end  
 end  


Run Tests, Examine Logs

Let's run our test suite again, and see what we find:
  • bundle exec gauge run specs -v
logs/chromedriver.log:
logs/chrome.log:
[#<Selenium::WebDriver::LogEntry:0x00007f8b121610a8 @level="SEVERE", @timestamp=1545326365292, @message="https://the-internet.herokuapp.com/favicon.ico - 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

18 comments:

saivenkat said...

Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.
Project Management Apps

Elizabeth Johanson said...

I really appreciate you for providing such informative posts which I can firmly use for the PhD dissertation proposal help UK, most students will be able to take this for guidance.

Nick Hunter said...

Well, I heard about chromium for the first time through this post. Earlier I only know about chrome. After completing my academic projects using coursework writing services, I will try this logging method.

thisisemliyblunt said...

It was an amazing purchase, quick delivery & got my item in time yumove discount code So happy with the product.

Michael Alex said...

Looking to Buy Argumentative Essay turnout to Master Essay Writers for all essay services at an affordable price.

thisisemliyblunt said...

The track down all relevant info and agreements on the site difference between llp and ltd.

John said...

These automotive features have made our lives so easy or I would say that every technical invention has brought quite a relief to us. One such technical invention which has benefited me a lot is the ability to buy the Best Assignment Writing Help through online platforms. I thank the innovator of this invention.

ireland assignment help said...

It is great to see this amazing article. I am glad to read this article and get amazing stuff from this. thesis writing help

Zayn Khaled said...

Namaz is the essential part of a Muslim's life and it is important to keep track of the prayer times in Lucknow. The Namaz Time in Lucknow begins with the Fajr prayer at 4:43 am, followed by the Dhuhr prayer at 12:17 pm, the Asr prayer at 3:48 pm, the Maghrib prayer at 6:24 pm and the Isha prayer at 8:03 pm. It is important to note that the times may vary slightly according to the season and weather changes. It is also important to follow the local rules and regulations when performing your namaz. May Allah grant us the strength and resources to perform our namaz regularly and with sincerity.

James Abe said...

Thanks for sharing this post.
Get expert help with Management Case Studies. Seek the best Management Case Study Help at a budget-friendly price. Assignmenttask.com provides you with 100% plagiarism free content. Our professional writers deliver assignment papers on time. We provide services such as, free Management assignment samples, editing & proofreading etc. To know more, visit us!

Malaysia Translators said...

Nice post
Malaysiatranslators.com provides professional divorce certificate translation services in Malaysia. Whether you need to translate your divorce certificate from Malay to English or from any other language, the translators can accurately translate your document and ensure it meets the legal requirements of the Malaysian authorities.

Ziya said...

I enjoy all of your posts. You did some excellent stuff. I appreciate the information you offer; it was quite helpful. I anticipate receiving several more entries from you. Blog is quite fascinating. If you're looking for Customized ERP Software then please visit customer relationship management in erp

Greece Assignment help said...

Awesome blog
Seeking professional dissertation Writer Greece? Look no further than GreeceAssignmentHelp.com. Our skilled writers provide custom dissertation writing help in Greece, delivering impeccable results. Avail yourself of our dissertation proofreading and editing services Greece for a flawless research document.

kalusnickalus1 said...

Juvenile Reckless Driving Virginia Reckless driving virginia is a serious offense.

Free Embroidery Digitizing Online said...

Integrating Chrome and ChromeDriver logging capabilities into the fundamental Capybara-Gauge system is a strategic move towards enhancing the debugging and monitoring aspects of web automation. This addition empowers developers to delve deeper into the intricacies of their test environments, providing valuable insights through detailed logs. By incorporating Chrome and ChromeDriver logging, developers gain a comprehensive view of the test execution process, enabling more efficient troubleshooting and optimization. This robust feature exemplifies the commitment to refining the Capybara-Gauge experience, ensuring that developers can navigate the complexities of web automation with precision and ease.

Anonymous said...

I have read your blog and also seen video on youtube. Its amazing I really like it very much. Please share more this type videos with us.
Abogado de DUI Fairfax VA

johnsy said...


Looking for a NJ Indian divorce attorney? Speak with [Lawyer's Name] for committed legal assistance. [Lawyer's Name] has experience in Indian family law and can help you with the procedure. Speak on [Phone Number].
Indian Divorce Lawyer NJ

marshtravis said...

Protective orders in Virginia can be issued for a maximum of two years. On the other hand, they may be extended for an additional year in situations involving abuse within the family. If a protection order is broken, criminal proceedings may follow.
how long does a protective order last in virginia