-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
frame_spec.rb
334 lines (298 loc) · 10.6 KB
/
frame_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
require 'spec_helper'
RSpec.describe Puppeteer::Frame do
context 'with empty page' do
sinatra do
get('/') do
'<html><body>Hello puppeteer!</body></html>'
end
end
describe '#execution_context' do
include Utils::AttachFrame
it 'should work' do
page.goto('http://127.0.0.1:4567/')
attach_frame(page, 'frame1', '/')
expect(page.frames.size).to eq(2)
frames = page.frames
contexts = frames.map(&:execution_context)
expect(contexts).to all(be_truthy)
expect(contexts.first).not_to eq(contexts.last)
expect(contexts.map(&:frame)).to eq(frames)
contexts.each_with_index do |context, i|
context.evaluate("() => (globalThis.a = #{i + 1})")
end
values = contexts.map { |context| context.evaluate('() => globalThis.a') }
expect(values).to eq([1, 2])
end
end
describe '#evaluate_handle' do
it 'should work' do
page.goto('http://127.0.0.1:4567/')
main_frame = page.main_frame
window_handle = main_frame.evaluate_handle('() => window')
expect(window_handle).to be_truthy
end
end
describe '#evaluate' do
include Utils::AttachFrame
include Utils::DetachFrame
it 'should throw for detached frames' do
page.goto('http://127.0.0.1:4567/')
frame1 = attach_frame(page, 'frame1', '/')
detach_frame(page, 'frame1')
expect {
frame1.evaluate('() => 7 * 8')
}.to raise_error(/Execution Context is not available in detached frame/)
end
end
end
context 'with nested frames page' do
sinatra do
get('/nested-frames.html') do
<<~HTML
<style>
body {
display: flex;
}
body iframe {
flex-grow: 1;
flex-shrink: 1;
}
::-webkit-scrollbar{
display: none;
}
html { /* for Firefox */
scrollbar-width: none;
}
</style>
<script>
async function attachFrame(frameId, url) {
var frame = document.createElement('iframe');
frame.src = url;
frame.id = frameId;
document.body.appendChild(frame);
await new Promise(x => frame.onload = x);
return 'kazakh';
}
</script>
<iframe src='./two-frames.html' name='2frames'></iframe>
<iframe src='./frame.html' name='aframe'></iframe>
HTML
end
get('/two-frames.html') do
<<~HTML
<style>
body {
display: flex;
flex-direction: column;
}
body iframe {
flex-grow: 1;
flex-shrink: 1;
}
html { /* for Firefox */
scrollbar-width: none;
}
</style>
<iframe src='./frame.html' name='uno'></iframe>
<iframe src='./frame.html' name='dos'></iframe>
HTML
end
get('/frame.html') do
<<~HTML
<script src='./script.js' type='text/javascript'></script>
<style>
div {
color: blue;
line-height: 18px;
}
</style>
<div>Hi, I'm frame</div>
HTML
end
end
include Utils::DumpFrames
it 'should handle nested frames' do
page.goto('http://127.0.0.1:4567/nested-frames.html')
expect(dump_frames(page.main_frame)).to eq([
'http://127.0.0.1:<PORT>/nested-frames.html',
' http://127.0.0.1:<PORT>/two-frames.html (2frames)',
' http://127.0.0.1:<PORT>/frame.html (uno)',
' http://127.0.0.1:<PORT>/frame.html (dos)',
' http://127.0.0.1:<PORT>/frame.html (aframe)',
])
end
end
# itFailsFirefox(
# 'should send events when frames are manipulated dynamically',
# async () => {
# const { page, server } = getTestState();
# await page.goto(server.EMPTY_PAGE);
# // validate frameattached events
# const attachedFrames = [];
# page.on('frameattached', (frame) => attachedFrames.push(frame));
# await utils.attachFrame(page, 'frame1', './assets/frame.html');
# expect(attachedFrames.length).toBe(1);
# expect(attachedFrames[0].url()).toContain('/assets/frame.html');
# // validate framenavigated events
# const navigatedFrames = [];
# page.on('framenavigated', (frame) => navigatedFrames.push(frame));
# await utils.navigateFrame(page, 'frame1', './empty.html');
# expect(navigatedFrames.length).toBe(1);
# expect(navigatedFrames[0].url()).toBe(server.EMPTY_PAGE);
# // validate framedetached events
# const detachedFrames = [];
# page.on('framedetached', (frame) => detachedFrames.push(frame));
# await utils.detachFrame(page, 'frame1');
# expect(detachedFrames.length).toBe(1);
# expect(detachedFrames[0].isDetached()).toBe(true);
# }
# );
# itFailsFirefox(
# 'should send "framenavigated" when navigating on anchor URLs',
# async () => {
# const { page, server } = getTestState();
# await page.goto(server.EMPTY_PAGE);
# await Promise.all([
# page.goto(server.EMPTY_PAGE + '#foo'),
# utils.waitEvent(page, 'framenavigated'),
# ]);
# expect(page.url()).toBe(server.EMPTY_PAGE + '#foo');
# }
# );
# it('should persist mainFrame on cross-process navigation', async () => {
# const { page, server } = getTestState();
# await page.goto(server.EMPTY_PAGE);
# const mainFrame = page.mainFrame();
# await page.goto(server.CROSS_PROCESS_PREFIX + '/empty.html');
# expect(page.mainFrame() === mainFrame).toBeTruthy();
# });
# it('should not send attach/detach events for main frame', async () => {
# const { page, server } = getTestState();
# let hasEvents = false;
# page.on('frameattached', () => (hasEvents = true));
# page.on('framedetached', () => (hasEvents = true));
# await page.goto(server.EMPTY_PAGE);
# expect(hasEvents).toBe(false);
# });
# itFailsFirefox('should detach child frames on navigation', async () => {
# const { page, server } = getTestState();
# let attachedFrames = [];
# let detachedFrames = [];
# let navigatedFrames = [];
# page.on('frameattached', (frame) => attachedFrames.push(frame));
# page.on('framedetached', (frame) => detachedFrames.push(frame));
# page.on('framenavigated', (frame) => navigatedFrames.push(frame));
# await page.goto(server.PREFIX + '/frames/nested-frames.html');
# expect(attachedFrames.length).toBe(4);
# expect(detachedFrames.length).toBe(0);
# expect(navigatedFrames.length).toBe(5);
# attachedFrames = [];
# detachedFrames = [];
# navigatedFrames = [];
# await page.goto(server.EMPTY_PAGE);
# expect(attachedFrames.length).toBe(0);
# expect(detachedFrames.length).toBe(4);
# expect(navigatedFrames.length).toBe(1);
# });
# itFailsFirefox('should support framesets', async () => {
# const { page, server } = getTestState();
# let attachedFrames = [];
# let detachedFrames = [];
# let navigatedFrames = [];
# page.on('frameattached', (frame) => attachedFrames.push(frame));
# page.on('framedetached', (frame) => detachedFrames.push(frame));
# page.on('framenavigated', (frame) => navigatedFrames.push(frame));
# await page.goto(server.PREFIX + '/frames/frameset.html');
# expect(attachedFrames.length).toBe(4);
# expect(detachedFrames.length).toBe(0);
# expect(navigatedFrames.length).toBe(5);
# attachedFrames = [];
# detachedFrames = [];
# navigatedFrames = [];
# await page.goto(server.EMPTY_PAGE);
# expect(attachedFrames.length).toBe(0);
# expect(detachedFrames.length).toBe(4);
# expect(navigatedFrames.length).toBe(1);
# });
# itFailsFirefox('should report frame from-inside shadow DOM', async () => {
# const { page, server } = getTestState();
# await page.goto(server.PREFIX + '/shadow.html');
# await page.evaluate(async (url: string) => {
# const frame = document.createElement('iframe');
# frame.src = url;
# document.body.shadowRoot.appendChild(frame);
# await new Promise((x) => (frame.onload = x));
# }, server.EMPTY_PAGE);
# expect(page.frames().length).toBe(2);
# expect(page.frames()[1].url()).toBe(server.EMPTY_PAGE);
# });
context 'with empty page' do
include Utils::AttachFrame
sinatra do
get('/') do
'<html><body>Hello puppeteer!</body></html>'
end
end
before { page.goto('http://127.0.0.1:4567/') }
it 'should report frame.name()' do
attach_frame(page, 'theFrameId', '/')
js = <<~JAVASCRIPT
function (url) {
const frame = document.createElement('iframe');
frame.name = 'theFrameName';
frame.src = url;
document.body.appendChild(frame);
return new Promise((x) => (frame.onload = x));
}
JAVASCRIPT
page.evaluate(js, '/')
expect(page.frames.map(&:name)).to eq(['', 'theFrameId', 'theFrameName'])
end
it 'should report frame.parent()' do
attach_frame(page, 'frame1', '/')
attach_frame(page, 'frame2', '/')
expect(page.frames.map(&:parent_frame)).to eq([nil, page.main_frame, page.main_frame])
end
it 'should report different frame instance when frame re-attaches' do
frame1 = attach_frame(page, 'frame1', '/')
js = <<~JAVASCRIPT
() => {
globalThis.frame = document.querySelector('#frame1');
globalThis.frame.remove();
}
JAVASCRIPT
page.evaluate(js)
expect(frame1).to be_detached
frame2 = await_all(
resolvable_future { |f| page.once('frameattached') { |frame| f.fulfill(frame) }},
page.async_evaluate('() => document.body.appendChild(globalThis.frame)'),
).first
expect(frame2).not_to be_detached
expect(frame1).not_to eq(frame2)
end
end
context 'with one-frame-url-fragment page' do
sinatra do
get('/one-frame-url-fragment.html') do
"<iframe src='./frame.html?param=value#fragment'></iframe>"
end
get('/frame.html') do
<<~HTML
<link rel='stylesheet' href='./style.css'>
<script src='./script.js' type='text/javascript'></script>
<style>
div {
line-height: 18px;
}
</style>
<div>Hi, I'm frame</div>
HTML
end
end
it 'should support url fragment' do
page.goto('http://127.0.0.1:4567/one-frame-url-fragment.html')
expect(page.frames.size).to eq(2)
expect(page.frames.last.url).to eq('http://127.0.0.1:4567/frame.html?param=value#fragment')
end
end
end