Thursday, July 2, 2015

Rails, minitest and Selenium


Introduction

Assume we will follow TDD (huh?) so build testing environment first.

This article describe how to setup and run selenium with minitest in Rails 4.

Pre-request

Make sure you have installed Rails 4, see
Install Rails 4 in Ubuntu 14.04

Steps

Open Terminal (Ctrl + Alt + T)

execute
# create testing project
rails new SeleniumTest
# go into project folder
cd SeleniumTest

Then edit SeleniumTest/Gemfile, append
gem 'selenium-webdriver'
gem 'minitest-rails'

save Gemfile, go back to terminal and run
# install for selenium-webdriver and minitest-raills
bin/bundle install
# create first test case
bin/rails generate integration_test check_selenium_work

edit generated test case
test/integration/check_selenium_work_test.rb
as below
require "test_helper"
require "selenium-webdriver"

class CheckSeleniumWorkTest < ActionDispatch::IntegrationTest
  def test_sanity
    driver = Selenium::WebDriver.for :firefox
    driver.navigate.to "http://google.com"

    element = driver.find_element(:name, 'q')
    element.send_keys "Hello WebDriver!"
    element.submit

    puts driver.title

    # do not quit driver so we can see search result
    # driver.quit
  end
end

save test case, then run
rake test

in terminal,

you should see firefox navigate to google and
search "Hello WebDriver!"

References

https://code.google.com/p/selenium/wiki/RubyBindings
https://github.com/blowmage/minitest-rails

Sample Project

Rails Selenium Test

No comments:

Post a Comment