Skip to content

Commit f0906db

Browse files
Mifrillroute
authored andcommitted
Fix select/selected to work properly within frame scope
1 parent b3045b1 commit f0906db

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

lib/ferrum/node.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def selected
139139
return Array.from(element).filter(option => option.selected);
140140
}
141141
JS
142-
page.evaluate_func(function, self)
142+
page.evaluate_func(function, self, on: self)
143143
end
144144

145145
def select(*values, by: :value)
@@ -159,7 +159,7 @@ def select(*values, by: :value)
159159
element.dispatchEvent(new Event('change', { bubbles: true }));
160160
}
161161
JS
162-
page.evaluate_func(function, self, values.flatten, by)
162+
page.evaluate_func(function, self, values.flatten, by, on: self)
163163
end
164164
end
165165

spec/node_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ module Ferrum
110110
.to raise_exception(Ferrum::JavaScriptError, /Element is not a <select> element/)
111111
end
112112
end
113+
114+
it "returns selected options within frame" do
115+
browser.execute <<-JS
116+
document.body.innerHTML += "<iframe src='about:blank' name='frame'>";
117+
var iframeDocument = document.querySelector("iframe[name='frame']").contentWindow.document;
118+
var content = "<html><body><select id='select'><option>One</option></select></body></html>";
119+
iframeDocument.open("text/html", "replace");
120+
iframeDocument.write(content);
121+
iframeDocument.close();
122+
JS
123+
frame = browser.at_xpath("//iframe[@name='frame']").frame
124+
expect(frame.at_xpath("//*[@id='select']").selected.map(&:text)).to eq(["One"])
125+
end
113126
end
114127

115128
describe "#select" do
@@ -193,6 +206,19 @@ module Ferrum
193206
.to eq([""])
194207
end
195208
end
209+
210+
it "picks option within frame" do
211+
browser.execute <<-JS
212+
document.body.innerHTML += "<iframe src='about:blank' name='frame'>";
213+
var iframeDocument = document.querySelector("iframe[name='frame']").contentWindow.document;
214+
var content = "<html><body><select id='select'><option>One</option><option>Two</option></select></body></html>";
215+
iframeDocument.open("text/html", "replace");
216+
iframeDocument.write(content);
217+
iframeDocument.close();
218+
JS
219+
frame = browser.at_xpath("//iframe[@name='frame']").frame
220+
expect(frame.at_xpath("//*[@id='select']").select("Two", by: :text).selected.map(&:text)).to eq(["Two"])
221+
end
196222
end
197223

198224
context "when the element is not in the viewport" do

0 commit comments

Comments
 (0)