Skip to content

Commit 8bd6251

Browse files
committed
issue#229 add option 'by' for Node#select to picks option by provided attribute
1 parent b5241dc commit 8bd6251

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/ferrum/node.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,17 +142,17 @@ def selected
142142
page.evaluate_func(function, self)
143143
end
144144

145-
def select(*values)
145+
def select(*values, by: :value)
146146
tap do
147147
function = <<~JS
148-
function(element, values) {
148+
function(element, values, by) {
149149
if (element.nodeName.toLowerCase() !== 'select') {
150150
throw new Error('Element is not a <select> element.');
151151
}
152152
const options = Array.from(element.options);
153153
element.value = undefined;
154154
for (const option of options) {
155-
option.selected = values.includes(option.value);
155+
option.selected = values.includes(option[by]);
156156
if (option.selected && !element.multiple) break;
157157
}
158158
element.dispatchEvent(new Event('input', { bubbles: true }));
@@ -162,7 +162,7 @@ def select(*values)
162162
.map((option) => option.value);
163163
}
164164
JS
165-
page.evaluate_func(function, self, values.join(","))
165+
page.evaluate_func(function, self, values.join(","), by)
166166
end
167167
end
168168

spec/node_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ module Ferrum
171171
expect(browser.at_xpath("//*[@id='form_title']").select(["Other"]).selected.map(&:text)).to eq(["Other"])
172172
end
173173
end
174+
175+
context "when option with text and value" do
176+
it "picks option in select by matched text" do
177+
expect(browser.at_xpath("//select[@id='form_locale']").select("Swedish", by: :text).selected.map(&:value)).
178+
to eq(["sv"])
179+
end
180+
end
174181
end
175182

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

0 commit comments

Comments
 (0)