My setup for configuring a Rails app with RSpec Feature tests that uses Capabara to test on a browserstack remote browser.
The browserstack docs only show either an RSpec or a Capabara setup.
What I wanted:
- In development run my tests quickly with poltergiest.
- Run nightly builds on CI to test against different web browsers using browserstack.
- Have the option to visually run my tests with selenium with Chrome, Firefox etc.
What browsers are used in the test can now be changed when using the selenium driver. The test suite can be changed by setting optional Environment variables TEST_BROWSER.
If TEST_BROWSER is set it will determin whether to use selenium or poltergiest.
The default is to run poltergiest.
TEST_BROWSER=chrome rspec spec/some_spec.rb
TEST_BROWSER=headless_chrome rspec spec/some_spec.rb
TEST_BROWSER=firefox guard
rspec spec/some_spec.rb # Defaults
Each browser requires its own driver installed.
-
Firefox requires geckodriver
brew install geckodriver
-
Chrome Requires ChromeDriver
This is setup by chromedriver-helper in the Gemfile!
#TODO: Circleci will have to pre install these!
Currently we can run our tests in three modes:
-
Headless :
-
poltergiest ( Default and fastest )
rspec
-
selenium with headless chrome browser using ChromeDriver ( Not fully compatible yet. )
TEST_BROWSER=headless_chrome rspec
-
-
In Browser:
-
selenium driver with chrome browser
TEST_BROWSER=chrome rspec
TEST_BROWSER=chrome guard
-
selemium driver with firefox browser
TEST_BROWSER=firefox rspec
TEST_BROWSER=firefox guard
-
-
On a remote browser
-
on browserstack
TEST_BROWSER=browserstack rspec
rake browserstack:local
-
- Only on poltergiest do all the tests pass.
- headless chrome cannot do
save_and_open_screenshot
- chrome cannot
.trigger('click')
page.driver.network_traffic
only works in poltergeist.
We can use browserstack to run our test against any browser. Edge and Firefox are free to use on browserstack.
- environment variables need to be set for it to run.
BROWSERSTACK_USERNAME
andBROWSERSTACK_ACCESS_KEY
. Get these on the browserstack settings page. - The browser used can be set in the browserstack config files.
- It is set up to run against the latest version of whatever browser selected.
- It can be configured to use a specific bowser by setting “browser_version”: “48”
Allows you to run the server locally on your machine and run all the feature tests on the remote browsers on browserstacks.
rake browserstack:local
The rake task sets a ENV['TEST_BROWSER'] = 'browserstack'
which triggers the switchover in rails_helper.rb
to run the tests on browserstack.
- We have a window of 2 hours to finish our tests before our session on browserstack ends.
- It is REALLY slow to test on a remote browser. Only makes sense to run on a nightly build.
Steps:
- Start the rails sever locally.
- Added the Chrome exension for browserstack.
- Open any web browser on browserstack and turn on local server testing.
- Navigate to http://localhost:3000 on the browserstack browser.
- Browser within a browser inception.
- Great for clicking around the development site in any browser.
Thank you!