Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run headless browser system tests in Rails 5.1?

The documentation for Rails 5.1 system tests is a bit sparse. I'm unable to get headless tests that execute javascript running. I think one approach entails installing and running xvf. But this is more manual setup than I'm used to when running capybara in other versions of rails.

What's the most straightforward way to achieve this?

like image 645
John Bachir Avatar asked May 05 '17 20:05

John Bachir


People also ask

How do I run a test in headless mode?

Test Studio currently supports the headless mode for Chrome and Edge Chromium browsers. You can execute any existing test in your automation project in headless mode by selecting that type of browser.

Which of the following is used for headless browser testing?

Selenium It supports various browsers that run on different operating systems. Selenium web driver delivers enhanced support to dynamic web pages, and using Selenium headless can deliver great results. Moreover, you can use either Headless Chrome or Headless Firefox to execute the headless browser Selenium.

Can we use headless browser in cross browser testing?

Headless Browser Testing is a process of running the browser tests without the type of browser UI or GUI. In headless browser testing, to conduct cross-browser testing the tester can run test cases accurately and successfully without requiring the browser on which application needs to be tested.

What is headless browser testing?

Headless testing is when you run a UI-based browser test without showing the browser UI. It's running a test or running a script against a browser, but without the browser, UI starts up.


1 Answers

In Rails 5.1 system tests the driver used is set by the driven_by call in ApplicationSystemTestCase (test/application_system_test_case.rb). Assuming you have registered your capybara-webkit driver as 'webkit' you should be able to do

driven_by :webkit

Another potential option if you use Chrome 59+ on linux/mac is to use headless chrome

Capybara.register_driver :headless_chrome do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => ['headless'])
end

and then in your test case class

driven_by :headless_chrome

That gives you a headless version of chrome so none of the issues of capybara-webkit/poltergeist not supporting current web standards. Unfortunately currently chromedriver has issues with JS system modals (alert, confirm, prompt - workaround in capybara master branch) and hangs if you attempt to close windows during your tests. Hopefully those 2 issues will be fixed soon.

Also note that rails 5.1 should have removed the need for database_cleaner for most peoples testing since it already handles sharing of a single database connection between multiple threads in test mode.

like image 173
Thomas Walpole Avatar answered Nov 01 '22 21:11

Thomas Walpole