-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
utils.rb
41 lines (38 loc) · 1.08 KB
/
utils.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
module Utils ; end
module Utils::AttachFrame
def attach_frame(page, frame_id, url)
js = <<~JAVASCRIPT
async function attachFrame(frameId, url) {
const frame = document.createElement('iframe');
frame.src = url;
frame.id = frameId;
document.body.appendChild(frame);
await new Promise((x) => (frame.onload = x));
return frame;
}
JAVASCRIPT
page.evaluate_handle(js, frame_id, url).as_element.content_frame
end
end
module Utils::DetachFrame
def detach_frame(page, frame_id)
js = <<~JAVASCRIPT
function detachFrame(frameId) {
const frame = document.getElementById(frameId);
frame.remove();
}
JAVASCRIPT
page.evaluate(js, frame_id)
end
end
module Utils::DumpFrames
def dump_frames(frame, indentation = '')
description = frame.url.gsub(/:\d{4}\//, ':<PORT>/')
if frame.name && frame.name.length > 0
description = "#{description} (#{frame.name})"
end
["#{indentation}#{description}"] + frame.child_frames.flat_map do |child|
dump_frames(child, " #{indentation}")
end
end
end