Permalink
Browse files

fix example file webrat compatible steps

  • Loading branch information...
1 parent 6d9b40e commit a852e17153e5e90b21ee805904fa594017f12bf9 Sven Fuchs committed Feb 28, 2010
@@ -1,17 +1,15 @@
-# Sets up the Rails environment for Cucumber
ENV["RAILS_ENV"] ||= "cucumber"
-$: << File.expand_path(File.dirname(__FILE__) + "/../../vendor/plugins/steam/lib")
-require 'steam'
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
+require 'steam'
+require 'test/unit'
+# require 'rspec'
-browser = Steam::Browser::HtmlUnit.new
+Steam.config[:html_unit][:java_path] = 'path/to/your/htmlunit-2.6'
+
+browser = Steam::Browser.create
World do
Steam::Session::Rails.new(browser)
end
-Before do
- ActiveRecord::Base.send(:subclasses).each do |model|
- model.connection.execute("DELETE FROM #{model.table_name}")
- end
-end
+at_exit { browser.close }
@@ -1,16 +1,7 @@
-# IMPORTANT: This file was generated by Cucumber 0.4.4
-# It has been adjusted in order to seamlessly support steam when steam is loaded.
-
-require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
-
-# Commonly used webrat steps
-# http://github.com/brynary/webrat
-
Given /^(?:|I )am on (.+)$/ do |page_name|
visit path_to(page_name)
- if defined?(Steam)
- response.status.should == 200
- end
+ status = response.status
+ status.respond_to?(:should) ? status.should == 200 : assert_equal(200, status)
end
When /^(?:|I )go to (.+)$/ do |page_name|
@@ -22,19 +13,15 @@
end
When /^(?:|I )click on "([^\"]*)"$/ do |text|
- if defined?(Steam)
- click_on(text)
- else
- raise '"When I click on" is only supported under Steam'
- end
+ click_on(text)
end
When /^(?:|I )follow "([^\"]*)"$/ do |link|
click_link(link)
end
When /^(?:|I )follow "([^\"]*)" within "([^\"]*)"$/ do |link, parent|
- click_link_within(parent, link)
+ click_link(link, :within => parent)
end
When /^(?:|I )fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
@@ -54,7 +41,7 @@
# | Wants Email? | |
When /^(?:|I )fill in the following:$/ do |fields|
fields.rows_hash.each do |name, value|
- reformat_date!(value) if name =~ /_date/
+ # reformat_date!(value) if name =~ /_date/
When %{I fill in "#{name}" with "#{value}"}
end
end
@@ -78,8 +65,8 @@
# The following steps would fill out the form:
# When I select "November 23, 2004 11:20" as the "Preferred" date and time
# And I select "November 25, 2004 10:30" as the "Alternative" date and time
-When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date and time$/ do |datetime, datetime_label|
- select_datetime(datetime, :from => datetime_label)
+When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date and time$/ do |datetime, select|
+ select_datetime(datetime, :from => select)
end
# Use this step in conjunction with Rail's time_select helper. For example:
@@ -100,15 +87,15 @@
# Use this step in conjunction with Rail's date_select helper. For example:
# When I select "February 20, 1981" as the date
When /^(?:|I )select "([^\"]*)" as the date$/ do |date|
- reformat_date!(date)
+ # reformat_date!(date)
select_date(date)
end
# Use this step when using multiple date_select helpers on one page or
# you want to specify the name of the date on the form. For example:
# When I select "April 26, 1982" as the "Date of Birth" date
When /^(?:|I )select "([^\"]*)" as the "([^\"]*)" date$/ do |date, date_label|
- reformat_date!(date)
+ # reformat_date!(date)
select_date(date, :from => date_label)
end
@@ -129,85 +116,67 @@
end
Then /^(?:|I )should see "([^\"]*)"$/ do |text|
- if defined?(Steam)
- locate(text).should_not be_nil
- else
- response.should contain(text)
- end
+ assert_contain(response.body, text)
end
Then /^(?:|I )should see "([^\"]*)" within "([^\"]*)"$/ do |text, selector|
- within(selector) do |content|
- content.should contain(text)
- end
+ within(selector) { assert_contain(text) }
end
Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp|
- regexp = Regexp.new(regexp)
- if defined?(Steam)
- page.should contain(regexp)
- else
- response.should contain(regexp)
- end
+ assert_contain(response.body, Regexp.new(regexp))
end
Then /^(?:|I )should see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector|
- within(selector) do |content|
- regexp = Regexp.new(regexp)
- content.should contain(regexp)
- end
+ within(selector) { assert_contain(Regexp.new(regexp)) }
end
Then /^(?:|I )should not see "([^\"]*)"$/ do |text|
- response.should_not contain(text)
+ assert_not_contain(response.body, text)
end
Then /^(?:|I )should not see "([^\"]*)" within "([^\"]*)"$/ do |text, selector|
- within(selector) do |content|
- content.should_not contain(text)
- end
+ within(selector) { assert_not_contain(text) }
end
Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp|
- regexp = Regexp.new(regexp)
- response.should_not contain(regexp)
+ assert_not_contain(response.body, Regexp.new(regexp))
end
Then /^(?:|I )should not see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector|
- within(selector) do |content|
- regexp = Regexp.new(regexp)
- content.should_not contain(regexp)
- end
+ within(selector) { assert_not_contain(Regexp.new(regexp)) }
end
-Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |field, value|
- field_labeled(field).value.should =~ /#{value}/
+Then /^the "([^\"]*)" field should contain "([^\"]*)"$/ do |selector, text|
+ value = locate(:field, selector).value
+ value.respond_to?(:should) ? value.should =~ /#{text}/ : assert_match(/#{text}/, value)
end
-Then /^the "([^\"]*)" field should not contain "([^\"]*)"$/ do |field, value|
- field_labeled(field).value.should_not =~ /#{value}/
+Then /^the "([^\"]*)" field should not contain "([^\"]*)"$/ do |selector, text|
+ value = locate(:field, selector).value
+ value.respond_to?(:should) ? value.should_not =~ /#{text}/ : assert_no_match(/#{text}/, value)
end
-Then /^the "([^\"]*)" checkbox should be checked$/ do |label|
- field_labeled(label).should be_checked
+Then /^the "([^\"]*)" checkbox should be checked$/ do |selector|
+ field = locate(:check_box, selector)
+ field.respond_to?(:should) ? field.should(be_checked) : assert_equal('checked', field.checked)
end
-Then /^the "([^\"]*)" checkbox should not be checked$/ do |label|
- field_labeled(label).should_not be_checked
+Then /^the "([^\"]*)" checkbox should not be checked$/ do |selector|
+ field = locate(:check_box, selector)
+ field.respond_to?(:should) ? field.should_not(be_checked) : assert_not_equal('checked', field.checked)
end
Then /^(?:|I )should be on (.+)$/ do |page_name|
- URI.parse(page.url).path.should == path_to(page_name)
+ actual = URI.parse(page.url).path
+ expected = URI.parse(path_to(page_name)).path
+ actual.respond_to?(:should) ? actual.should == expected : assert_equal(expected, actual)
end
Then /^output the body$/ do
puts response.body
end
Then /^show me the page$/ do
- if defined?(Steam)
- raise '"show me the page" is only supported under Webrat'
- else
- save_and_open_page
- end
+ Steam.save_and_open(response)
end
View
@@ -9,6 +9,12 @@ module Steam
autoload :Response, 'steam/response'
autoload :Session, 'steam/session'
+ class ElementNotFound < StandardError
+ def initialize(*args)
+ super "could not find element: #{args.map { |arg| arg.inspect }.join(', ') }"
+ end
+ end
+
class << self
def config
@@config ||= {
@@ -33,11 +39,19 @@ def config
}
}
end
- end
+
+ def save_and_open(response)
+ filename = "/tmp/steam/#{response.url}.html"
+ FileUtils.mkdir_p(File.dirname(file))
+ File.open(filename, "w") { |f| f.write response.body }
+ open_in_browser(filename)
+ end
- class ElementNotFound < StandardError
- def initialize(*args)
- super "could not find element: #{args.map { |arg| arg.inspect }.join(', ') }"
+ def open_in_browser(filename)
+ require "launchy"
+ Launchy::Browser.run(filename)
+ rescue LoadError
+ warn "Sorry, you need to install launchy to open pages: `gem install launchy`"
end
end
end
@@ -19,12 +19,16 @@ class HtmlUnit
include Actions
- attr_accessor :client, :page, :connection, :request, :response
+ attr_accessor :client, :page, :request, :response
def initialize(*args)
@client = Client.new(*args)
end
+ def connection
+ client.connection
+ end
+
def close
@client.closeAllWindows
end
@@ -60,7 +64,7 @@ def locate_in_browser(*args, &block)
end
def within(*args, &block)
- Locator.within(*args, &block)
+ Locator.within(response.body, *args, &block)
end
protected
@@ -59,7 +59,10 @@ def set_hidden_field(element, options = {})
# TODO implement a way to supply content_type
def attach_file(element, path, options = {})
- fill_in(element, options.merge(:with => path))
+ respond_to do
+ element = locate_in_browser(:file, element, options)
+ element.setValueAttribute(path)
+ end
end
def submit_form(element, options = {})
@@ -19,7 +19,10 @@ class SilencingListener
def notify(message, origin); end
end
+ attr_reader :connection
+
def initialize(connection = nil, options = {})
+ @connection = connection
options = Steam.config[:html_unit].merge(options)
@java = WebClient.new(BrowserVersion.send(options[:browser_version]))
Oops, something went wrong.

0 comments on commit a852e17

Please sign in to comment.