bbc-capybara 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +325 -0
- data/License.txt +22 -0
- data/README.md +815 -0
- data/lib/capybara.rb +368 -0
- data/lib/capybara/cucumber.rb +25 -0
- data/lib/capybara/driver/base.rb +64 -0
- data/lib/capybara/driver/node.rb +74 -0
- data/lib/capybara/dsl.rb +50 -0
- data/lib/capybara/node/actions.rb +146 -0
- data/lib/capybara/node/base.rb +63 -0
- data/lib/capybara/node/document.rb +25 -0
- data/lib/capybara/node/element.rb +201 -0
- data/lib/capybara/node/finders.rb +154 -0
- data/lib/capybara/node/matchers.rb +442 -0
- data/lib/capybara/node/simple.rb +132 -0
- data/lib/capybara/query.rb +63 -0
- data/lib/capybara/rack_test/browser.rb +126 -0
- data/lib/capybara/rack_test/driver.rb +80 -0
- data/lib/capybara/rack_test/form.rb +80 -0
- data/lib/capybara/rack_test/node.rb +121 -0
- data/lib/capybara/rails.rb +17 -0
- data/lib/capybara/rspec.rb +26 -0
- data/lib/capybara/rspec/features.rb +22 -0
- data/lib/capybara/rspec/matchers.rb +152 -0
- data/lib/capybara/selector.rb +156 -0
- data/lib/capybara/selenium/driver.rb +169 -0
- data/lib/capybara/selenium/node.rb +91 -0
- data/lib/capybara/server.rb +87 -0
- data/lib/capybara/session.rb +322 -0
- data/lib/capybara/spec/driver.rb +329 -0
- data/lib/capybara/spec/fixtures/capybara.jpg +3 -0
- data/lib/capybara/spec/fixtures/test_file.txt +1 -0
- data/lib/capybara/spec/public/jquery-ui.js +791 -0
- data/lib/capybara/spec/public/jquery.js +9046 -0
- data/lib/capybara/spec/public/test.js +43 -0
- data/lib/capybara/spec/session.rb +148 -0
- data/lib/capybara/spec/session/all_spec.rb +78 -0
- data/lib/capybara/spec/session/attach_file_spec.rb +76 -0
- data/lib/capybara/spec/session/check_spec.rb +68 -0
- data/lib/capybara/spec/session/choose_spec.rb +29 -0
- data/lib/capybara/spec/session/click_button_spec.rb +305 -0
- data/lib/capybara/spec/session/click_link_or_button_spec.rb +37 -0
- data/lib/capybara/spec/session/click_link_spec.rb +120 -0
- data/lib/capybara/spec/session/current_url_spec.rb +83 -0
- data/lib/capybara/spec/session/fill_in_spec.rb +127 -0
- data/lib/capybara/spec/session/find_button_spec.rb +18 -0
- data/lib/capybara/spec/session/find_by_id_spec.rb +18 -0
- data/lib/capybara/spec/session/find_field_spec.rb +26 -0
- data/lib/capybara/spec/session/find_link_spec.rb +19 -0
- data/lib/capybara/spec/session/find_spec.rb +168 -0
- data/lib/capybara/spec/session/first_spec.rb +105 -0
- data/lib/capybara/spec/session/has_button_spec.rb +32 -0
- data/lib/capybara/spec/session/has_css_spec.rb +243 -0
- data/lib/capybara/spec/session/has_field_spec.rb +192 -0
- data/lib/capybara/spec/session/has_link_spec.rb +37 -0
- data/lib/capybara/spec/session/has_select_spec.rb +129 -0
- data/lib/capybara/spec/session/has_selector_spec.rb +129 -0
- data/lib/capybara/spec/session/has_table_spec.rb +34 -0
- data/lib/capybara/spec/session/has_text_spec.rb +138 -0
- data/lib/capybara/spec/session/has_xpath_spec.rb +123 -0
- data/lib/capybara/spec/session/headers.rb +19 -0
- data/lib/capybara/spec/session/javascript.rb +312 -0
- data/lib/capybara/spec/session/response_code.rb +19 -0
- data/lib/capybara/spec/session/select_spec.rb +119 -0
- data/lib/capybara/spec/session/text_spec.rb +24 -0
- data/lib/capybara/spec/session/uncheck_spec.rb +21 -0
- data/lib/capybara/spec/session/unselect_spec.rb +67 -0
- data/lib/capybara/spec/session/within_spec.rb +178 -0
- data/lib/capybara/spec/test_app.rb +156 -0
- data/lib/capybara/spec/views/buttons.erb +4 -0
- data/lib/capybara/spec/views/fieldsets.erb +29 -0
- data/lib/capybara/spec/views/form.erb +366 -0
- data/lib/capybara/spec/views/frame_one.erb +8 -0
- data/lib/capybara/spec/views/frame_two.erb +8 -0
- data/lib/capybara/spec/views/header_links.erb +7 -0
- data/lib/capybara/spec/views/host_links.erb +12 -0
- data/lib/capybara/spec/views/popup_one.erb +8 -0
- data/lib/capybara/spec/views/popup_two.erb +8 -0
- data/lib/capybara/spec/views/postback.erb +13 -0
- data/lib/capybara/spec/views/tables.erb +62 -0
- data/lib/capybara/spec/views/with_html.erb +75 -0
- data/lib/capybara/spec/views/with_html_entities.erb +1 -0
- data/lib/capybara/spec/views/with_js.erb +53 -0
- data/lib/capybara/spec/views/with_scope.erb +36 -0
- data/lib/capybara/spec/views/with_simple_html.erb +1 -0
- data/lib/capybara/spec/views/within_frames.erb +10 -0
- data/lib/capybara/spec/views/within_popups.erb +25 -0
- data/lib/capybara/util/save_and_open_page.rb +45 -0
- data/lib/capybara/util/timeout.rb +27 -0
- data/lib/capybara/version.rb +3 -0
- data/spec/basic_node_spec.rb +89 -0
- data/spec/capybara_spec.rb +46 -0
- data/spec/driver/rack_test_driver_spec.rb +90 -0
- data/spec/driver/selenium_driver_spec.rb +55 -0
- data/spec/dsl_spec.rb +255 -0
- data/spec/fixtures/selenium_driver_rspec_failure.rb +8 -0
- data/spec/fixtures/selenium_driver_rspec_success.rb +8 -0
- data/spec/rspec/features_spec.rb +43 -0
- data/spec/rspec/matchers_spec.rb +564 -0
- data/spec/rspec_spec.rb +51 -0
- data/spec/save_and_open_page_spec.rb +155 -0
- data/spec/server_spec.rb +74 -0
- data/spec/session/rack_test_session_spec.rb +61 -0
- data/spec/session/selenium_session_spec.rb +26 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/timeout_spec.rb +28 -0
- metadata +297 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
shared_examples_for "current_url" do
|
|
2
|
+
before :all do
|
|
3
|
+
@servers = 2.times.map { Capybara::Server.new(TestApp.clone).boot }
|
|
4
|
+
# sanity check
|
|
5
|
+
@servers[0].port.should_not == @servers[1].port
|
|
6
|
+
@servers.map { |s| s.port }.should_not include 80
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def bases
|
|
10
|
+
@servers.map { |s| "http://#{s.host}:#{s.port}" }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
describe '#current_url, #current_path, #current_host' do
|
|
14
|
+
def should_be_on server_index, path="/host", scheme="http"
|
|
15
|
+
# Check that we are on /host on the given server
|
|
16
|
+
s = @servers[server_index]
|
|
17
|
+
@session.current_url.chomp('?').should == "#{scheme}://#{s.host}:#{s.port}#{path}"
|
|
18
|
+
@session.current_host.should == "#{scheme}://#{s.host}" # no port
|
|
19
|
+
@session.current_path.should == path
|
|
20
|
+
if path == '/host'
|
|
21
|
+
# Server should agree with us
|
|
22
|
+
@session.body.should include("Current host is #{scheme}://#{s.host}:#{s.port}")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def visit_host_links
|
|
27
|
+
@session.visit("#{bases[0]}/host_links?absolute_host=#{bases[1]}")
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "is affected by visiting a page directly" do
|
|
31
|
+
@session.visit("#{bases[0]}/host")
|
|
32
|
+
should_be_on 0
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "returns to the app host when visiting a relative url" do
|
|
36
|
+
Capybara.app_host = bases[1]
|
|
37
|
+
@session.visit("#{bases[0]}/host")
|
|
38
|
+
should_be_on 0
|
|
39
|
+
@session.visit('/host')
|
|
40
|
+
should_be_on 1
|
|
41
|
+
Capybara.app_host = nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "is affected by setting Capybara.app_host" do
|
|
45
|
+
Capybara.app_host = bases[0]
|
|
46
|
+
@session.visit("/host")
|
|
47
|
+
should_be_on 0
|
|
48
|
+
Capybara.app_host = bases[1]
|
|
49
|
+
@session.visit("/host")
|
|
50
|
+
should_be_on 1
|
|
51
|
+
Capybara.app_host = nil
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "is unaffected by following a relative link" do
|
|
55
|
+
visit_host_links
|
|
56
|
+
@session.click_link("Relative Host")
|
|
57
|
+
should_be_on 0
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it "is affected by following an absolute link" do
|
|
61
|
+
visit_host_links
|
|
62
|
+
@session.click_link("Absolute Host")
|
|
63
|
+
should_be_on 1
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "is unaffected by posting through a relative form" do
|
|
67
|
+
visit_host_links
|
|
68
|
+
@session.click_button("Relative Host")
|
|
69
|
+
should_be_on 0
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "is affected by posting through an absolute form" do
|
|
73
|
+
visit_host_links
|
|
74
|
+
@session.click_button("Absolute Host")
|
|
75
|
+
should_be_on 1
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "is affected by following a redirect" do
|
|
79
|
+
@session.visit("#{bases[0]}/redirect")
|
|
80
|
+
should_be_on 0, "/landed"
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
shared_examples_for "fill_in" do
|
|
2
|
+
describe "#fill_in" do
|
|
3
|
+
before do
|
|
4
|
+
@session.visit('/form')
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
it "should fill in a text field by id" do
|
|
8
|
+
@session.fill_in('form_first_name', :with => 'Harry')
|
|
9
|
+
@session.click_button('awesome')
|
|
10
|
+
extract_results(@session)['first_name'].should == 'Harry'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should fill in a text field by name" do
|
|
14
|
+
@session.fill_in('form[last_name]', :with => 'Green')
|
|
15
|
+
@session.click_button('awesome')
|
|
16
|
+
extract_results(@session)['last_name'].should == 'Green'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "should fill in a text field by label without for" do
|
|
20
|
+
@session.fill_in('Street', :with => 'Avenue Q')
|
|
21
|
+
@session.click_button('awesome')
|
|
22
|
+
extract_results(@session)['street'].should == 'Avenue Q'
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "should fill in a url field by label without for" do
|
|
26
|
+
@session.fill_in('Html5 Url', :with => 'http://www.avenueq.com')
|
|
27
|
+
@session.click_button('html5_submit')
|
|
28
|
+
extract_results(@session)['html5_url'].should == 'http://www.avenueq.com'
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "should favour exact label matches over partial matches" do
|
|
32
|
+
@session.fill_in('Name', :with => 'Harry Jones')
|
|
33
|
+
@session.click_button('awesome')
|
|
34
|
+
extract_results(@session)['name'].should == 'Harry Jones'
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it "should fill in a textarea by id" do
|
|
38
|
+
@session.fill_in('form_description', :with => 'Texty text')
|
|
39
|
+
@session.click_button('awesome')
|
|
40
|
+
extract_results(@session)['description'].should == 'Texty text'
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "should fill in a textarea by label" do
|
|
44
|
+
@session.fill_in('Description', :with => 'Texty text')
|
|
45
|
+
@session.click_button('awesome')
|
|
46
|
+
extract_results(@session)['description'].should == 'Texty text'
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "should fill in a textarea by name" do
|
|
50
|
+
@session.fill_in('form[description]', :with => 'Texty text')
|
|
51
|
+
@session.click_button('awesome')
|
|
52
|
+
extract_results(@session)['description'].should == 'Texty text'
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "should fill in a password field by id" do
|
|
56
|
+
@session.fill_in('form_password', :with => 'supasikrit')
|
|
57
|
+
@session.click_button('awesome')
|
|
58
|
+
extract_results(@session)['password'].should == 'supasikrit'
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "should fill in a field with a custom type" do
|
|
62
|
+
@session.fill_in('Schmooo', :with => 'Schmooo is the game')
|
|
63
|
+
@session.click_button('awesome')
|
|
64
|
+
extract_results(@session)['schmooo'].should == 'Schmooo is the game'
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "should fill in a field without a type" do
|
|
68
|
+
@session.fill_in('Phone', :with => '+1 555 7022')
|
|
69
|
+
@session.click_button('awesome')
|
|
70
|
+
extract_results(@session)['phone'].should == '+1 555 7022'
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it "should fill in a text field respecting its maxlength attribute" do
|
|
74
|
+
@session.fill_in('Zipcode', :with => '52071350')
|
|
75
|
+
@session.click_button('awesome')
|
|
76
|
+
extract_results(@session)['zipcode'].should == '52071'
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "should fill in a password field by name" do
|
|
80
|
+
@session.fill_in('form[password]', :with => 'supasikrit')
|
|
81
|
+
@session.click_button('awesome')
|
|
82
|
+
extract_results(@session)['password'].should == 'supasikrit'
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it "should fill in a password field by label" do
|
|
86
|
+
@session.fill_in('Password', :with => 'supasikrit')
|
|
87
|
+
@session.click_button('awesome')
|
|
88
|
+
extract_results(@session)['password'].should == 'supasikrit'
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "should fill in a password field by name" do
|
|
92
|
+
@session.fill_in('form[password]', :with => 'supasikrit')
|
|
93
|
+
@session.click_button('awesome')
|
|
94
|
+
extract_results(@session)['password'].should == 'supasikrit'
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
it "should prefer exact matches over partial matches" do
|
|
98
|
+
@session.fill_in('Name', :with => 'Ford Prefect')
|
|
99
|
+
@session.click_button('awesome')
|
|
100
|
+
extract_results(@session)['name'].should == 'Ford Prefect'
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it "should throw an exception if a hash containing 'with' is not provided" do
|
|
104
|
+
lambda{@session.fill_in 'Name', 'ignu'}.should raise_error
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
context "with ignore_hidden_fields" do
|
|
108
|
+
before { Capybara.ignore_hidden_elements = true }
|
|
109
|
+
after { Capybara.ignore_hidden_elements = false }
|
|
110
|
+
it "should not find a hidden field" do
|
|
111
|
+
msg = "no text field, text area or password field with id, name, or label 'Super Secret' found"
|
|
112
|
+
running do
|
|
113
|
+
@session.fill_in('Super Secret', :with => '777')
|
|
114
|
+
end.should raise_error(Capybara::ElementNotFound, msg)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
context "with a locator that doesn't exist" do
|
|
119
|
+
it "should raise an error" do
|
|
120
|
+
msg = "no text field, text area or password field with id, name, or label 'does not exist' found"
|
|
121
|
+
running do
|
|
122
|
+
@session.fill_in('does not exist', :with => 'Blah blah')
|
|
123
|
+
end.should raise_error(Capybara::ElementNotFound, msg)
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
shared_examples_for "find_button" do
|
|
2
|
+
describe '#find_button' do
|
|
3
|
+
before do
|
|
4
|
+
@session.visit('/form')
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
it "should find any field" do
|
|
8
|
+
@session.find_button('med')[:id].should == "mediocre"
|
|
9
|
+
@session.find_button('crap321').value.should == "crappy"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "should raise error if the field doesn't exist" do
|
|
13
|
+
running do
|
|
14
|
+
@session.find_button('Does not exist')
|
|
15
|
+
end.should raise_error(Capybara::ElementNotFound)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
shared_examples_for "find_by_id" do
|
|
2
|
+
describe '#find_by_id' do
|
|
3
|
+
before do
|
|
4
|
+
@session.visit('/with_html')
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
it "should find any element by id" do
|
|
8
|
+
@session.find_by_id('red').tag_name.should == 'a'
|
|
9
|
+
@session.find_by_id('hidden_via_ancestor').tag_name.should == 'div'
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "should raise error if no element with id is found" do
|
|
13
|
+
running do
|
|
14
|
+
@session.find_by_id('nothing_with_this_id')
|
|
15
|
+
end.should raise_error(Capybara::ElementNotFound)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
shared_examples_for "find_field" do
|
|
2
|
+
describe '#find_field' do
|
|
3
|
+
before do
|
|
4
|
+
@session.visit('/form')
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
it "should find any field" do
|
|
8
|
+
@session.find_field('Dog').value.should == 'dog'
|
|
9
|
+
@session.find_field('form_description').text.should == 'Descriptive text goes here'
|
|
10
|
+
@session.find_field('Region')[:name].should == 'form[region]'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should raise error if the field doesn't exist" do
|
|
14
|
+
running do
|
|
15
|
+
@session.find_field('Does not exist')
|
|
16
|
+
end.should raise_error(Capybara::ElementNotFound)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "should be aliased as 'field_labeled' for webrat compatibility" do
|
|
20
|
+
@session.field_labeled('Dog').value.should == 'dog'
|
|
21
|
+
running do
|
|
22
|
+
@session.field_labeled('Does not exist')
|
|
23
|
+
end.should raise_error(Capybara::ElementNotFound)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
shared_examples_for "find_link" do
|
|
2
|
+
|
|
3
|
+
describe '#find_link' do
|
|
4
|
+
before do
|
|
5
|
+
@session.visit('/with_html')
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "should find any field" do
|
|
9
|
+
@session.find_link('foo').text.should == "ullamco"
|
|
10
|
+
@session.find_link('labore')[:href].should =~ %r(/with_simple_html$)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should raise error if the field doesn't exist" do
|
|
14
|
+
running do
|
|
15
|
+
@session.find_link('Does not exist')
|
|
16
|
+
end.should raise_error(Capybara::ElementNotFound)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
shared_examples_for "find" do
|
|
2
|
+
describe '#find' do
|
|
3
|
+
before do
|
|
4
|
+
@session.visit('/with_html')
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
after do
|
|
8
|
+
Capybara::Selector.remove(:monkey)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "should find the first element using the given locator" do
|
|
12
|
+
@session.find('//h1').text.should == 'This is a test'
|
|
13
|
+
@session.find("//input[@id='test_field']")[:value].should == 'monkey'
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should find the first element using the given locator and options" do
|
|
17
|
+
@session.find('//a', :text => 'Redirect')[:id].should == 'red'
|
|
18
|
+
@session.find(:css, 'a', :text => 'A link')[:title].should == 'twas a fine link'
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
describe 'the returned node' do
|
|
22
|
+
it "should act like a session object" do
|
|
23
|
+
@session.visit('/form')
|
|
24
|
+
@form = @session.find(:css, '#get-form')
|
|
25
|
+
@form.should have_field('Middle Name')
|
|
26
|
+
@form.should have_no_field('Languages')
|
|
27
|
+
@form.fill_in('Middle Name', :with => 'Monkey')
|
|
28
|
+
@form.click_button('med')
|
|
29
|
+
extract_results(@session)['middle_name'].should == 'Monkey'
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "should scope CSS selectors" do
|
|
33
|
+
@session.find(:css, '#second').should have_no_css('h1')
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "should have a reference to its parent if there is one" do
|
|
37
|
+
@node = @session.find(:css, '#first')
|
|
38
|
+
@node.parent.should == @node.session.document
|
|
39
|
+
@node.find('a').parent.should == @node
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
context "with css selectors" do
|
|
44
|
+
it "should find the first element using the given locator" do
|
|
45
|
+
@session.find(:css, 'h1').text.should == 'This is a test'
|
|
46
|
+
@session.find(:css, "input[id='test_field']")[:value].should == 'monkey'
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
context "with id selectors" do
|
|
51
|
+
it "should find the first element using the given locator" do
|
|
52
|
+
@session.find(:id, 'john_monkey').text.should == 'Monkey John'
|
|
53
|
+
@session.find(:id, 'red').text.should == 'Redirect'
|
|
54
|
+
@session.find(:red).text.should == 'Redirect'
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
context "with xpath selectors" do
|
|
59
|
+
it "should find the first element using the given locator" do
|
|
60
|
+
@session.find(:xpath, '//h1').text.should == 'This is a test'
|
|
61
|
+
@session.find(:xpath, "//input[@id='test_field']")[:value].should == 'monkey'
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
context "with custom selector" do
|
|
66
|
+
it "should use the custom selector" do
|
|
67
|
+
Capybara.add_selector(:monkey) do
|
|
68
|
+
xpath { |name| ".//*[@id='#{name}_monkey']" }
|
|
69
|
+
end
|
|
70
|
+
@session.find(:monkey, 'john').text.should == 'Monkey John'
|
|
71
|
+
@session.find(:monkey, 'paul').text.should == 'Monkey Paul'
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
context "with custom selector with :for option" do
|
|
76
|
+
it "should use the selector when it matches the :for option" do
|
|
77
|
+
Capybara.add_selector(:monkey) do
|
|
78
|
+
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
|
|
79
|
+
match { |value| value.is_a?(Fixnum) }
|
|
80
|
+
end
|
|
81
|
+
@session.find(:monkey, '2').text.should == 'Monkey Paul'
|
|
82
|
+
@session.find(1).text.should == 'Monkey John'
|
|
83
|
+
@session.find(2).text.should == 'Monkey Paul'
|
|
84
|
+
@session.find('//h1').text.should == 'This is a test'
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
context "with custom selector with failure_message option" do
|
|
89
|
+
it "should raise an error with the failure message if the element is not found" do
|
|
90
|
+
Capybara.add_selector(:monkey) do
|
|
91
|
+
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
|
|
92
|
+
failure_message { |node, selector| node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') }
|
|
93
|
+
end
|
|
94
|
+
running do
|
|
95
|
+
@session.find(:monkey, '14').text.should == 'Monkey Paul'
|
|
96
|
+
end.should raise_error(Capybara::ElementNotFound, "Monkey John, Monkey Paul")
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
it "should pass the selector as the second argument" do
|
|
100
|
+
Capybara.add_selector(:monkey) do
|
|
101
|
+
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
|
|
102
|
+
failure_message { |node, selector| selector.name.to_s + ': ' + selector.locator + ' - ' + node.all(".//*[contains(@id, 'monkey')]").map { |node| node.text }.sort.join(', ') }
|
|
103
|
+
end
|
|
104
|
+
running do
|
|
105
|
+
@session.find(:monkey, '14').text.should == 'Monkey Paul'
|
|
106
|
+
end.should raise_error(Capybara::ElementNotFound, "monkey: 14 - Monkey John, Monkey Paul")
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
context "with custom selector with custom filter" do
|
|
111
|
+
before do
|
|
112
|
+
Capybara.add_selector(:monkey) do
|
|
113
|
+
xpath { |num| ".//*[contains(@id, 'monkey')][#{num}]" }
|
|
114
|
+
filter(:name) { |node, name| node.text == name }
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
it "should find elements that match the filter" do
|
|
119
|
+
@session.find(:monkey, '1', :name => 'Monkey John').text.should == 'Monkey John'
|
|
120
|
+
@session.find(:monkey, '2', :name => 'Monkey Paul').text.should == 'Monkey Paul'
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "should not find elements that don't match the filter" do
|
|
124
|
+
expect { @session.find(:monkey, '2', :name => 'Monkey John') }.to raise_error(Capybara::ElementNotFound)
|
|
125
|
+
expect { @session.find(:monkey, '1', :name => 'Monkey Paul') }.to raise_error(Capybara::ElementNotFound)
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
context "with css as default selector" do
|
|
130
|
+
before { Capybara.default_selector = :css }
|
|
131
|
+
it "should find the first element using the given locator" do
|
|
132
|
+
@session.find('h1').text.should == 'This is a test'
|
|
133
|
+
@session.find("input[id='test_field']")[:value].should == 'monkey'
|
|
134
|
+
end
|
|
135
|
+
after { Capybara.default_selector = :xpath }
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it "should raise ElementNotFound with specified fail message if nothing was found" do
|
|
139
|
+
running do
|
|
140
|
+
@session.find(:xpath, '//div[@id="nosuchthing"]', :message => 'arghh').should be_nil
|
|
141
|
+
end.should raise_error(Capybara::ElementNotFound, "arghh")
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it "should raise ElementNotFound with a useful default message if nothing was found" do
|
|
145
|
+
running do
|
|
146
|
+
@session.find(:xpath, '//div[@id="nosuchthing"]').should be_nil
|
|
147
|
+
end.should raise_error(Capybara::ElementNotFound, "Unable to find xpath \"//div[@id=\\\"nosuchthing\\\"]\"")
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
it "should accept an XPath instance and respect the order of paths" do
|
|
151
|
+
@session.visit('/form')
|
|
152
|
+
@xpath = XPath::HTML.fillable_field('Name')
|
|
153
|
+
@session.find(@xpath).value.should == 'John Smith'
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
context "within a scope" do
|
|
157
|
+
before do
|
|
158
|
+
@session.visit('/with_scope')
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
it "should find the first element using the given locator" do
|
|
162
|
+
@session.within(:xpath, "//div[@id='for_bar']") do
|
|
163
|
+
@session.find('.//li').text.should =~ /With Simple HTML/
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|