capybara 2.13.0 → 2.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. checksums.yaml +5 -5
  2. data/History.md +218 -18
  3. data/README.md +54 -23
  4. data/lib/capybara/config.rb +132 -0
  5. data/lib/capybara/cucumber.rb +1 -0
  6. data/lib/capybara/driver/base.rb +14 -0
  7. data/lib/capybara/dsl.rb +1 -3
  8. data/lib/capybara/helpers.rb +3 -3
  9. data/lib/capybara/minitest/spec.rb +14 -37
  10. data/lib/capybara/minitest.rb +95 -114
  11. data/lib/capybara/node/actions.rb +10 -10
  12. data/lib/capybara/node/base.rb +7 -2
  13. data/lib/capybara/node/element.rb +9 -3
  14. data/lib/capybara/node/finders.rb +92 -18
  15. data/lib/capybara/node/matchers.rb +21 -9
  16. data/lib/capybara/node/simple.rb +5 -0
  17. data/lib/capybara/queries/ancestor_query.rb +25 -0
  18. data/lib/capybara/queries/base_query.rb +12 -3
  19. data/lib/capybara/queries/current_path_query.rb +13 -9
  20. data/lib/capybara/queries/selector_query.rb +62 -23
  21. data/lib/capybara/queries/sibling_query.rb +25 -0
  22. data/lib/capybara/queries/text_query.rb +10 -5
  23. data/lib/capybara/queries/title_query.rb +1 -0
  24. data/lib/capybara/rack_test/browser.rb +13 -5
  25. data/lib/capybara/rack_test/driver.rb +6 -1
  26. data/lib/capybara/rack_test/form.rb +4 -3
  27. data/lib/capybara/rack_test/node.rb +1 -1
  28. data/lib/capybara/rspec/compound.rb +95 -0
  29. data/lib/capybara/rspec/matcher_proxies.rb +45 -0
  30. data/lib/capybara/rspec/matchers.rb +108 -7
  31. data/lib/capybara/rspec.rb +3 -1
  32. data/lib/capybara/selector/filter.rb +13 -41
  33. data/lib/capybara/selector/filter_set.rb +30 -4
  34. data/lib/capybara/selector/filters/base.rb +33 -0
  35. data/lib/capybara/selector/filters/expression_filter.rb +40 -0
  36. data/lib/capybara/selector/filters/node_filter.rb +27 -0
  37. data/lib/capybara/selector/selector.rb +36 -15
  38. data/lib/capybara/selector.rb +63 -42
  39. data/lib/capybara/selenium/driver.rb +177 -33
  40. data/lib/capybara/selenium/node.rb +106 -55
  41. data/lib/capybara/server.rb +6 -5
  42. data/lib/capybara/session/config.rb +114 -0
  43. data/lib/capybara/session/matchers.rb +15 -4
  44. data/lib/capybara/session.rb +178 -65
  45. data/lib/capybara/spec/fixtures/no_extension +1 -0
  46. data/lib/capybara/spec/public/test.js +18 -3
  47. data/lib/capybara/spec/session/accept_alert_spec.rb +9 -1
  48. data/lib/capybara/spec/session/accept_prompt_spec.rb +29 -1
  49. data/lib/capybara/spec/session/all_spec.rb +13 -1
  50. data/lib/capybara/spec/session/ancestor_spec.rb +85 -0
  51. data/lib/capybara/spec/session/assert_all_of_selectors_spec.rb +24 -8
  52. data/lib/capybara/spec/session/assert_selector.rb +1 -1
  53. data/lib/capybara/spec/session/assert_text.rb +8 -0
  54. data/lib/capybara/spec/session/assert_title.rb +22 -9
  55. data/lib/capybara/spec/session/attach_file_spec.rb +8 -1
  56. data/lib/capybara/spec/session/check_spec.rb +4 -4
  57. data/lib/capybara/spec/session/choose_spec.rb +2 -2
  58. data/lib/capybara/spec/session/click_button_spec.rb +1 -1
  59. data/lib/capybara/spec/session/click_link_or_button_spec.rb +3 -3
  60. data/lib/capybara/spec/session/click_link_spec.rb +1 -1
  61. data/lib/capybara/spec/session/current_url_spec.rb +3 -3
  62. data/lib/capybara/spec/session/dismiss_confirm_spec.rb +3 -3
  63. data/lib/capybara/spec/session/dismiss_prompt_spec.rb +1 -1
  64. data/lib/capybara/spec/session/evaluate_async_script_spec.rb +22 -0
  65. data/lib/capybara/spec/session/evaluate_script_spec.rb +1 -1
  66. data/lib/capybara/spec/session/fill_in_spec.rb +8 -2
  67. data/lib/capybara/spec/session/find_field_spec.rb +1 -0
  68. data/lib/capybara/spec/session/find_spec.rb +8 -6
  69. data/lib/capybara/spec/session/first_spec.rb +10 -5
  70. data/lib/capybara/spec/session/has_all_selectors_spec.rb +69 -0
  71. data/lib/capybara/spec/session/has_css_spec.rb +11 -0
  72. data/lib/capybara/spec/session/has_current_path_spec.rb +52 -7
  73. data/lib/capybara/spec/session/has_link_spec.rb +4 -4
  74. data/lib/capybara/spec/session/has_none_selectors_spec.rb +76 -0
  75. data/lib/capybara/spec/session/has_select_spec.rb +64 -6
  76. data/lib/capybara/spec/session/has_selector_spec.rb +1 -3
  77. data/lib/capybara/spec/session/has_text_spec.rb +5 -3
  78. data/lib/capybara/spec/session/has_title_spec.rb +4 -2
  79. data/lib/capybara/spec/session/has_xpath_spec.rb +5 -3
  80. data/lib/capybara/spec/session/node_spec.rb +50 -26
  81. data/lib/capybara/spec/session/refresh_spec.rb +28 -0
  82. data/lib/capybara/spec/session/reset_session_spec.rb +3 -3
  83. data/lib/capybara/spec/session/select_spec.rb +3 -2
  84. data/lib/capybara/spec/session/sibling_spec.rb +52 -0
  85. data/lib/capybara/spec/session/uncheck_spec.rb +2 -2
  86. data/lib/capybara/spec/session/unselect_spec.rb +2 -2
  87. data/lib/capybara/spec/session/visit_spec.rb +56 -1
  88. data/lib/capybara/spec/session/window/become_closed_spec.rb +11 -11
  89. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +11 -9
  90. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +4 -4
  91. data/lib/capybara/spec/session/window/within_window_spec.rb +27 -2
  92. data/lib/capybara/spec/spec_helper.rb +28 -4
  93. data/lib/capybara/spec/test_app.rb +3 -1
  94. data/lib/capybara/spec/views/form.erb +27 -1
  95. data/lib/capybara/spec/views/initial_alert.erb +10 -0
  96. data/lib/capybara/spec/views/with_fixed_header_footer.erb +17 -0
  97. data/lib/capybara/spec/views/with_hover.erb +5 -0
  98. data/lib/capybara/spec/views/with_html.erb +33 -2
  99. data/lib/capybara/spec/views/with_js.erb +12 -0
  100. data/lib/capybara/spec/views/with_windows.erb +4 -0
  101. data/lib/capybara/version.rb +1 -1
  102. data/lib/capybara/window.rb +1 -1
  103. data/lib/capybara.rb +102 -124
  104. data/spec/capybara_spec.rb +43 -21
  105. data/spec/dsl_spec.rb +1 -0
  106. data/spec/filter_set_spec.rb +28 -0
  107. data/spec/minitest_spec.rb +9 -1
  108. data/spec/minitest_spec_spec.rb +19 -5
  109. data/spec/per_session_config_spec.rb +67 -0
  110. data/spec/result_spec.rb +20 -0
  111. data/spec/rspec/shared_spec_matchers.rb +148 -44
  112. data/spec/rspec/views_spec.rb +4 -0
  113. data/spec/rspec_matchers_spec.rb +46 -0
  114. data/spec/rspec_spec.rb +77 -0
  115. data/spec/selector_spec.rb +2 -1
  116. data/spec/selenium_spec_chrome.rb +25 -17
  117. data/spec/selenium_spec_firefox.rb +2 -1
  118. data/spec/selenium_spec_marionette.rb +18 -5
  119. data/spec/session_spec.rb +44 -0
  120. data/spec/shared_selenium_session.rb +72 -8
  121. data/spec/spec_helper.rb +4 -0
  122. metadata +55 -8
@@ -11,6 +11,13 @@ Capybara::SpecHelper.spec '#accept_prompt', requires: [:modals] do
11
11
  expect(@session).to have_xpath("//a[@id='open-prompt' and @response='']")
12
12
  end
13
13
 
14
+ it "should accept the prompt with no message when there is a default" do
15
+ @session.accept_prompt do
16
+ @session.click_link('Open defaulted prompt')
17
+ end
18
+ expect(@session).to have_xpath("//a[@id='open-prompt-with-default' and @response='Default value!']")
19
+ end
20
+
14
21
  it "should return the message presented" do
15
22
  message = @session.accept_prompt do
16
23
  @session.click_link('Open prompt')
@@ -25,6 +32,28 @@ Capybara::SpecHelper.spec '#accept_prompt', requires: [:modals] do
25
32
  expect(@session).to have_xpath("//a[@id='open-prompt' and @response='the response']")
26
33
  end
27
34
 
35
+ it "should accept the prompt with a response when there is a default" do
36
+ @session.accept_prompt with: 'the response' do
37
+ @session.click_link('Open defaulted prompt')
38
+ end
39
+ expect(@session).to have_xpath("//a[@id='open-prompt-with-default' and @response='the response']")
40
+ end
41
+
42
+ it "should accept the prompt with a blank response when there is a default" do
43
+ pending "Geckodriver doesn't set a blank response currently" if marionette?(@session)
44
+ @session.accept_prompt with: '' do
45
+ @session.click_link('Open defaulted prompt')
46
+ end
47
+ expect(@session).to have_xpath("//a[@id='open-prompt-with-default' and @response='']")
48
+ end
49
+
50
+ it "should allow special characters in the reponse" do
51
+ @session.accept_prompt with: '\'the\' \b "response"' do
52
+ @session.click_link('Open prompt')
53
+ end
54
+ expect(@session).to have_xpath(%{//a[@id='open-prompt' and @response=concat("'the' ", '\\b "response"')]})
55
+ end
56
+
28
57
  it "should accept the prompt if the message matches" do
29
58
  @session.accept_prompt 'Prompt opened', with: 'matched' do
30
59
  @session.click_link('Open prompt')
@@ -40,7 +69,6 @@ Capybara::SpecHelper.spec '#accept_prompt', requires: [:modals] do
40
69
  end.to raise_error(Capybara::ModalNotFound)
41
70
  end
42
71
 
43
-
44
72
  it "should return the message presented" do
45
73
  message = @session.accept_prompt with: 'the response' do
46
74
  @session.click_link('Open prompt')
@@ -16,7 +16,8 @@ Capybara::SpecHelper.spec "#all" do
16
16
 
17
17
  it "should accept an XPath instance" do
18
18
  @session.visit('/form')
19
- @xpath = XPath::HTML.fillable_field('Name')
19
+ @xpath = Capybara::Selector.all[:fillable_field].call('Name')
20
+ expect(@xpath).to be_a(::XPath::Union)
20
21
  @result = @session.all(@xpath).map { |r| r.value }
21
22
  expect(@result).to include('Smith', 'John', 'John Smith')
22
23
  end
@@ -66,6 +67,17 @@ Capybara::SpecHelper.spec "#all" do
66
67
  Capybara.ignore_hidden_elements = false
67
68
  expect(@session.all(:css, "a.simple").size).to eq(2)
68
69
  end
70
+
71
+ context "with per session config", requires: [:psc] do
72
+ it "should use the sessions ignore_hidden_elements", psc: true do
73
+ Capybara.ignore_hidden_elements = true
74
+ @session.config.ignore_hidden_elements = false
75
+ expect(Capybara.ignore_hidden_elements).to eq(true)
76
+ expect(@session.all(:css, "a.simple").size).to eq(2)
77
+ @session.config.ignore_hidden_elements = true
78
+ expect(@session.all(:css, "a.simple").size).to eq(1)
79
+ end
80
+ end
69
81
  end
70
82
 
71
83
  context 'with element count filters' do
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+ Capybara::SpecHelper.spec '#ancestor' 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 ancestor element using the given locator" do
12
+ el = @session.find(:css, '#first_image')
13
+ expect(el.ancestor('//p')).to have_text('Lorem ipsum dolor')
14
+ expect(el.ancestor("//a")[:'aria-label']).to eq('Go to simple')
15
+ end
16
+
17
+ it "should find the ancestor element using the given locator and options" do
18
+ el = @session.find(:css, '#child')
19
+ expect(el.ancestor('//div', text: 'Ancestor Ancestor Ancestor')[:id]).to eq('ancestor3')
20
+ end
21
+
22
+ it "should raise an error if there are multiple matches" do
23
+ el = @session.find(:css, '#child')
24
+ expect { el.ancestor('//div') }.to raise_error(Capybara::Ambiguous)
25
+ expect { el.ancestor('//div', text: 'Ancestor') }.to raise_error(Capybara::Ambiguous)
26
+ end
27
+
28
+ context "with css selectors" do
29
+ it "should find the first element using the given locator" do
30
+ el = @session.find(:css, '#first_image')
31
+ expect(el.ancestor(:css, 'p')).to have_text('Lorem ipsum dolor')
32
+ expect(el.ancestor(:css, 'a')[:'aria-label']).to eq('Go to simple')
33
+ end
34
+
35
+ it "should support pseudo selectors" do
36
+ el = @session.find(:css, '#button_img')
37
+ expect(el.ancestor(:css, 'button:disabled')[:id]).to eq('ancestor_button')
38
+ end
39
+ end
40
+
41
+ context "with xpath selectors" do
42
+ it "should find the first element using the given locator" do
43
+ el = @session.find(:css, '#first_image')
44
+ expect(el.ancestor(:xpath, '//p')).to have_text('Lorem ipsum dolor')
45
+ expect(el.ancestor(:xpath, "//a")[:'aria-label']).to eq('Go to simple')
46
+ end
47
+ end
48
+
49
+ context "with custom selector" do
50
+ it "should use the custom selector" do
51
+ Capybara.add_selector(:level) do
52
+ xpath { |num| ".//*[@id='ancestor#{num}']" }
53
+ end
54
+ el = @session.find(:css, '#child')
55
+ expect(el.ancestor(:level, 1).text).to eq('Ancestor Child')
56
+ expect(el.ancestor(:level, 3).text).to eq('Ancestor Ancestor Ancestor Child')
57
+ end
58
+ end
59
+
60
+
61
+ it "should raise ElementNotFound with a useful default message if nothing was found" do
62
+ el = @session.find(:css, '#child')
63
+ expect do
64
+ el.ancestor(:xpath, '//div[@id="nosuchthing"]')
65
+ end.to raise_error(Capybara::ElementNotFound, "Unable to find xpath \"//div[@id=\\\"nosuchthing\\\"]\" that is an ancestor of visible css \"#child\"")
66
+ end
67
+
68
+
69
+
70
+ context "within a scope" do
71
+ it "should limit the ancestors to inside the scope" do
72
+ @session.within(:css, '#ancestor2') do
73
+ el = @session.find(:css, '#child')
74
+ expect(el.ancestor(:css,'div', text: 'Ancestor')[:id]).to eq('ancestor1')
75
+ end
76
+ end
77
+ end
78
+
79
+ it "should raise if selector type is unknown" do
80
+ el = @session.find(:css, '#child')
81
+ expect do
82
+ el.ancestor(:unknown, '//h1')
83
+ end.to raise_error(ArgumentError)
84
+ end
85
+ end
@@ -18,10 +18,18 @@ Capybara::SpecHelper.spec '#assert_all_of_selectors' do
18
18
  @session.assert_all_of_selectors("p a#foo", "h2#h2two", "h2#h2one" )
19
19
  end
20
20
 
21
- it "should respect scopes" do
22
- @session.within "//p[@id='first']" do
23
- @session.assert_all_of_selectors(".//a[@id='foo']")
24
- expect { @session.assert_all_of_selectors(".//a[@id='red']") }.to raise_error(Capybara::ElementNotFound)
21
+ context "should respect scopes" do
22
+ it "when used with `within`" do
23
+ @session.within "//p[@id='first']" do
24
+ @session.assert_all_of_selectors(".//a[@id='foo']")
25
+ expect { @session.assert_all_of_selectors(".//a[@id='red']") }.to raise_error(Capybara::ElementNotFound)
26
+ end
27
+ end
28
+
29
+ it "when called on elements" do
30
+ el = @session.find "//p[@id='first']"
31
+ el.assert_all_of_selectors(".//a[@id='foo']")
32
+ expect { el.assert_all_of_selectors(".//a[@id='red']") }.to raise_error(Capybara::ElementNotFound)
25
33
  end
26
34
  end
27
35
 
@@ -65,10 +73,18 @@ Capybara::SpecHelper.spec '#assert_none_of_selectors' do
65
73
  expect { @session.assert_none_of_selectors("abbr", "p a#foo") }.to raise_error(Capybara::ElementNotFound)
66
74
  end
67
75
 
68
- it "should respect scopes" do
69
- @session.within "//p[@id='first']" do
70
- expect { @session.assert_none_of_selectors(".//a[@id='foo']") }.to raise_error(Capybara::ElementNotFound)
71
- @session.assert_none_of_selectors(".//a[@id='red']")
76
+ context "should respect scopes" do
77
+ it "when used with `within`" do
78
+ @session.within "//p[@id='first']" do
79
+ expect { @session.assert_none_of_selectors(".//a[@id='foo']") }.to raise_error(Capybara::ElementNotFound)
80
+ @session.assert_none_of_selectors(".//a[@id='red']")
81
+ end
82
+ end
83
+
84
+ it "when called on an element" do
85
+ el = @session.find "//p[@id='first']"
86
+ expect { el.assert_none_of_selectors(".//a[@id='foo']") }.to raise_error(Capybara::ElementNotFound)
87
+ el.assert_none_of_selectors(".//a[@id='red']")
72
88
  end
73
89
  end
74
90
 
@@ -65,7 +65,7 @@ Capybara::SpecHelper.spec '#assert_selector' do
65
65
  Capybara.using_wait_time(0.1) do
66
66
  @session.visit('/with_js')
67
67
  @session.click_link('Click me')
68
- @session.assert_selector(:css, "a#has-been-clicked", text: "Has been clicked", wait: 0.9)
68
+ @session.assert_selector(:css, "a#has-been-clicked", text: "Has been clicked", wait: 2)
69
69
  end
70
70
  end
71
71
  end
@@ -52,6 +52,14 @@ Capybara::SpecHelper.spec '#assert_text' do
52
52
  end.to raise_error(Capybara::ExpectationNotMet, /it was found 1 time using a case insensitive search/)
53
53
  end
54
54
 
55
+ it "should raise error with helpful message if requested text is present but invisible and with incorrect case", requires: [:js] do
56
+ @session.visit('/with_html')
57
+ el = @session.find(:css, '#uppercase')
58
+ expect do
59
+ el.assert_text('text here')
60
+ end.to raise_error(Capybara::ExpectationNotMet, /it was found 1 time using a case insensitive search and it was found 1 time including non-visible text/)
61
+ end
62
+
55
63
  it "should raise the correct error if requested text is missing but contains regex special characters" do
56
64
  @session.visit('/with_html')
57
65
  expect do
@@ -4,16 +4,23 @@ Capybara::SpecHelper.spec '#assert_title' do
4
4
  @session.visit('/with_js')
5
5
  end
6
6
 
7
- it "should be true if the page's title contains the given string" do
8
- expect(@session.assert_title('js')).to eq(true)
7
+ it "should not raise if the page's title contains the given string" do
8
+ expect do
9
+ @session.assert_title('js')
10
+ end.not_to raise_error
9
11
  end
10
12
 
11
- it "should be true when given an empty string" do
12
- expect(@session.assert_title('')).to eq(true)
13
+ it "should not raise when given an empty string" do
14
+ expect do
15
+ @session.assert_title('')
16
+ end.not_to raise_error
13
17
  end
14
18
 
15
19
  it "should allow regexp matches" do
16
- expect(@session.assert_title(/w[a-z]{3}_js/)).to eq(true)
20
+ expect do
21
+ @session.assert_title(/w[a-z]{3}_js/)
22
+ end.not_to raise_error
23
+
17
24
  expect do
18
25
  @session.assert_title(/w[a-z]{10}_js/)
19
26
  end.to raise_error(Capybara::ExpectationNotMet, 'expected "with_js" to match /w[a-z]{10}_js/')
@@ -21,7 +28,9 @@ Capybara::SpecHelper.spec '#assert_title' do
21
28
 
22
29
  it "should wait for title", requires: [:js] do
23
30
  @session.click_link("Change title")
24
- expect(@session.assert_title("changed title")).to eq(true)
31
+ expect do
32
+ @session.assert_title("changed title", wait: 3)
33
+ end.not_to raise_error
25
34
  end
26
35
 
27
36
  it "should raise error if the title doesn't contain the given string" do
@@ -61,10 +70,14 @@ Capybara::SpecHelper.spec '#assert_no_title' do
61
70
 
62
71
  it "should wait for title to disappear", requires: [:js] do
63
72
  @session.click_link("Change title")
64
- expect(@session.assert_no_title('with_js')).to eq(true)
73
+ expect do
74
+ @session.assert_no_title('with_js', wait: 3)
75
+ end.not_to raise_error
65
76
  end
66
77
 
67
- it "should be true if the title doesn't contain the given string" do
68
- expect(@session.assert_no_title('monkey')).to eq(true)
78
+ it "should not raise if the title doesn't contain the given string" do
79
+ expect do
80
+ @session.assert_no_title('monkey')
81
+ end.not_to raise_error
69
82
  end
70
83
  end
@@ -4,6 +4,7 @@ Capybara::SpecHelper.spec "#attach_file" do
4
4
  @test_file_path = File.expand_path('../fixtures/test_file.txt', File.dirname(__FILE__))
5
5
  @another_test_file_path = File.expand_path('../fixtures/another_test_file.txt', File.dirname(__FILE__))
6
6
  @test_jpg_file_path = File.expand_path('../fixtures/capybara.jpg', File.dirname(__FILE__))
7
+ @no_extension_file_path = File.expand_path('../fixtures/no_extension', File.dirname(__FILE__))
7
8
  @session.visit('/form')
8
9
  end
9
10
 
@@ -57,6 +58,12 @@ Capybara::SpecHelper.spec "#attach_file" do
57
58
  expect(@session).to have_content('image/jpeg')
58
59
  end
59
60
 
61
+ it "should not break when uploading a file without extension" do
62
+ @session.attach_file "Single Document", @no_extension_file_path
63
+ @session.click_button 'Upload Single'
64
+ expect(@session).to have_content(File.read(@no_extension_file_path))
65
+ end
66
+
60
67
  it "should not break when using HTML5 multiple file input" do
61
68
  @session.attach_file "Multiple Documents", @test_file_path
62
69
  @session.click_button('Upload Multiple')
@@ -81,7 +88,7 @@ Capybara::SpecHelper.spec "#attach_file" do
81
88
 
82
89
  context "with a locator that doesn't exist" do
83
90
  it "should raise an error" do
84
- msg = "Unable to find file field \"does not exist\""
91
+ msg = "Unable to find visible file field \"does not exist\" that is not disabled"
85
92
  expect do
86
93
  @session.attach_file('does not exist', @test_file_path)
87
94
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -69,7 +69,7 @@ Capybara::SpecHelper.spec "#check" do
69
69
 
70
70
  context "with a locator that doesn't exist" do
71
71
  it "should raise an error" do
72
- msg = "Unable to find checkbox \"does not exist\""
72
+ msg = "Unable to find visible checkbox \"does not exist\" that is not disabled"
73
73
  expect do
74
74
  @session.check('does not exist')
75
75
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -142,11 +142,11 @@ Capybara::SpecHelper.spec "#check" do
142
142
  end
143
143
 
144
144
  it "should raise original error when no label available" do
145
- expect { @session.check('form_cars_ariel') }.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_ariel"')
145
+ expect { @session.check('form_cars_ariel') }.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_ariel" that is not disabled')
146
146
  end
147
147
 
148
148
  it "should raise error if not allowed to click label" do
149
- expect{@session.check('form_cars_mclaren', allow_label_click: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_mclaren"')
149
+ expect{@session.check('form_cars_mclaren', allow_label_click: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_mclaren" that is not disabled')
150
150
  end
151
151
  end
152
152
 
@@ -158,7 +158,7 @@ Capybara::SpecHelper.spec "#check" do
158
158
  end
159
159
 
160
160
  it "should raise error if checkbox not visible" do
161
- expect{@session.check('form_cars_mclaren')}.to raise_error(Capybara::ElementNotFound, 'Unable to find checkbox "form_cars_mclaren"')
161
+ expect{@session.check('form_cars_mclaren')}.to raise_error(Capybara::ElementNotFound, 'Unable to find visible checkbox "form_cars_mclaren" that is not disabled')
162
162
  end
163
163
 
164
164
  context "with allow_label_click == true" do
@@ -24,7 +24,7 @@ Capybara::SpecHelper.spec "#choose" do
24
24
 
25
25
  context "with a locator that doesn't exist" do
26
26
  it "should raise an error" do
27
- msg = "Unable to find radio button \"does not exist\""
27
+ msg = "Unable to find visible radio button \"does not exist\" that is not disabled"
28
28
  expect do
29
29
  @session.choose('does not exist')
30
30
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -82,7 +82,7 @@ Capybara::SpecHelper.spec "#choose" do
82
82
  end
83
83
 
84
84
  it "should raise error if not allowed to click label" do
85
- expect{@session.choose("party_democrat", allow_label_click: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find radio button "party_democrat"')
85
+ expect{@session.choose("party_democrat", allow_label_click: false)}.to raise_error(Capybara::ElementNotFound, 'Unable to find visible radio button "party_democrat" that is not disabled')
86
86
  end
87
87
  end
88
88
  end
@@ -353,7 +353,7 @@ Capybara::SpecHelper.spec '#click_button' do
353
353
 
354
354
  context "with a locator that doesn't exist" do
355
355
  it "should raise an error" do
356
- msg = "Unable to find button \"does not exist\""
356
+ msg = "Unable to find visible button \"does not exist\""
357
357
  expect do
358
358
  @session.click_button('does not exist')
359
359
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -54,7 +54,7 @@ Capybara::SpecHelper.spec '#click_link_or_button' do
54
54
  context "when `false`" do
55
55
  it "does not click on link which matches approximately" do
56
56
  @session.visit('/with_html')
57
- msg = "Unable to find link or button \"abore\""
57
+ msg = "Unable to find visible link or button \"abore\""
58
58
  expect do
59
59
  @session.click_link_or_button('abore', exact: true)
60
60
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -62,7 +62,7 @@ Capybara::SpecHelper.spec '#click_link_or_button' do
62
62
 
63
63
  it "does not click on approximately matching button" do
64
64
  @session.visit('/form')
65
- msg = "Unable to find link or button \"awe\""
65
+ msg = "Unable to find visible link or button \"awe\""
66
66
 
67
67
  expect do
68
68
  @session.click_link_or_button('awe', exact: true)
@@ -74,7 +74,7 @@ Capybara::SpecHelper.spec '#click_link_or_button' do
74
74
  context "with a locator that doesn't exist" do
75
75
  it "should raise an error" do
76
76
  @session.visit('/with_html')
77
- msg = "Unable to find link or button \"does not exist\""
77
+ msg = "Unable to find visible link or button \"does not exist\""
78
78
  expect do
79
79
  @session.click_link_or_button('does not exist')
80
80
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -67,7 +67,7 @@ Capybara::SpecHelper.spec '#click_link' do
67
67
 
68
68
  context "with a locator that doesn't exist" do
69
69
  it "should raise an error" do
70
- msg = "Unable to find link \"does not exist\""
70
+ msg = "Unable to find visible link \"does not exist\""
71
71
  expect do
72
72
  @session.click_link('does not exist')
73
73
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -3,7 +3,7 @@ require "capybara/spec/test_app"
3
3
 
4
4
  Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
5
5
  before :all do
6
- @servers = 2.times.map { Capybara::Server.new(TestApp.clone).boot }
6
+ @servers = 2.times.map { Capybara::Server.new(TestApp.new).boot }
7
7
  # sanity check
8
8
  expect(@servers[0].port).not_to eq(@servers[1].port)
9
9
  expect(@servers.map { |s| s.port }).not_to include 80
@@ -98,9 +98,9 @@ Capybara::SpecHelper.spec '#current_url, #current_path, #current_host' do
98
98
  end
99
99
 
100
100
  it "doesn't raise exception on a nil current_url" do
101
+ skip "Only makes sense when there is a real driver" unless @session.respond_to?(:driver)
102
+ allow(@session.driver).to receive(:current_url) { nil }
101
103
  @session.visit("/")
102
- allow_any_instance_of(Capybara::Session).to receive(:current_url) { nil }
103
-
104
104
  expect { @session.current_url }.not_to raise_exception
105
105
  expect { @session.current_path }.not_to raise_exception
106
106
  end
@@ -10,14 +10,14 @@ Capybara::SpecHelper.spec '#dismiss_confirm', requires: [:modals] do
10
10
  end
11
11
  expect(@session).to have_xpath("//a[@id='open-confirm' and @confirmed='false']")
12
12
  end
13
-
13
+
14
14
  it "should dismiss the confirm if the message matches" do
15
15
  @session.dismiss_confirm 'Confirm opened' do
16
16
  @session.click_link('Open confirm')
17
17
  end
18
18
  expect(@session).to have_xpath("//a[@id='open-confirm' and @confirmed='false']")
19
19
  end
20
-
20
+
21
21
  it "should not dismiss the confirm if the message doesn't match" do
22
22
  expect do
23
23
  @session.dismiss_confirm 'Incorrect Text' do
@@ -25,7 +25,7 @@ Capybara::SpecHelper.spec '#dismiss_confirm', requires: [:modals] do
25
25
  end
26
26
  end.to raise_error(Capybara::ModalNotFound)
27
27
  end
28
-
28
+
29
29
 
30
30
  it "should return the message presented" do
31
31
  message = @session.dismiss_confirm do
@@ -10,7 +10,7 @@ Capybara::SpecHelper.spec '#dismiss_prompt', requires: [:modals] do
10
10
  end
11
11
  expect(@session).to have_xpath("//a[@id='open-prompt' and @response='dismissed']")
12
12
  end
13
-
13
+
14
14
  it "should return the message presented" do
15
15
  message = @session.dismiss_prompt do
16
16
  @session.click_link('Open prompt')
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ Capybara::SpecHelper.spec "#evaluate_async_script", requires: [:js] do
3
+ it "should evaluate the given script and return whatever it produces" do
4
+ @session.visit('/with_js')
5
+ expect(@session.evaluate_async_script("arguments[0](4)")).to eq(4)
6
+ end
7
+
8
+ it "should support passing elements as arguments to the script", requires: [:js, :es_args] do
9
+ @session.visit('/with_js')
10
+ el = @session.find(:css, '#drag p')
11
+ result = @session.evaluate_async_script("arguments[2]([arguments[0].innerText, arguments[1]])", el, "Doodle Funk")
12
+ expect(result).to eq ["This is a draggable element.", "Doodle Funk"]
13
+ end
14
+
15
+ it "should support returning elements after asynchronous operation", requires: [:js, :es_args] do
16
+ @session.visit('/with_js')
17
+ @session.find(:css, '#change') # ensure page has loaded and element is available
18
+ el = @session.evaluate_async_script("var cb = arguments[0]; setTimeout(function(){ cb(document.getElementById('change')) }, 100)")
19
+ expect(el).to be_instance_of(Capybara::Node::Element)
20
+ expect(el).to eq(@session.find(:css, '#change'))
21
+ end
22
+ end
@@ -20,7 +20,7 @@ Capybara::SpecHelper.spec "#evaluate_script", requires: [:js] do
20
20
 
21
21
  it "should support returning elements", requires: [:js, :es_args] do
22
22
  @session.visit('/with_js')
23
- el = @session.find(:css, '#change')
23
+ @session.find(:css, '#change') # ensure page has loaded and element is available
24
24
  el = @session.evaluate_script("document.getElementById('change')")
25
25
  expect(el).to be_instance_of(Capybara::Node::Element)
26
26
  expect(el).to eq(@session.find(:css, '#change'))
@@ -106,6 +106,12 @@ Capybara::SpecHelper.spec "#fill_in" do
106
106
  expect(extract_results(@session)['first_name']).to eq('Thomas')
107
107
  end
108
108
 
109
+ it "should fill in a field based on type" do
110
+ @session.fill_in(type: 'schmooo', with: 'Schmooo for all')
111
+ @session.click_button('awesome')
112
+ expect(extract_results(@session)['schmooo']).to eq('Schmooo for all')
113
+ end
114
+
109
115
  it "should throw an exception if a hash containing 'with' is not provided" do
110
116
  expect {@session.fill_in 'Name', 'ignu'}.to raise_error(RuntimeError, /with/)
111
117
  end
@@ -150,7 +156,7 @@ Capybara::SpecHelper.spec "#fill_in" do
150
156
  before { Capybara.ignore_hidden_elements = true }
151
157
  after { Capybara.ignore_hidden_elements = false }
152
158
  it "should not find a hidden field" do
153
- msg = "Unable to find field \"Super Secret\""
159
+ msg = "Unable to find visible field \"Super Secret\" that is not disabled"
154
160
  expect do
155
161
  @session.fill_in('Super Secret', with: '777')
156
162
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -159,7 +165,7 @@ Capybara::SpecHelper.spec "#fill_in" do
159
165
 
160
166
  context "with a locator that doesn't exist" do
161
167
  it "should raise an error" do
162
- msg = "Unable to find field \"does not exist\""
168
+ msg = "Unable to find visible field \"does not exist\" that is not disabled"
163
169
  expect do
164
170
  @session.fill_in('does not exist', with: 'Blah blah')
165
171
  end.to raise_error(Capybara::ElementNotFound, msg)
@@ -8,6 +8,7 @@ Capybara::SpecHelper.spec '#find_field' do
8
8
  expect(@session.find_field('Dog').value).to eq('dog')
9
9
  expect(@session.find_field('form_description').text).to eq('Descriptive text goes here')
10
10
  expect(@session.find_field('Region')[:name]).to eq('form[region]')
11
+ expect(@session.find_field('With Asterisk*')).to be
11
12
  end
12
13
 
13
14
  context "aria_label attribute with Capybara.enable_aria_label" do
@@ -54,7 +54,7 @@ Capybara::SpecHelper.spec '#find' do
54
54
  it "should find element if it appears before given wait duration" do
55
55
  @session.visit('/with_js')
56
56
  @session.click_link('Click me')
57
- expect(@session.find(:css, "a#has-been-clicked", wait: 0.9).text).to include('Has been clicked')
57
+ expect(@session.find(:css, "a#has-been-clicked", wait: 2.0).text).to include('Has been clicked')
58
58
  end
59
59
  end
60
60
 
@@ -205,12 +205,13 @@ Capybara::SpecHelper.spec '#find' do
205
205
  it "should raise ElementNotFound with a useful default message if nothing was found" do
206
206
  expect do
207
207
  @session.find(:xpath, '//div[@id="nosuchthing"]').to be_nil
208
- end.to raise_error(Capybara::ElementNotFound, "Unable to find xpath \"//div[@id=\\\"nosuchthing\\\"]\"")
208
+ end.to raise_error(Capybara::ElementNotFound, "Unable to find visible xpath \"//div[@id=\\\"nosuchthing\\\"]\"")
209
209
  end
210
210
 
211
211
  it "should accept an XPath instance" do
212
212
  @session.visit('/form')
213
- @xpath = XPath::HTML.fillable_field('First Name')
213
+ @xpath = Capybara::Selector.all[:fillable_field].call('First Name')
214
+ expect(@xpath).to be_a(::XPath::Union)
214
215
  expect(@session.find(@xpath).value).to eq('John')
215
216
  end
216
217
 
@@ -421,8 +422,9 @@ Capybara::SpecHelper.spec '#find' do
421
422
  end
422
423
  end
423
424
 
424
- it "should warn if selector type is unknown" do
425
- expect_any_instance_of(Kernel).to receive(:warn).with(/^Unknown selector type/)
426
- @session.find(:unknown, '//h1')
425
+ it "should raise if selector type is unknown" do
426
+ expect do
427
+ @session.find(:unknown, '//h1')
428
+ end.to raise_error(ArgumentError)
427
429
  end
428
430
  end
@@ -15,7 +15,8 @@ Capybara::SpecHelper.spec '#first' do
15
15
 
16
16
  it "should accept an XPath instance" do
17
17
  @session.visit('/form')
18
- @xpath = XPath::HTML.fillable_field('First Name')
18
+ @xpath = Capybara::Selector.all[:fillable_field].call('First Name')
19
+ expect(@xpath).to be_a(::XPath::Union)
19
20
  expect(@session.first(@xpath).value).to eq('John')
20
21
  end
21
22
 
@@ -110,14 +111,18 @@ Capybara::SpecHelper.spec '#first' do
110
111
 
111
112
  it "should wait for at least one match if true" do
112
113
  Capybara.wait_on_first_by_default = true
113
- @session.click_link('clickable')
114
- expect(@session.first(:css, 'a#has-been-clicked')).not_to be_nil
114
+ Capybara.using_wait_time(3) do
115
+ @session.click_link('clickable')
116
+ expect(@session.first(:css, 'a#has-been-clicked')).not_to be_nil
117
+ end
115
118
  end
116
119
 
117
120
  it "should return nil after waiting if no match" do
118
121
  Capybara.wait_on_first_by_default = true
119
- @session.click_link('clickable')
120
- expect(@session.first(:css, 'a#not-a-real-link')).to be_nil
122
+ Capybara.using_wait_time(3) do
123
+ @session.click_link('clickable')
124
+ expect(@session.first(:css, 'a#not-a-real-link')).to be_nil
125
+ end
121
126
  end
122
127
  end
123
128
  end