Cucumber + Webrat + Selenium Guide

Cucumber + Webrat + Selenium guide

I'm using Selenium with rspec on my project and generate code from a custom formatter for Selenium IDE.

There is many selenium for rails but i success using Selenium-RC http://seleniumhq.org/download/ , so download to your pc.

Here are my steps:

  1. Unzip and run> java -jar selenium-server.jar
  2. Open selenium-client-ruby, read the doc, follow it you will get success!
  3. gem install rspec, rspec-rails version 1.2.6 (it not, you need to comment version restrict of selenium-client source code)
  4. gem install selenium-client
  5. Open Selenium-IDE (Firefox of course), Open Options -> Options -> Formats
  6. Click Add, and paste this code in http://www.techdarkside.com/rspec_export.txt

Now, You just export spec to your spec folder for me, I use spec/features/xxxx_spec.rb see code below.

Very similar approach can find at here

For webrat+cucumber, the latest Rspec book will give all you need. (They don't have selenium + cucumber chapter finish yet)

example

 require 'rubygems'
gem "rspec", "=1.2.6"
gem "selenium-client", ">=1.2.15"
require "selenium/client"
require "selenium/rspec/spec_helper"

describe "Google Search" do
attr_reader :selenium_driver
alias :page :selenium_driver

before(:all) do
@selenium_driver = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*firefox",
:url => "http://www.google.com",
:timeout_in_second => 60
end

before(:each) do
selenium_driver.start_new_browser_session
end

# The system capture need to happen BEFORE closing the Selenium session
append_after(:each) do
@selenium_driver.close_current_browser_session
end

it "can find Selenium" do
page.open "/"
page.title.should eql("Google")
page.type "q", "Selenium seleniumhq"
page.click "btnG", :wait_for => :page
page.value("q").should eql("Selenium seleniumhq")
page.text?("seleniumhq.org").should be_true
page.title.should eql("Selenium seleniumhq - Google Search")
page.text?("seleniumhq.org").should be_true
page.element?("link=Cached").should be_true
end

end

Cucumber + webrat + selenium, how do I ignore hidden text?

For cases as these I use those two custom steps:

Then /^the element matched by "([^\"]*)" should be visible$/ do |locator|
selenium.should be_visible(locator)
end

Then /^the element matched by "([^\"]*)" should not be visible$/ do |locator|
selenium.should_not be_visible(locator)
end

Put those into a Ruby file under step_definitions/ directory.

So, in your case, instead of Then I should see "something" use Then the element matched by "something" should be visible.

How to use Page Object pattern with Cucumber and Webrat / Selenium?

Turns out the answer is:

class MyPage < BasePage
def visit
@world.visit "/"
end

end

class BasePage
def initialize(world)
@world = world
end
end

And then in a step definition:

Given /I am awesome/ do
page = MyPage.new(self)
page.visit
end

Cucumber/Webrat: Provide params to GET request

Take a look in ./features/support/paths.rb. This contains the mappings that Webrat will use to match the paths in your features. For example, /the home page/ maps to the string '/'. You can add your own mappings and use a regexp to pull out any needed parameters. Eg. (from the standard paths.rb):

when /^(.*)'s profile page$/i
user_profile_path(User.find_by_login($1))

cucumber webrat click on second delete link

For generating correct delete links rails uses javascript. So you get a DELETE request instead of GET which a normal link would produce. Since webrat doesn't handle javascript there is no way this can work. Check out Capybara as alternative to webrat or webrat with selenium.

select box in cucumber + webrat

That's not code, those are cucumber feature statements. Cucumber will translate those into step definitions, which in turn can contain code that will let you test the thing being described.

You probably have something like web_steps.rb that provides a set of pre-built statements like this, but you should not use them. You should be describing the business behavior of the application you are building, not the low-level UI details. Instead you should write something like this

Given I am on the registration page
When I sign up
Then I should be logged in automatically
And I should see a message stating "Thank you for signing up!"

This gives you an example of what you want to happen when someone signs up (They are logged in automatically and they are thanked for signing up). They way you have it now, you will have to change your specification to match the UI whenever you want to change the name of a field, and the specification should never change unless you are changing your business needs.

So to answer your more technical question, the step definition would look something like this, based on webrat's documentation

fill_in 'email', :with => '123@example.com'
fill_in 'name', :with => 'username'
select 'free account'
click_button 'Signup'


Related Topics



Leave a reply



Submit