-
-
Notifications
You must be signed in to change notification settings - Fork 144
/
Copy pathruntime_spec.rb
279 lines (230 loc) · 8.34 KB
/
runtime_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# frozen_string_literal: true
describe Ferrum::Frame::Runtime do
describe "#execute" do
it "executes multiple lines of javascript" do
browser.execute <<-JS
var a = 1
var b = 2
window.result = a + b
JS
expect(browser.evaluate("window.result")).to eq(3)
end
context "with javascript errors" do
let(:browser) { Ferrum::Browser.new(base_url: base_url, js_errors: true) }
it "propagates a Javascript error to a ruby exception" do
expect do
browser.execute(%(throw new Error("zomg")))
end.to raise_error(Ferrum::JavaScriptError) { |e|
expect(e.message).to include("Error: zomg")
}
end
it "propagates an asynchronous Javascript error on the page to a ruby exception" do
expect do
browser.execute "setTimeout(function() { omg }, 0)"
sleep 0.01
browser.execute ""
end.to raise_error(Ferrum::JavaScriptError, /ReferenceError.*omg/)
end
it "propagates a synchronous Javascript error on the page to a ruby exception" do
expect do
browser.execute "omg"
end.to raise_error(Ferrum::JavaScriptError, /ReferenceError.*omg/)
end
it "does not re-raise a Javascript error if it is rescued" do
expect do
browser.execute "setTimeout(function() { omg }, 0)"
sleep 0.01
browser.execute ""
end.to raise_error(Ferrum::JavaScriptError, /ReferenceError.*omg/)
# should not raise again
expect(browser.evaluate("1+1")).to eq(2)
end
it "propagates a Javascript error during page load to a ruby exception" do
expect { browser.go_to("/ferrum/js_error") }.to raise_error(Ferrum::JavaScriptError)
end
it "does not propagate a Javascript error to ruby if error raising disabled" do
browser = Ferrum::Browser.new(base_url: base_url, js_errors: false)
browser.go_to("/ferrum/js_error")
browser.execute "setTimeout(function() { omg }, 0)"
sleep 0.1
expect(browser.body).to include("hello")
ensure
browser&.quit
end
it "does not propagate a Javascript error to ruby if error raising disabled and client restarted" do
browser = Ferrum::Browser.new(base_url: base_url, js_errors: false)
browser.restart
browser.go_to("/ferrum/js_error")
browser.execute "setTimeout(function() { omg }, 0)"
sleep 0.1
expect(browser.body).to include("hello")
ensure
browser&.quit
end
end
end
describe "#evaluate" do
it "returns an element" do
browser.go_to("/ferrum/type")
element = browser.evaluate(%(document.getElementById("empty_input")))
expect(element).to eq(browser.at_css("#empty_input"))
end
it "returns structures with elements" do
browser.go_to("/ferrum/type")
result = browser.evaluate <<~JS
{
a: document.getElementById("empty_input"),
b: { c: document.querySelectorAll("#empty_textarea, #filled_textarea") }
}
JS
expect(result).to eq(
"a" => browser.at_css("#empty_input"),
"b" => {
"c" => browser.css("#empty_textarea, #filled_textarea")
}
)
end
it "returns values properly" do
expect(browser.evaluate("null")).to be_nil
expect(browser.evaluate("false")).to be false
expect(browser.evaluate("true")).to be true
expect(browser.evaluate("undefined")).to eq(nil)
expect(browser.evaluate("3;")).to eq(3)
expect(browser.evaluate("31337")).to eq(31_337)
expect(browser.evaluate(%("string"))).to eq("string")
expect(browser.evaluate(%({foo: "bar"}))).to eq("foo" => "bar")
expect(browser.evaluate("new Object")).to eq({})
expect(browser.evaluate("new Date(2012, 0).toDateString()")).to eq("Sun Jan 01 2012")
expect(browser.evaluate("new Object({a: 1})")).to eq({ "a" => 1 })
expect(browser.evaluate("new Array")).to eq([])
expect(browser.evaluate("new Function")).to eq({})
expect do
browser.evaluate(%(throw "smth"))
end.to raise_error(Ferrum::JavaScriptError)
end
context "when cyclic structure is returned" do
context "ignores seen" do
let(:code) do
<<~JS
(function() {
var a = {};
var b = {};
var c = {};
c.a = a;
a.a = a;
a.b = b;
a.c = c;
return %s;
})()
JS
end
it "returns object" do
expect(browser.evaluate(code % "a")).to eq(Ferrum::CyclicObject.instance)
end
it "returns array" do
expect(browser.evaluate(code % "[a]")).to eq([Ferrum::CyclicObject.instance])
end
end
it "backtracks what it has seen" do
expect(browser.evaluate("(function() { var a = {}; return [a, a] })()")).to eq([{}, {}])
end
end
end
describe "#evaluate_func" do
let(:function) do
<<~JS
function(c) {
let a = 1;
let b = 2;
return a + b + c;
}
JS
end
it "evaluates multiple lines of javascript function" do
expect(browser.evaluate_func(function, 3)).to eq(6)
end
it "evaluates a function on a node" do
browser.go_to("/ferrum/index")
node = browser.at_xpath(".//a")
function = <<~JS
function(attributeName) {
return this.getAttribute(attributeName);
}
JS
expect(browser.evaluate_func(function, "href", on: node)).to eq("js_redirect")
end
end
describe "#evaluate_async" do
it "handles values properly" do
expect(browser.evaluate_async("arguments[0](null)", 5)).to be_nil
expect(browser.evaluate_async("arguments[0](false)", 5)).to be false
expect(browser.evaluate_async("arguments[0](true)", 5)).to be true
expect(browser.evaluate_async(%(arguments[0]({foo: "bar"})), 5)).to eq("foo" => "bar")
end
it "times out" do
expect do
browser.evaluate_async("var callback=arguments[0]; setTimeout(function(){callback(true)}, 4000)", 1)
end.to raise_error(Ferrum::ScriptTimeoutError)
end
end
describe "#evaluate_on" do
it "does not retry if node is not around anymore" do
browser.go_to("/ferrum/table")
node = browser.at_xpath(".//td")
browser.go_to("/ferrum/table")
expect(Ferrum::Utils::Attempt).to receive(:with_retry).with(hash_including(errors: [])).and_call_original
expect { browser.evaluate_on(node: node, expression: "this.textContent") }.to raise_error(Ferrum::NodeNotFoundError)
end
end
describe "#add_script_tag" do
it "adds by url" do
browser.go_to
expect do
browser.evaluate("$('a').first().text()")
end.to raise_error(Ferrum::JavaScriptError)
browser.add_script_tag(url: "/ferrum/jquery.min.js")
expect(browser.evaluate("$('a').first().text()")).to eq("Relative")
end
it "adds by path" do
browser.go_to
path = "#{Ferrum::Application::FERRUM_PUBLIC}/jquery-1.11.3.min.js"
expect do
browser.evaluate("$('a').first().text()")
end.to raise_error(Ferrum::JavaScriptError)
browser.add_script_tag(path: path)
expect(browser.evaluate("$('a').first().text()")).to eq("Relative")
end
it "adds by content" do
browser.go_to
browser.add_script_tag(content: "function yay() { return 'yay!'; }")
expect(browser.evaluate("yay()")).to eq("yay!")
end
end
describe "#add_style_tag" do
let(:font_size) do
<<~JS
window
.getComputedStyle(document.querySelector('a'))
.getPropertyValue('font-size')
JS
end
it "adds by url" do
browser.go_to
expect(browser.evaluate(font_size)).to eq("16px")
browser.add_style_tag(url: "/ferrum/add_style_tag.css")
expect(browser.evaluate(font_size)).to eq("50px")
end
it "adds by path" do
browser.go_to
path = "#{Ferrum::Application::FERRUM_PUBLIC}/add_style_tag.css"
expect(browser.evaluate(font_size)).to eq("16px")
browser.add_style_tag(path: path)
expect(browser.evaluate(font_size)).to eq("50px")
end
it "adds by content" do
browser.go_to
browser.add_style_tag(content: "a { font-size: 20px; }")
expect(browser.evaluate(font_size)).to eq("20px")
end
end
end