capybara 2.2.1 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. checksums.yaml +4 -4
  2. data/History.md +26 -0
  3. data/README.md +36 -14
  4. data/lib/capybara.rb +6 -3
  5. data/lib/capybara/driver/base.rb +37 -1
  6. data/lib/capybara/driver/node.rb +10 -2
  7. data/lib/capybara/helpers.rb +21 -13
  8. data/lib/capybara/node/base.rb +12 -7
  9. data/lib/capybara/node/element.rb +17 -1
  10. data/lib/capybara/node/finders.rb +22 -1
  11. data/lib/capybara/node/matchers.rb +26 -5
  12. data/lib/capybara/node/simple.rb +9 -2
  13. data/lib/capybara/rack_test/css_handlers.rb +3 -1
  14. data/lib/capybara/rack_test/form.rb +3 -2
  15. data/lib/capybara/rack_test/node.rb +3 -3
  16. data/lib/capybara/rspec.rb +1 -0
  17. data/lib/capybara/rspec/features.rb +2 -1
  18. data/lib/capybara/rspec/matchers.rb +50 -5
  19. data/lib/capybara/selenium/driver.rb +76 -12
  20. data/lib/capybara/selenium/node.rb +8 -0
  21. data/lib/capybara/server.rb +1 -1
  22. data/lib/capybara/session.rb +234 -29
  23. data/lib/capybara/spec/public/jquery.js +1 -1
  24. data/lib/capybara/spec/public/test.js +7 -0
  25. data/lib/capybara/spec/session/all_spec.rb +88 -17
  26. data/lib/capybara/spec/session/assert_selector.rb +6 -0
  27. data/lib/capybara/spec/session/attach_file_spec.rb +15 -15
  28. data/lib/capybara/spec/session/body_spec.rb +4 -4
  29. data/lib/capybara/spec/session/check_spec.rb +16 -16
  30. data/lib/capybara/spec/session/choose_spec.rb +5 -5
  31. data/lib/capybara/spec/session/click_button_spec.rb +93 -84
  32. data/lib/capybara/spec/session/click_link_or_button_spec.rb +8 -8
  33. data/lib/capybara/spec/session/click_link_spec.rb +26 -19
  34. data/lib/capybara/spec/session/current_scope_spec.rb +3 -3
  35. data/lib/capybara/spec/session/current_url_spec.rb +8 -8
  36. data/lib/capybara/spec/session/evaluate_script_spec.rb +1 -1
  37. data/lib/capybara/spec/session/execute_script_spec.rb +2 -2
  38. data/lib/capybara/spec/session/fill_in_spec.rb +22 -22
  39. data/lib/capybara/spec/session/find_button_spec.rb +4 -4
  40. data/lib/capybara/spec/session/find_by_id_spec.rb +3 -3
  41. data/lib/capybara/spec/session/find_field_spec.rb +7 -7
  42. data/lib/capybara/spec/session/find_link_spec.rb +4 -4
  43. data/lib/capybara/spec/session/find_spec.rb +46 -46
  44. data/lib/capybara/spec/session/first_spec.rb +23 -23
  45. data/lib/capybara/spec/session/go_back_spec.rb +3 -3
  46. data/lib/capybara/spec/session/go_forward_spec.rb +4 -4
  47. data/lib/capybara/spec/session/has_button_spec.rb +13 -13
  48. data/lib/capybara/spec/session/has_css_spec.rb +87 -87
  49. data/lib/capybara/spec/session/has_field_spec.rb +87 -87
  50. data/lib/capybara/spec/session/has_link_spec.rb +11 -11
  51. data/lib/capybara/spec/session/has_select_spec.rb +58 -58
  52. data/lib/capybara/spec/session/has_selector_spec.rb +48 -48
  53. data/lib/capybara/spec/session/has_table_spec.rb +7 -7
  54. data/lib/capybara/spec/session/has_text_spec.rb +73 -73
  55. data/lib/capybara/spec/session/has_title_spec.rb +10 -10
  56. data/lib/capybara/spec/session/has_xpath_spec.rb +44 -44
  57. data/lib/capybara/spec/session/headers.rb +1 -1
  58. data/lib/capybara/spec/session/html_spec.rb +9 -9
  59. data/lib/capybara/spec/session/node_spec.rb +81 -65
  60. data/lib/capybara/spec/session/reset_session_spec.rb +15 -15
  61. data/lib/capybara/spec/session/response_code.rb +1 -1
  62. data/lib/capybara/spec/session/save_and_open_screenshot_spec.rb +46 -0
  63. data/lib/capybara/spec/session/save_page_spec.rb +9 -9
  64. data/lib/capybara/spec/session/{screenshot.rb → screenshot_spec.rb} +4 -2
  65. data/lib/capybara/spec/session/select_spec.rb +22 -22
  66. data/lib/capybara/spec/session/text_spec.rb +15 -10
  67. data/lib/capybara/spec/session/title_spec.rb +2 -2
  68. data/lib/capybara/spec/session/uncheck_spec.rb +7 -7
  69. data/lib/capybara/spec/session/unselect_spec.rb +14 -14
  70. data/lib/capybara/spec/session/visit_spec.rb +24 -17
  71. data/lib/capybara/spec/session/window/become_closed_spec.rb +84 -0
  72. data/lib/capybara/spec/session/window/current_window_spec.rb +25 -0
  73. data/lib/capybara/spec/session/window/open_new_window_spec.rb +28 -0
  74. data/lib/capybara/spec/session/window/switch_to_window_spec.rb +114 -0
  75. data/lib/capybara/spec/session/window/window_opened_by_spec.rb +83 -0
  76. data/lib/capybara/spec/session/window/window_spec.rb +141 -0
  77. data/lib/capybara/spec/session/window/windows_spec.rb +31 -0
  78. data/lib/capybara/spec/session/window/within_window_spec.rb +188 -0
  79. data/lib/capybara/spec/session/within_frame_spec.rb +9 -9
  80. data/lib/capybara/spec/session/within_spec.rb +16 -16
  81. data/lib/capybara/spec/spec_helper.rb +14 -4
  82. data/lib/capybara/spec/views/form.erb +7 -0
  83. data/lib/capybara/spec/views/popup_one.erb +1 -1
  84. data/lib/capybara/spec/views/popup_two.erb +1 -1
  85. data/lib/capybara/spec/views/with_js.erb +2 -0
  86. data/lib/capybara/spec/views/with_windows.erb +38 -0
  87. data/lib/capybara/version.rb +1 -1
  88. data/lib/capybara/window.rb +123 -0
  89. data/spec/basic_node_spec.rb +32 -32
  90. data/spec/capybara_spec.rb +6 -7
  91. data/spec/dsl_spec.rb +48 -48
  92. data/spec/fixtures/selenium_driver_rspec_failure.rb +2 -2
  93. data/spec/fixtures/selenium_driver_rspec_success.rb +2 -2
  94. data/spec/rack_test_spec.rb +33 -19
  95. data/spec/result_spec.rb +13 -13
  96. data/spec/rspec/features_spec.rb +20 -15
  97. data/spec/rspec/matchers_spec.rb +109 -109
  98. data/spec/rspec_spec.rb +10 -10
  99. data/spec/selenium_spec.rb +31 -6
  100. data/spec/selenium_spec_chrome.rb +2 -2
  101. data/spec/server_spec.rb +13 -13
  102. metadata +51 -62
  103. checksums.yaml.gz.sig +0 -0
  104. data.tar.gz.sig +0 -0
  105. data/lib/capybara/spec/session/within_window_spec.rb +0 -45
  106. data/lib/capybara/spec/views/within_popups.erb +0 -25
  107. metadata.gz.sig +0 -0
@@ -6,29 +6,29 @@ Capybara::SpecHelper.spec "#uncheck" do
6
6
  it "should uncheck a checkbox by id" do
7
7
  @session.uncheck("form_pets_hamster")
8
8
  @session.click_button('awesome')
9
- extract_results(@session)['pets'].should include('dog')
10
- extract_results(@session)['pets'].should_not include('hamster')
9
+ expect(extract_results(@session)['pets']).to include('dog')
10
+ expect(extract_results(@session)['pets']).not_to include('hamster')
11
11
  end
12
12
 
13
13
  it "should uncheck a checkbox by label" do
14
14
  @session.uncheck("Hamster")
15
15
  @session.click_button('awesome')
16
- extract_results(@session)['pets'].should include('dog')
17
- extract_results(@session)['pets'].should_not include('hamster')
16
+ expect(extract_results(@session)['pets']).to include('dog')
17
+ expect(extract_results(@session)['pets']).not_to include('hamster')
18
18
  end
19
19
 
20
20
  it "casts to string" do
21
21
  @session.uncheck(:"form_pets_hamster")
22
22
  @session.click_button('awesome')
23
- extract_results(@session)['pets'].should include('dog')
24
- extract_results(@session)['pets'].should_not include('hamster')
23
+ expect(extract_results(@session)['pets']).to include('dog')
24
+ expect(extract_results(@session)['pets']).not_to include('hamster')
25
25
  end
26
26
 
27
27
  context "with :exact option" do
28
28
  it "should accept partial matches when false" do
29
29
  @session.uncheck('Ham', :exact => false)
30
30
  @session.click_button('awesome')
31
- extract_results(@session)['pets'].should_not include('hamster')
31
+ expect(extract_results(@session)['pets']).not_to include('hamster')
32
32
  end
33
33
 
34
34
  it "should not accept partial matches when true" do
@@ -7,42 +7,42 @@ Capybara::SpecHelper.spec "#unselect" do
7
7
  it "should unselect an option from a select box by id" do
8
8
  @session.unselect('Commando', :from => 'form_underwear')
9
9
  @session.click_button('awesome')
10
- extract_results(@session)['underwear'].should include('Briefs', 'Boxerbriefs')
11
- extract_results(@session)['underwear'].should_not include('Commando')
10
+ expect(extract_results(@session)['underwear']).to include('Briefs', 'Boxerbriefs')
11
+ expect(extract_results(@session)['underwear']).not_to include('Commando')
12
12
  end
13
13
 
14
14
  it "should unselect an option without a select box" do
15
15
  @session.unselect('Commando')
16
16
  @session.click_button('awesome')
17
- extract_results(@session)['underwear'].should include('Briefs', 'Boxerbriefs')
18
- extract_results(@session)['underwear'].should_not include('Commando')
17
+ expect(extract_results(@session)['underwear']).to include('Briefs', 'Boxerbriefs')
18
+ expect(extract_results(@session)['underwear']).not_to include('Commando')
19
19
  end
20
20
 
21
21
  it "should unselect an option from a select box by label" do
22
22
  @session.unselect('Commando', :from => 'Underwear')
23
23
  @session.click_button('awesome')
24
- extract_results(@session)['underwear'].should include('Briefs', 'Boxerbriefs')
25
- extract_results(@session)['underwear'].should_not include('Commando')
24
+ expect(extract_results(@session)['underwear']).to include('Briefs', 'Boxerbriefs')
25
+ expect(extract_results(@session)['underwear']).not_to include('Commando')
26
26
  end
27
27
 
28
28
  it "should favour exact matches to option labels" do
29
29
  @session.unselect("Briefs", :from => 'Underwear')
30
30
  @session.click_button('awesome')
31
- extract_results(@session)['underwear'].should include('Commando', 'Boxerbriefs')
32
- extract_results(@session)['underwear'].should_not include('Briefs')
31
+ expect(extract_results(@session)['underwear']).to include('Commando', 'Boxerbriefs')
32
+ expect(extract_results(@session)['underwear']).not_to include('Briefs')
33
33
  end
34
34
 
35
35
  it "should escape quotes" do
36
36
  @session.unselect("Frenchman's Pantalons", :from => 'Underwear')
37
37
  @session.click_button('awesome')
38
- extract_results(@session)['underwear'].should_not include("Frenchman's Pantalons")
38
+ expect(extract_results(@session)['underwear']).not_to include("Frenchman's Pantalons")
39
39
  end
40
40
 
41
41
  it "casts to string" do
42
42
  @session.unselect(:"Briefs", :from => :'Underwear')
43
43
  @session.click_button('awesome')
44
- extract_results(@session)['underwear'].should include('Commando', 'Boxerbriefs')
45
- extract_results(@session)['underwear'].should_not include('Briefs')
44
+ expect(extract_results(@session)['underwear']).to include('Commando', 'Boxerbriefs')
45
+ expect(extract_results(@session)['underwear']).not_to include('Briefs')
46
46
  end
47
47
  end
48
48
 
@@ -75,19 +75,19 @@ Capybara::SpecHelper.spec "#unselect" do
75
75
  it "can match select box approximately" do
76
76
  @session.unselect("Boxerbriefs", :from => "Under", :exact => false)
77
77
  @session.click_button("awesome")
78
- extract_results(@session)["underwear"].should_not include("Boxerbriefs")
78
+ expect(extract_results(@session)["underwear"]).not_to include("Boxerbriefs")
79
79
  end
80
80
 
81
81
  it "can match option approximately" do
82
82
  @session.unselect("Boxerbr", :from => "Underwear", :exact => false)
83
83
  @session.click_button("awesome")
84
- extract_results(@session)["underwear"].should_not include("Boxerbriefs")
84
+ expect(extract_results(@session)["underwear"]).not_to include("Boxerbriefs")
85
85
  end
86
86
 
87
87
  it "can match option approximately when :from not given" do
88
88
  @session.unselect("Boxerbr", :exact => false)
89
89
  @session.click_button("awesome")
90
- extract_results(@session)["underwear"].should_not include("Boxerbriefs")
90
+ expect(extract_results(@session)["underwear"]).not_to include("Boxerbriefs")
91
91
  end
92
92
  end
93
93
 
@@ -1,9 +1,9 @@
1
1
  Capybara::SpecHelper.spec '#visit' do
2
2
  it "should fetch a response from the driver with a relative url" do
3
3
  @session.visit('/')
4
- @session.should have_content('Hello world!')
4
+ expect(@session).to have_content('Hello world!')
5
5
  @session.visit('/foo')
6
- @session.should have_content('Another World')
6
+ expect(@session).to have_content('Another World')
7
7
  end
8
8
 
9
9
  it "should fetch a response from the driver with an absolute url with a port" do
@@ -12,9 +12,9 @@ Capybara::SpecHelper.spec '#visit' do
12
12
  root_uri = URI.parse(@session.current_url)
13
13
 
14
14
  @session.visit("http://#{root_uri.host}:#{root_uri.port}/")
15
- @session.should have_content('Hello world!')
15
+ expect(@session).to have_content('Hello world!')
16
16
  @session.visit("http://#{root_uri.host}:#{root_uri.port}/foo")
17
- @session.should have_content('Another World')
17
+ expect(@session).to have_content('Another World')
18
18
  end
19
19
 
20
20
  it "should fetch a response when absolute URI doesn't have a trailing slash" do
@@ -23,7 +23,14 @@ Capybara::SpecHelper.spec '#visit' do
23
23
  root_uri = URI.parse(@session.current_url)
24
24
 
25
25
  @session.visit("http://localhost:#{root_uri.port}")
26
- @session.should have_content('Hello world!')
26
+ expect(@session).to have_content('Hello world!')
27
+ end
28
+
29
+ it "raises any errors caught inside the server", :requires => [:server] do
30
+ quietly { @session.visit("/error") }
31
+ expect do
32
+ @session.visit("/")
33
+ end.to raise_error(TestApp::TestAppError)
27
34
  end
28
35
 
29
36
  context "when Capybara.always_include_port is true" do
@@ -43,12 +50,12 @@ Capybara::SpecHelper.spec '#visit' do
43
50
 
44
51
  it "should fetch a response from the driver with an absolute url without a port" do
45
52
  @session.visit("http://#{root_uri.host}/")
46
- URI.parse(@session.current_url).port.should == root_uri.port
47
- @session.should have_content('Hello world!')
53
+ expect(URI.parse(@session.current_url).port).to eq(root_uri.port)
54
+ expect(@session).to have_content('Hello world!')
48
55
 
49
56
  @session.visit("http://#{root_uri.host}/foo")
50
- URI.parse(@session.current_url).port.should == root_uri.port
51
- @session.should have_content('Another World')
57
+ expect(URI.parse(@session.current_url).port).to eq(root_uri.port)
58
+ expect(@session).to have_content('Another World')
52
59
  end
53
60
  end
54
61
 
@@ -57,49 +64,49 @@ Capybara::SpecHelper.spec '#visit' do
57
64
  serverless_session = Capybara::Session.new(@session.mode, nil)
58
65
  Capybara.app_host = "http://#{@session.server.host}:#{@session.server.port}"
59
66
  serverless_session.visit("/foo")
60
- serverless_session.should have_content("Another World")
67
+ expect(serverless_session).to have_content("Another World")
61
68
  end
62
69
 
63
70
  it "should visit a fully qualified URL" do
64
71
  serverless_session = Capybara::Session.new(@session.mode, nil)
65
72
  serverless_session.visit("http://#{@session.server.host}:#{@session.server.port}/foo")
66
- serverless_session.should have_content("Another World")
73
+ expect(serverless_session).to have_content("Another World")
67
74
  end
68
75
  end
69
76
 
70
77
  it "should send no referer when visiting a page" do
71
78
  @session.visit '/get_referer'
72
- @session.should have_content 'No referer'
79
+ expect(@session).to have_content 'No referer'
73
80
  end
74
81
 
75
82
  it "should send no referer when visiting a second page" do
76
83
  @session.visit '/get_referer'
77
84
  @session.visit '/get_referer'
78
- @session.should have_content 'No referer'
85
+ expect(@session).to have_content 'No referer'
79
86
  end
80
87
 
81
88
  it "should send a referer when following a link" do
82
89
  @session.visit '/referer_base'
83
90
  @session.find('//a[@href="/get_referer"]').click
84
- @session.should have_content %r{http://.*/referer_base}
91
+ expect(@session).to have_content %r{http://.*/referer_base}
85
92
  end
86
93
 
87
94
  it "should preserve the original referer URL when following a redirect" do
88
95
  @session.visit('/referer_base')
89
96
  @session.find('//a[@href="/redirect_to_get_referer"]').click
90
- @session.should have_content %r{http://.*/referer_base}
97
+ expect(@session).to have_content %r{http://.*/referer_base}
91
98
  end
92
99
 
93
100
  it "should send a referer when submitting a form" do
94
101
  @session.visit '/referer_base'
95
102
  @session.find('//input').click
96
- @session.should have_content %r{http://.*/referer_base}
103
+ expect(@session).to have_content %r{http://.*/referer_base}
97
104
  end
98
105
 
99
106
  it "can set cookie if a blank path is specified" do
100
107
  @session.visit("")
101
108
  @session.visit('/get_cookie')
102
- @session.should have_content('root cookie')
109
+ expect(@session).to have_content('root cookie')
103
110
  end
104
111
 
105
112
  end
@@ -0,0 +1,84 @@
1
+ Capybara::SpecHelper.spec '#become_closed', requires: [:windows, :js] do
2
+ before(:each) do
3
+ @window = @session.current_window
4
+ @session.visit('/with_windows')
5
+ @other_window = @session.window_opened_by do
6
+ @session.find(:css, '#openWindow').click
7
+ end
8
+ end
9
+
10
+ after(:each) do
11
+ @session.document.synchronize(3, errors: [Capybara::CapybaraError]) do
12
+ raise Capybara::CapybaraError if @session.windows.size != 1
13
+ end
14
+ @session.switch_to_window(@window)
15
+ end
16
+
17
+ context 'with :wait option' do
18
+ it 'should wait if value of :wait is more than timeout' do
19
+ @session.within_window @other_window do
20
+ @session.execute_script('setTimeout(function(){ window.close(); }, 500);')
21
+ end
22
+ Capybara.using_wait_time 0.1 do
23
+ expect(@other_window).to become_closed(wait: 0.7)
24
+ end
25
+ end
26
+
27
+ it 'should raise error if value of :wait is less than timeout' do
28
+ @session.within_window @other_window do
29
+ @session.execute_script('setTimeout(function(){ window.close(); }, 700);')
30
+ end
31
+ Capybara.using_wait_time 2 do
32
+ expect do
33
+ expect(@other_window).to become_closed(wait: 0.4)
34
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> to become closed after 0.4 seconds\Z/)
35
+ end
36
+ end
37
+ end
38
+
39
+ context 'without :wait option' do
40
+ it 'should wait if value of default_wait_time is more than timeout' do
41
+ @session.within_window @other_window do
42
+ @session.execute_script('setTimeout(function(){ window.close(); }, 500);')
43
+ end
44
+ Capybara.using_wait_time 0.7 do
45
+ expect(@other_window).to become_closed
46
+ end
47
+ end
48
+
49
+ it 'should raise error if value of default_wait_time is less than timeout' do
50
+ @session.within_window @other_window do
51
+ @session.execute_script('setTimeout(function(){ window.close(); }, 900);')
52
+ end
53
+ Capybara.using_wait_time 0.4 do
54
+ expect do
55
+ expect(@other_window).to become_closed
56
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> to become closed after 0.4 seconds\Z/)
57
+ end
58
+ end
59
+ end
60
+
61
+ context 'with not_to' do
62
+ it 'should raise error if default_wait_time is more than timeout' do
63
+ @session.within_window @other_window do
64
+ @session.execute_script('setTimeout(function(){ window.close(); }, 700);')
65
+ end
66
+ Capybara.using_wait_time 0.4 do
67
+ expect do
68
+ expect(@other_window).not_to become_closed
69
+ end
70
+ end
71
+ end
72
+
73
+ it 'should raise error if default_wait_time is more than timeout' do
74
+ @session.within_window @other_window do
75
+ @session.execute_script('setTimeout(function(){ window.close(); }, 700);')
76
+ end
77
+ Capybara.using_wait_time 1.1 do
78
+ expect do
79
+ expect(@other_window).not_to become_closed
80
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /\Aexpected #<Window @handle=".+"> not to become closed after 1.1 seconds\Z/)
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,25 @@
1
+ Capybara::SpecHelper.spec '#current_window', requires: [:windows] do
2
+ before(:each) do
3
+ @window = @session.current_window
4
+ @session.visit('/with_windows')
5
+ end
6
+ after(:each) do
7
+ (@session.windows - [@window]).each do |w|
8
+ @session.switch_to_window w
9
+ w.close
10
+ end
11
+ @session.switch_to_window(@window)
12
+ end
13
+
14
+ it 'should return window' do
15
+ expect(@session.current_window).to be_instance_of(Capybara::Window)
16
+ end
17
+
18
+ it "should be modified by switching to another window" do
19
+ window = @session.window_opened_by { @session.find(:css, '#openWindow').click }
20
+
21
+ expect do
22
+ @session.switch_to_window(window)
23
+ end.to change { @session.current_window }.from(@window).to(window)
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ Capybara::SpecHelper.spec '#open_new_window', requires: [:windows] do
2
+ before(:each) do
3
+ @window = @session.current_window
4
+ @session.visit('/with_windows')
5
+ end
6
+ after(:each) do
7
+ (@session.windows - [@window]).each do |w|
8
+ @session.switch_to_window w
9
+ w.close
10
+ end
11
+ @session.switch_to_window(@window)
12
+ end
13
+
14
+ it 'should open new window with blank url and title' do
15
+ window = @session.open_new_window
16
+ @session.switch_to_window(window)
17
+ expect(['', 'about:blank']).to include(@session.title)
18
+ expect(@session.current_url).to eq('about:blank')
19
+ end
20
+
21
+ it 'should open window with changeable content' do
22
+ window = @session.open_new_window
23
+ @session.within_window window do
24
+ @session.visit '/with_html'
25
+ expect(@session).to have_css('#first')
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,114 @@
1
+ Capybara::SpecHelper.spec '#switch_to_window', requires: [:windows] do
2
+ before(:each) do
3
+ @window = @session.current_window
4
+ @session.visit('/with_windows')
5
+ end
6
+ after(:each) do
7
+ (@session.windows - [@window]).each do |w|
8
+ @session.switch_to_window w
9
+ w.close
10
+ end
11
+ @session.switch_to_window(@window)
12
+ end
13
+
14
+ it "should raise error when invoked without args" do
15
+ expect do
16
+ @session.switch_to_window
17
+ end.to raise_error(ArgumentError, "`switch_to_window`: either window or block should be provided")
18
+ end
19
+
20
+ it "should raise error when invoked with window and block" do
21
+ expect do
22
+ @session.switch_to_window(@window) { @session.title == 'Title of the first popup' }
23
+ end.to raise_error(ArgumentError, "`switch_to_window` can take either a block or a window, not both")
24
+ end
25
+
26
+ context "with an instance of Capybara::Window" do
27
+ it "should be able to switch to window" do
28
+ window = @session.open_new_window
29
+ expect(@session.title).to eq('With Windows')
30
+ @session.switch_to_window(window)
31
+ expect(['', 'about:blank']).to include(@session.title)
32
+ end
33
+
34
+ it "should raise error when closed window is passed" do
35
+ original_window = @session.current_window
36
+ new_window = @session.open_new_window
37
+ @session.switch_to_window(new_window)
38
+ new_window.close
39
+ @session.switch_to_window(original_window)
40
+ expect do
41
+ @session.switch_to_window(new_window)
42
+ end.to raise_error(@session.driver.no_such_window_error)
43
+ end
44
+ end
45
+
46
+ context "with block" do
47
+ before(:each) do
48
+ @session.find(:css, '#openTwoWindows').click
49
+ end
50
+
51
+ it "should be able to switch to current window" do
52
+ @session.switch_to_window { @session.title == 'With Windows' }
53
+ expect(@session).to have_css('#openTwoWindows')
54
+ end
55
+
56
+ it "should find the div in another window" do
57
+ @session.switch_to_window { @session.title == 'Title of popup two' }
58
+ expect(@session).to have_css('#divInPopupTwo')
59
+ end
60
+
61
+ it "should be able to switch multiple times" do
62
+ @session.switch_to_window { @session.title == 'Title of the first popup' }
63
+ expect(@session).to have_css('#divInPopupOne')
64
+ @session.switch_to_window { @session.title == 'Title of popup two' }
65
+ expect(@session).to have_css('#divInPopupTwo')
66
+ end
67
+
68
+ it "should return window" do
69
+ window = @session.switch_to_window { @session.title == 'Title of popup two' }
70
+ expect((@session.windows - [@window])).to include(window)
71
+ end
72
+
73
+ it "should raise error when invoked inside `within` as it's nonsense" do
74
+ expect do
75
+ @session.within(:css, '#doesNotOpenWindows') do
76
+ @session.switch_to_window { @session.title == 'With Windows' }
77
+ end
78
+ end.to raise_error(Capybara::ScopeError, "`switch_to_window` is not supposed to be invoked from `within`'s, `within_frame`'s' or `within_window`'s' block.")
79
+ end
80
+
81
+ it "should raise error when invoked inside `within_frame` as it's nonsense" do
82
+ expect do
83
+ @session.within_frame('frameOne') do
84
+ @session.switch_to_window { @session.title == 'With Windows' }
85
+ end
86
+ end.to raise_error(Capybara::ScopeError, "`switch_to_window` is not supposed to be invoked from `within`'s, `within_frame`'s' or `within_window`'s' block.")
87
+ end
88
+
89
+ it "should raise error when invoked inside `within_window` as it's nonsense" do
90
+ window = (@session.windows - [@window]).first
91
+ expect do
92
+ @session.within_window window do
93
+ @session.switch_to_window { @session.title == 'With Windows' }
94
+ end
95
+ end.to raise_error(Capybara::ScopeError, "`switch_to_window` is not supposed to be invoked from `within`'s, `within_frame`'s' or `within_window`'s' block.")
96
+ end
97
+
98
+ it "should raise error if window matching block wasn't found" do
99
+ original = @session.current_window
100
+ expect do
101
+ @session.switch_to_window { @session.title == 'A title' }
102
+ end.to raise_error(Capybara::WindowError, "Could not find a window matching block/lambda")
103
+ expect(@session.current_window).to eq(original)
104
+ end
105
+
106
+ it "should switch to original window if error is raised inside block" do
107
+ original = @session.switch_to_window(@session.windows[1])
108
+ expect do
109
+ @session.switch_to_window { raise 'error' }
110
+ end.to raise_error(StandardError, 'error')
111
+ expect(@session.current_window).to eq(original)
112
+ end
113
+ end
114
+ end