I use Cucumber and Capybara for testing my Rails application, it’s easier to write tests using Cucumber tool, like an example mentioned below:
Most of the time on CI (Continuous Integration) server we execute our tests in headless mode, but recently I had to run my tests across many browsers like Firefox, Chrome, Internet Explorer 8 and 9 and that’s when the need to run these test across multiple browser arise.
You can also configure your capybara to run on remote hosts, that means from your machine you can connect to browser installed on some other machine which is on same network, It’s really simple to configure on Rails application.
Install Cucumber and Capybara
To configure Cucumber and Capybara in your Rails application, add these dependencies to your Gemfile and then run bundle install:
If you don’t define the version, it will pick the most recent one when you run “bundle update”, Once these gems are installed then run the following command to generate your cucumber config:
Run in default headless mode
Now, lets use the above mentioned example of Cucumber feature and get it working (assuming you will write step definition, use it for reference
). By default test run’s in Capybara’s headless browser, so before we test on real browser, lets get this test working on default one.
To run the test, type this command from your web folder.
Now, when feature is passing in headless mode, lets try to do some configuration changes to run tests in various browsers.
Run tests on remote Internet Explorer (Windows Machine)
Use any windows machine on your network to set up.
Selenium requires Java so you should have it on your machine, you can download it from here. Open new command prompt (Start..Run…cmd) and run the java command to see it has installed successfully, if not then set the environment variable to get it working.
Now from Selenium page get the latest selenium-server-standalone.jar and from your command prompt you should be able to navigate to where the file is downloaded and run:
It should show some logs on command prompt and let you know its running.
It’s time to hook Capybara to Windows
To get your features running on that box go back to the Rails application, open up features/support/env.rb and add this snippet after capybara.default_selector line
Make sure you change the ip address in the snippet to the correct one and now when you type:
You should see IE browser launched on your Window’s box and test should pass.
Run tests on Firefox browser
To get your features running on your local Firefox instance go back to the Rails application, open up features/support/env.rb and change the default driver to be selenium instead of capybara.
Now type the same command and your tests should run on firefox browser.
Run tests on Chrome browser
To get your features running on your local Chrome browser go back to the Rails application, open up features/support/env.rb and change the default driver to be selenium_chrome instead of capybara and add following snippet.
Also on your machine you will have to install chromedriver
before you run the test against chrome browser
Now type the same command and your tests should run on chrome browser.
I am yet to try it against Safari and will keep this post updated, let me know if you had any issues.
References and examples picked from: