-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
wait_task_spec.rb
278 lines (240 loc) · 9.92 KB
/
wait_task_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
require 'spec_helper'
RSpec.describe Puppeteer::WaitTask do
describe 'Page.waitFor', sinatra: true do
it 'should wait for selector' do
found = false
wait_for = page.async_wait_for_selector('div').then { found = true }
page.goto(server_empty_page)
expect(found).to eq(false)
page.goto("#{server_prefix}/grid.html")
await wait_for
expect(found).to eq(true)
end
it 'should wait for an xpath' do
found = false
wait_for = page.async_wait_for_xpath('//div').then { found = true }
page.goto(server_empty_page)
expect(found).to eq(false)
page.goto("#{server_prefix}/grid.html")
await wait_for
expect(found).to eq(true)
end
it 'should timeout' do
start_time = Time.now
page.wait_for_timeout(42)
end_time = Time.now
expect(end_time - start_time).to be >= 0.021
end
it 'should work with multiline body' do
result = page.wait_for_function(<<~JAVASCRIPT)
() => true
JAVASCRIPT
expect(result.json_value).to eq(true)
end
it 'should wait for predicate' do
Timeout.timeout(1) do # assert not timeout.
page.wait_for_function('() => window.innerWidth < 100') do
page.viewport = Puppeteer::Viewport.new(width: 10, height: 10)
end
end
end
it 'should wait for predicate with arguments' do
Timeout.timeout(1) do # assert not timeout.
page.wait_for_function('(arg1, arg2) => arg1 !== arg2', args: [1, 2])
end
end
end
describe 'Frame.waitForSelector' do
include Utils::AttachFrame
include Utils::DetachFrame
let(:add_element) { "(tag) => document.body.appendChild(document.createElement(tag))" }
it 'should immediately resolve promise if node exists', sinatra: true do
page.goto(server_empty_page)
frame = page.main_frame
Timeout.timeout(1) { frame.wait_for_selector('*') }
frame.evaluate(add_element, 'div')
Timeout.timeout(1) { frame.wait_for_selector('div') }
end
it_fails_firefox 'should work with removed MutationObserver' do
page.evaluate("() => delete window.MutationObserver")
handle = page.wait_for_selector('.zombo') do
sleep 0.1
page.content = "<div class='zombo'>anything</div>"
end
expect(page.evaluate("(x) => x.textContent", handle)).to eq('anything')
end
it 'should resolve promise when node is added' do
frame = page.main_frame
watchdog = frame.async_wait_for_selector('div')
frame.evaluate(add_element, 'br')
frame.evaluate(add_element, 'div')
handle = Timeout.timeout(1) { await watchdog }
tag_name = handle.property('tagName').json_value
expect(tag_name).to eq('DIV')
end
it 'should work when node is added through innerHTML', sinatra: true do
page.goto(server_empty_page)
watchdog = page.async_wait_for_selector('h3 div')
page.evaluate(add_element, 'span')
page.evaluate("() => (document.querySelector('span').innerHTML = '<h3><div></div></h3>')")
Timeout.timeout(1) { await watchdog }
end
it 'Page.waitForSelector is shortcut for main frame', sinatra: true do
page.goto(server_empty_page)
attach_frame(page, 'frame1', server_empty_page)
other_frame = page.frames.last
watchdog = page.async_wait_for_selector('div')
other_frame.evaluate(add_element, 'div')
page.evaluate(add_element, 'div')
handle = await watchdog
expect(handle.execution_context.frame).to eq(page.main_frame)
end
it 'should run in specified frame', sinatra: true do
page.goto(server_empty_page)
attach_frame(page, 'frame1', server_empty_page)
attach_frame(page, 'frame2', server_empty_page)
frame1 = page.frames[1]
frame2 = page.frames[2]
promise = frame2.async_wait_for_selector('div')
frame1.evaluate(add_element, 'div')
frame2.evaluate(add_element, 'div')
handle = await promise
expect(handle.execution_context.frame).to eq(frame2)
end
it_fails_firefox 'should throw when frame is detached', sinatra: true do
page.goto(server_empty_page)
attach_frame(page, 'frame1', server_empty_page)
frame = page.frames.last
promise = frame.async_wait_for_selector('.box')
detach_frame(page, 'frame1')
expect { await promise }.to raise_error(/waitForFunction failed: frame got detached./)
end
# it('should survive cross-process navigation', async () => {
# const { page, server } = getTestState();
# let boxFound = false;
# const waitForSelector = page
# .waitForSelector('.box')
# .then(() => (boxFound = true));
# await page.goto(server.EMPTY_PAGE);
# expect(boxFound).toBe(false);
# await page.reload();
# expect(boxFound).toBe(false);
# await page.goto(server.CROSS_PROCESS_PREFIX + '/grid.html');
# await waitForSelector;
# expect(boxFound).toBe(true);
# });
it 'should wait for visible' do
promise = page.async_wait_for_selector('div', visible: true)
page.content = "<div style='display: none; visibility: hidden;'>1</div>"
sleep 1
expect(promise).not_to be_fulfilled
page.evaluate("() => document.querySelector('div').style.removeProperty('display')")
sleep 1
expect(promise).not_to be_fulfilled
page.evaluate("() => document.querySelector('div').style.removeProperty('visibility')")
Timeout.timeout(1) { await promise }
end
# it('should wait for visible recursively', async () => {
# const { page } = getTestState();
# let divVisible = false;
# const waitForSelector = page
# .waitForSelector('div#inner', { visible: true })
# .then(() => (divVisible = true));
# await page.setContent(
# `<div style='display: none; visibility: hidden;'><div id="inner">hi</div></div>`
# );
# expect(divVisible).toBe(false);
# await page.evaluate(() =>
# document.querySelector('div').style.removeProperty('display')
# );
# expect(divVisible).toBe(false);
# await page.evaluate(() =>
# document.querySelector('div').style.removeProperty('visibility')
# );
# expect(await waitForSelector).toBe(true);
# expect(divVisible).toBe(true);
# });
# it('hidden should wait for visibility: hidden', async () => {
# const { page } = getTestState();
# let divHidden = false;
# await page.setContent(`<div style='display: block;'></div>`);
# const waitForSelector = page
# .waitForSelector('div', { hidden: true })
# .then(() => (divHidden = true));
# await page.waitForSelector('div'); // do a round trip
# expect(divHidden).toBe(false);
# await page.evaluate(() =>
# document.querySelector('div').style.setProperty('visibility', 'hidden')
# );
# expect(await waitForSelector).toBe(true);
# expect(divHidden).toBe(true);
# });
it 'hidden should wait for display: none' do
page.content = "<div style='display: block;'></div>"
promise = page.async_wait_for_selector('div', hidden: true)
Timeout.timeout(1) { page.wait_for_selector('div') } # do a round trip
expect(promise).not_to be_fulfilled
page.evaluate("() => document.querySelector('div').style.setProperty('display', 'none')")
Timeout.timeout(1) { await promise }
end
it 'hidden should wait for removal' do
page.content = '<div></div>'
promise = page.async_wait_for_selector('div', hidden: true)
page.evaluate("() => document.querySelector('div').remove()")
Timeout.timeout(1) { await promise }
end
it 'should return null if waiting to hide non-existing element' do
handle = page.wait_for_selector('non-existing', hidden: true)
expect(handle).to be_nil
end
it 'should respect timeout' do
page.content = '<span></span>'
expect {
page.wait_for_selector('div', timeout: 10)
}.to raise_error(Puppeteer::TimeoutError)
end
# it('should have an error message specifically for awaiting an element to be hidden', async () => {
# const { page } = getTestState();
# await page.setContent(`<div></div>`);
# let error = null;
# await page
# .waitForSelector('div', { hidden: true, timeout: 10 })
# .catch((error_) => (error = error_));
# expect(error).toBeTruthy();
# expect(error.message).toContain(
# 'waiting for selector `div` to be hidden failed: timeout'
# );
# });
# it('should respond to node attribute mutation', async () => {
# const { page } = getTestState();
# let divFound = false;
# const waitForSelector = page
# .waitForSelector('.zombo')
# .then(() => (divFound = true));
# await page.setContent(`<div class='notZombo'></div>`);
# expect(divFound).toBe(false);
# await page.evaluate(
# () => (document.querySelector('div').className = 'zombo')
# );
# expect(await waitForSelector).toBe(true);
# });
it 'should return the element handle' do
promise = page.async_wait_for_selector('.zombo')
page.content = "<div class='zombo'>anything</div>"
handle = await promise
expect(handle).to be_a(Puppeteer::ElementHandle)
expect(page.evaluate('(x) => x.textContent', handle)).to eq('anything')
end
# it('should have correct stack trace for timeout', async () => {
# const { page } = getTestState();
# let error;
# await page
# .waitForSelector('.zombo', { timeout: 10 })
# .catch((error_) => (error = error_));
# expect(error.stack).toContain('waiting for selector `.zombo` failed');
# // The extension is ts here as Mocha maps back via sourcemaps.
# expect(error.stack).toContain('waittask.spec.ts');
# });
# });
end
end