Read more

Cucumber Webrat steps

Lexy
August 25, 2010Software engineer at makandra GmbH

Most of these will not work in newer projects because these use the Capybara/Rack::Test combo in lieu of Webrat.

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Find input fields

Then /^there should be a "([^"]+)" field$/ do |name|
  lambda { webrat.current_scope.send(:locate_field, name) }.should_not raise_error(Webrat::NotFoundError)
end

Then /^there should be no "([^"]+)" field$/ do |name|
  lambda { webrat.current_scope.send(:locate_field, name) }.should raise_error(Webrat::NotFoundError)
end

Find html content

Then /^I should see "([^\"]*)" in the HTML$/ do |text|
  response.body.should include(text)
end

Then /^I should not see "([^\"]*)" in the HTML$/ do |text|
  response.body.should_not include(text)
end

Find html content within a selector

Then /^I should see "([^\"]*)" in the HTML within "([^\"]*)"$/ do |text, selector|
  have_selector(selector).matches?(response) do |content|
    content.to_html.should include(text)
  end
end

Then /^I should not see "([^\"]*)" in the HTML within "([^\"]*)"$/ do |text, selector|
  have_selector(selector).matches?(response) do |content|
    content.to_html.should_not include(text)
  end
end

Match regular expressions

Then /^I should see \/([^\/]*)\/ in the HTML$/ do |text|
  response.body.should =~ /#{text}/
end

Then /^I should see \/([^\/]*)\/m in the HTML$/ do |text|
  response.body.should =~ /#{text}/m
end

Find tables

This step looks for tables by its name as table#name, table.name or #name_table.

Extra columns are ignored, column order does not matter, whereas row order does.

Then /^I should see the following ([\w]+) table:?$/ do |name, expected_table|
  name = name.underscore
  table_element = begin
    element_at("table\##{name}")
  rescue Webrat::NotFoundError
    begin
      element_at("table.#{name}")
    rescue
      element_at("\##{name}_table")
    end
  end
  table = table_element.to_table
  expected_table = expected_table.raw

  table.size.should == expected_table.size
  table.each.with_index do |row, index|
    expected_row = expected_table[index]
    expected_row.each do |expected_column|
      row.any?{ |column| column.include?(expected_column) }.should be_true
    end
  end
end

Call it like

Then I should see the following users table:
  | Name  | Is Admin? |
  | Alice | Yes       |
  | Bob   | No        |

Find tables (Mk. ii)

Looks for table#name. Expects columns and rows to be in the given order, but ignores extra rows and columns.
Only looks at one row at once, so does not check, that values given for one column actually appear in the same column.

Then /^I should see the following rows in the ([\w]+) table:?$/ do |name, expected_table|
  document = Webrat::XML.html_document(response)

  rows = document.xpath("//table[@id='#{name}']//tr").collect { |row| row.xpath('.//td|.//th') }

  expected_table = expected_table.raw

  expected_table.all? do |expected_row|
    first_row = rows.find_index do |row|
      expected_row.all? do |expected_column|
        first_column = row.find_index do |column|
          Webrat::XML.inner_text(column).include? expected_column.strip
        end
        if first_column.nil?
          false
        else
          row = row[(first_column + 1)..-1]
          true
        end
      end
    end
    if first_row.nil?
      false
    else
      rows = rows[(first_row + 1)..-1]
      true
    end
  end.should be_true
end
Lexy
August 25, 2010Software engineer at makandra GmbH
Posted by Lexy to makandra dev (2010-08-25 16:11)