Skip to content

Commit

Permalink
Update all tests to "expect" format and add support for RSpec 3 usage
Browse files Browse the repository at this point in the history
Swap from :skip to :capybara_skip since :skip is now used by RSpec 3
Add rspec 3 testing to beta gemfile
  • Loading branch information
twalpole committed Apr 2, 2014
1 parent 0d70095 commit ba37f78
Show file tree
Hide file tree
Showing 70 changed files with 1,260 additions and 1,229 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ gemfile:
- Gemfile
- gemfiles/Gemfile.base-versions
matrix:
include:
- gemfile: gemfiles/Gemfile.beta-versions
rvm: 2.1.1
exclude:
# Nokogiri 1.3.3 is not compatible with Rubinius or JRuby
- gemfile: gemfiles/Gemfile.base-versions
rvm: rbx
- gemfile: gemfiles/Gemfile.base-versions
rvm: jruby-19mode
allow_failures:
- gemfile: gemfiles/Gemfile.beta-versions
rvm: 2.1.1
before_install:
- CHROMEDRIVER_VERSION=$(wget -q -O - http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
- CHROMEDRIVER_URL="http://chromedriver.storage.googleapis.com/$CHROMEDRIVER_VERSION/chromedriver_linux64.zip"
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ You can get the [current path](http://rubydoc.info/github/jnicklas/capybara/mast
of the browsing session for test assertions:

```ruby
current_path.should == post_comments_path(post)
expect(current_path).to eq(post_comments_path(post))
```

### Clicking links and buttons
Expand Down Expand Up @@ -400,12 +400,12 @@ has_selector?`. Read the section on asynchronous JavaScript for an explanation.
You can use these with RSpec's magic matchers:
```ruby
page.should have_selector('table tr')
page.should have_selector(:xpath, '//table/tr')
expect(page).to have_selector('table tr')
expect(page).to have_selector(:xpath, '//table/tr')
page.should have_xpath('//table/tr')
page.should have_css('table tr.foo')
page.should have_content('foo')
expect(page).to have_xpath('//table/tr')
expect(page).to have_css('table tr.foo')
expect(page).to have_content('foo')
```
### Finding
Expand All @@ -432,7 +432,7 @@ to specific parts of the page:
```ruby
find('#navigation').click_link('Home')
find('#navigation').should have_button('Sign out')
expect(find('#navigation')).to have_button('Sign out')
```

### Scoping
Expand Down Expand Up @@ -595,7 +595,7 @@ When issuing instructions to the DSL such as:
```ruby
click_link('foo')
click_link('bar')
page.should have_content('baz')
expect(page).to have_content('baz')
```

If clicking on the *foo* link triggers an asynchronous process, such as
Expand Down Expand Up @@ -627,15 +627,15 @@ Capybara's Rspec matchers, however, are smart enough to handle either form.
The two following statements are functionally equivalent:

```ruby
page.should_not have_xpath('a')
page.should have_no_xpath('a')
expect(page).not_to have_xpath('a')
expect(page).to have_no_xpath('a')
```

Capybara's waiting behaviour is quite advanced, and can deal with situations
such as the following line of code:

```ruby
find('#sidebar').find('h1').should have_content('Something')
expect(find('#sidebar').find('h1')).to have_content('Something')
```

Even if JavaScript causes `#sidebar` to disappear off the page, Capybara
Expand Down
8 changes: 4 additions & 4 deletions features/step_definitions/capybara_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
end

Then /^I should see "([^"]*)"$/ do |text|
page.should have_content(text)
expect(page).to have_content(text)
end

Then /^Capybara should use the "([^"]*)" driver$/ do |driver|
Capybara.current_driver.should == driver.to_sym
expect(Capybara.current_driver).to eq(driver.to_sym)
end

When /^I use a matcher that fails$/ do
begin
page.should have_css('h1#doesnotexist')
expect(page).to have_css('h1#doesnotexist')
rescue StandardError => e
@error_message = e.message
end
end

Then /^the failing exception should be nice$/ do
@error_message.should =~ %r(expected to find css \"h1#doesnotexist\")
expect(@error_message).to match %r(expected to find css \"h1#doesnotexist\")
end

1 change: 1 addition & 0 deletions gemfiles/Gemfile.base-versions
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'rack', '= 1.3.0' # cannot go lower because referer tests need aa7ce77cd0
gem 'rack-test', '= 0.5.4'
gem 'nokogiri', '= 1.3.3'
gem 'rspec', '= 2.2.0'
gem 'fuubar', '>= 0.0.1'
gem 'cucumber', '= 0.10.5'
# We cannot test against older versions of selenium-webdriver without
# installing older compatible Firefox versions.
9 changes: 9 additions & 0 deletions gemfiles/Gemfile.beta-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source "https://rubygems.org"

gem 'bundler', '~> 1.0'
gemspec :path => '..'

gem 'xpath', :git => 'git://github.com/jnicklas/xpath.git'

gem 'rspec', '>= 3.0.0.beta2'
gem 'fuubar', '>= 2.0.0.beta1'
2 changes: 1 addition & 1 deletion lib/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def register_driver(name, &block)
# find(:row, 3)
# page.find('table#myTable').find(:row, 3).text
# page.find('table#myTable').has_selector?(:row, 3)
# within(:row, 3) { page.should have_content('$100.000') }
# within(:row, 3) { expect(page).to have_content('$100.000') }
#
# Here is another example:
#
Expand Down
1 change: 1 addition & 0 deletions lib/capybara/rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
end
end
end

2 changes: 1 addition & 1 deletion lib/capybara/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module Capybara
#
# session.fill_in('q', :with => 'Capybara')
# session.click_button('Search')
# session.should have_content('Capybara')
# expect(session).to have_content('Capybara')
#
# When using capybara/dsl, the Session is initialized automatically for you.
#
Expand Down
34 changes: 17 additions & 17 deletions lib/capybara/spec/session/all_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
end

it "should find all elements using the given locator" do
@session.all('//p').should have(3).elements
@session.all('//h1').first.text.should == 'This is a test'
@session.all("//input[@id='test_field']").first[:value].should == 'monkey'
expect(@session.all('//p').size).to eq(3)
expect(@session.all('//h1').first.text).to eq('This is a test')
expect(@session.all("//input[@id='test_field']").first[:value]).to eq('monkey')
end

it "should return an empty array when nothing was found" do
@session.all('//div[@id="nosuchthing"]').should be_empty
expect(@session.all('//div[@id="nosuchthing"]')).to be_empty
end

it "should accept an XPath instance" do
@session.visit('/form')
@xpath = XPath::HTML.fillable_field('Name')
@result = @session.all(@xpath).map { |r| r.value }
@result.should include('Smith', 'John', 'John Smith')
expect(@result).to include('Smith', 'John', 'John Smith')
end

it "should raise an error when given invalid options" do
Expand All @@ -26,44 +26,44 @@

context "with css selectors" do
it "should find all elements using the given selector" do
@session.all(:css, 'h1').first.text.should == 'This is a test'
@session.all(:css, "input[id='test_field']").first[:value].should == 'monkey'
expect(@session.all(:css, 'h1').first.text).to eq('This is a test')
expect(@session.all(:css, "input[id='test_field']").first[:value]).to eq('monkey')
end

it "should find all elements when given a list of selectors" do
@session.all(:css, 'h1, p').should have(4).elements
expect(@session.all(:css, 'h1, p').size).to eq(4)
end
end

context "with xpath selectors" do
it "should find the first element using the given locator" do
@session.all(:xpath, '//h1').first.text.should == 'This is a test'
@session.all(:xpath, "//input[@id='test_field']").first[:value].should == 'monkey'
expect(@session.all(:xpath, '//h1').first.text).to eq('This is a test')
expect(@session.all(:xpath, "//input[@id='test_field']").first[:value]).to eq('monkey')
end
end

context "with css as default selector" do
before { Capybara.default_selector = :css }
it "should find the first element using the given locator" do
@session.all('h1').first.text.should == 'This is a test'
@session.all("input[id='test_field']").first[:value].should == 'monkey'
expect(@session.all('h1').first.text).to eq('This is a test')
expect(@session.all("input[id='test_field']").first[:value]).to eq('monkey')
end
end

context "with visible filter" do
it "should only find visible nodes when true" do
@session.all(:css, "a.simple", :visible => true).should have(1).elements
expect(@session.all(:css, "a.simple", :visible => true).size).to eq(1)
end

it "should find nodes regardless of whether they are invisible when false" do
@session.all(:css, "a.simple", :visible => false).should have(2).elements
expect(@session.all(:css, "a.simple", :visible => false).size).to eq(2)
end

it "should default to Capybara.ignore_hidden_elements" do
Capybara.ignore_hidden_elements = true
@session.all(:css, "a.simple").should have(1).elements
expect(@session.all(:css, "a.simple").size).to eq(1)
Capybara.ignore_hidden_elements = false
@session.all(:css, "a.simple").should have(2).elements
expect(@session.all(:css, "a.simple").size).to eq(2)
end
end

Expand Down Expand Up @@ -145,7 +145,7 @@

it "should find any element using the given locator" do
@session.within(:xpath, "//div[@id='for_bar']") do
@session.all('.//li').should have(2).elements
expect(@session.all('.//li').size).to eq(2)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/spec/session/assert_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

Capybara::SpecHelper.spec '#refute_selector' do
it "should be an alias of #assert_no_selector" do
Capybara::Node::Matchers.instance_method(:refute_selector).should == Capybara::Node::Matchers.instance_method(:assert_no_selector)
expect(Capybara::Node::Matchers.instance_method(:refute_selector)).to eq Capybara::Node::Matchers.instance_method(:assert_no_selector)
end
end

Expand Down
30 changes: 15 additions & 15 deletions lib/capybara/spec/session/attach_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,71 +10,71 @@
it "should set a file path by id" do
@session.attach_file "form_image", __FILE__
@session.click_button('awesome')
extract_results(@session)['image'].should == File.basename(__FILE__)
expect(extract_results(@session)['image']).to eq(File.basename(__FILE__))
end

it "should set a file path by label" do
@session.attach_file "Image", __FILE__
@session.click_button('awesome')
extract_results(@session)['image'].should == File.basename(__FILE__)
expect(extract_results(@session)['image']).to eq(File.basename(__FILE__))
end

it "casts to string" do
@session.attach_file :"form_image", __FILE__
@session.click_button('awesome')
extract_results(@session)['image'].should == File.basename(__FILE__)
expect(extract_results(@session)['image']).to eq(File.basename(__FILE__))
end
end

context "with multipart form" do
it "should set a file path by id" do
@session.attach_file "form_document", @test_file_path
@session.click_button('Upload Single')
@session.should have_content(File.read(@test_file_path))
expect(@session).to have_content(File.read(@test_file_path))
end

it "should set a file path by label" do
@session.attach_file "Single Document", @test_file_path
@session.click_button('Upload Single')
@session.should have_content(File.read(@test_file_path))
expect(@session).to have_content(File.read(@test_file_path))
end

it "should not break if no file is submitted" do
@session.click_button('Upload Single')
@session.should have_content('No file uploaded')
expect(@session).to have_content('No file uploaded')
end

it "should send content type text/plain when uploading a text file" do
@session.attach_file "Single Document", @test_file_path
@session.click_button 'Upload Single'
@session.should have_content('text/plain')
expect(@session).to have_content('text/plain')
end

it "should send content type image/jpeg when uploading an image" do
@session.attach_file "Single Document", @test_jpg_file_path
@session.click_button 'Upload Single'
@session.should have_content('image/jpeg')
expect(@session).to have_content('image/jpeg')
end

it "should not break when using HTML5 multiple file input" do
@session.attach_file "Multiple Documents", @test_file_path
@session.click_button('Upload Multiple')
@session.body.should include("1 | ")#number of files
@session.should have_content(File.read(@test_file_path))
expect(@session.body).to include("1 | ")#number of files
expect(@session).to have_content(File.read(@test_file_path))
end

it "should not break when using HTML5 multiple file input uploading multiple files" do
pending "Selenium is buggy on this, see http://code.google.com/p/selenium/issues/detail?id=2239" if @session.respond_to?(:mode) && @session.mode.to_s =~ /^selenium/
@session.attach_file "Multiple Documents", [@test_file_path, @another_test_file_path]
@session.click_button('Upload Multiple')
@session.body.should include("2 | ")#number of files
@session.body.should include(File.read(@test_file_path))
@session.body.should include(File.read(@another_test_file_path))
expect(@session.body).to include("2 | ")#number of files
expect(@session.body).to include(File.read(@test_file_path))
expect(@session.body).to include(File.read(@another_test_file_path))
end

it "should not send anything when attaching no files to a multiple upload field" do
@session.click_button('Upload Empty Multiple')
@session.body.should include("Successfully ignored empty file field")
expect(@session.body).to include("Successfully ignored empty file field")
end
end

Expand All @@ -97,7 +97,7 @@
it "should set a file path by partial label when false" do
@session.attach_file "Imag", __FILE__, :exact => false
@session.click_button('awesome')
extract_results(@session)['image'].should == File.basename(__FILE__)
expect(extract_results(@session)['image']).to eq(File.basename(__FILE__))
end

it "not allow partial matches when true" do
Expand Down
8 changes: 4 additions & 4 deletions lib/capybara/spec/session/body_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
Capybara::SpecHelper.spec '#body' do
it "should return the unmodified page body" do
@session.visit('/')
@session.should have_content('Hello world!') # wait for content to appear if visit is async
@session.body.should include('Hello world!')
expect(@session).to have_content('Hello world!') # wait for content to appear if visit is async
expect(@session.body).to include('Hello world!')
end

if "".respond_to?(:encoding)
context "encoding of response between ascii and utf8" do
it "should be valid with html entities" do
@session.visit('/with_html_entities')
lambda { @session.body.encode!("UTF-8") }.should_not raise_error
expect { @session.body.encode!("UTF-8") }.not_to raise_error
end

it "should be valid without html entities" do
@session.visit('/with_html')
lambda { @session.body.encode!("UTF-8") }.should_not raise_error
expect { @session.body.encode!("UTF-8") }.not_to raise_error
end
end
end
Expand Down
Loading

0 comments on commit ba37f78

Please sign in to comment.