forked from watir/watirspec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
after_hooks_spec.rb
164 lines (140 loc) · 5.68 KB
/
after_hooks_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require File.expand_path("../spec_helper", __FILE__)
describe "Browser::AfterHooks" do
describe "#add" do
it "raises ArgumentError when not given any arguments" do
expect { browser.after_hooks.add }.to raise_error(ArgumentError)
end
it "runs the given proc on each page load" do
output = ''
proc = Proc.new { |browser| output << browser.text }
begin
browser.after_hooks.add(proc)
browser.goto(WatirSpec.url_for("non_control_elements.html"))
expect(output).to include('Dubito, ergo cogito, ergo sum')
ensure
browser.after_hooks.delete(proc)
end
end
end
describe "#delete" do
it "removes a previously added after_hook" do
output = ''
after_hook = lambda{ |browser| output << browser.text }
browser.after_hooks.add(after_hook)
browser.goto(WatirSpec.url_for("non_control_elements.html"))
expect(output).to include('Dubito, ergo cogito, ergo sum')
browser.after_hooks.delete(after_hook)
browser.goto(WatirSpec.url_for("definition_lists.html"))
expect(output).to_not include('definition_lists')
end
end
describe "#run" do
after(:each) do
browser.window(index: 0).use
browser.alert.close if browser.alert.exists?
browser.after_hooks.delete @page_after_hook
end
it "runs after_hooks after Browser#goto" do
@page_after_hook = Proc.new { @yield = browser.title == "The font element" }
browser.after_hooks.add @page_after_hook
browser.goto WatirSpec.url_for("font.html")
expect(@yield).to be true
end
it "runs after_hooks after Browser#refresh" do
browser.goto WatirSpec.url_for("font.html")
@page_after_hook = Proc.new { @yield = browser.title == "The font element" }
browser.after_hooks.add @page_after_hook
browser.refresh
expect(@yield).to be true
end
it "runs after_hooks after Element#click" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
@page_after_hook = Proc.new { @yield = browser.title == "Non-control elements" }
browser.after_hooks.add @page_after_hook
browser.link(index: 1).click
expect(@yield).to be true
end
not_compliant_on %i(webdriver iphone) do
it "runs after_hooks after Element#double_click" do
browser.goto(WatirSpec.url_for("non_control_elements.html"))
@page_after_hook = Proc.new { @yield = browser.title == "Non-control elements" }
browser.after_hooks.add @page_after_hook
browser.div(id: 'html_test').double_click
expect(@yield).to be true
end
end
it "runs after_hooks after Element#right_click" do
browser.goto(WatirSpec.url_for("right_click.html"))
@page_after_hook = Proc.new { @yield = browser.title == "Right Click Test" }
browser.after_hooks.add @page_after_hook
browser.div(id: "click").right_click
expect(@yield).to be true
end
bug "https://github.com/detro/ghostdriver/issues/20", :phantomjs do
it "runs after_hooks after Alert#ok" do
browser.goto(WatirSpec.url_for("alerts.html"))
@page_after_hook = Proc.new { @yield = browser.title == "Alerts" }
browser.after_hooks.add @page_after_hook
browser.after_hooks.without do
not_compliant_on :watir_classic do
browser.button(id: 'alert').click
end
deviates_on :watir_classic do
browser.button(id: 'alert').click_no_wait
end
end
browser.alert.ok
expect(@yield).to be true
end
it "runs after_hooks after Alert#close" do
browser.goto(WatirSpec.url_for("alerts.html"))
@page_after_hook = Proc.new { @yield = browser.title == "Alerts" }
browser.after_hooks.add @page_after_hook
browser.after_hooks.without do
not_compliant_on :watir_classic do
browser.button(id: 'alert').click
end
deviates_on :watir_classic do
browser.button(id: 'alert').click_no_wait
end
end
browser.alert.close
expect(@yield).to be true
end
end
it "raises UnhandledAlertError error when running error checks with alert present" do
url = WatirSpec.url_for("alerts.html")
@page_after_hook = Proc.new { browser.url }
browser.after_hooks.add @page_after_hook
browser.goto url
expect { browser.button(id: "alert").click }.to raise_error(Selenium::WebDriver::Error::UnhandledAlertError)
end
it "does not raise error when running error checks using #after_hooks#without with alert present" do
url = WatirSpec.url_for("alerts.html")
@page_after_hook = Proc.new { browser.url }
browser.after_hooks.add @page_after_hook
browser.goto url
expect { browser.after_hooks.without {browser.button(id: "alert").click} }.to_not raise_error
end
it "does not raise error if no error checks are defined with alert present" do
url = WatirSpec.url_for("alerts.html")
@page_after_hook = Proc.new { browser.url }
browser.after_hooks.add @page_after_hook
browser.goto url
browser.after_hooks.delete @page_after_hook
expect { browser.button(id: "alert").click }.to_not raise_error
browser.alert.close
end
it "does not raise error when running error checks on closed window" do
url = WatirSpec.url_for("window_switching.html")
@page_after_hook = Proc.new { browser.url }
browser.after_hooks.add @page_after_hook
browser.goto url
browser.a(id: "open").click
window = browser.window(title: "closeable window")
window.use
expect { browser.a(id: "close").click }.to_not raise_error
browser.window(index: 0).use
end
end
end