Skip to content

Commit 2750c63

Browse files
authored
feat: Add Frame#parent method (#523)
Facilitates easier traversal of nested iframes.
1 parent 20f4373 commit 2750c63

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/ferrum/frame.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,23 @@ def main?
9494
@parent_id.nil?
9595
end
9696

97+
#
98+
# Returns the parent frame if this frame is nested in another one.
99+
#
100+
# @return [Frame, nil]
101+
#
102+
# @example
103+
# browser.go_to("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")
104+
# frame = browser.frames.last
105+
# frame.url # => "https://www.openstreetmap.org/export/embed.html?bbox=-0.004017949104309083%2C51.47612752641776%2C0.00030577182769775396%2C51.478569861898606&layer=mapnik"
106+
# frame.parent.main? # => false
107+
# frame.parent.parent.main? # => false
108+
# frame.parent.parent.parent.main? # => true
109+
#
110+
def parent
111+
@page.frame_by(id: @parent_id) if @parent_id
112+
end
113+
97114
#
98115
# Sets a content of a given frame.
99116
#

spec/frame_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
expect(opened_page.main_frame.url).to end_with("/frames")
4444
end
4545

46+
it "finds parent frame properly" do
47+
page.go_to("/frames")
48+
parent_frame = page.at_xpath("//iframe").frame.parent
49+
expect(parent_frame.url).to end_with("/frames")
50+
end
51+
4652
it "waits for the frame to load" do
4753
page.go_to
4854
page.execute <<-JS

0 commit comments

Comments
 (0)