Skip to content

Commit dcb368a

Browse files
committed
issue#229 fix cases when option with empty text/value
1 parent 7558cc5 commit dcb368a

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

lib/ferrum/node.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ def select(*values, by: :value)
152152
const options = Array.from(element.options);
153153
element.value = undefined;
154154
for (const option of options) {
155-
option.selected = values.includes(option[by]);
155+
option.selected = values.some((value) => option[by] === value);
156156
if (option.selected && !element.multiple) break;
157157
}
158158
element.dispatchEvent(new Event('input', { bubbles: true }));
159159
element.dispatchEvent(new Event('change', { bubbles: true }));
160160
}
161161
JS
162-
page.evaluate_func(function, self, values.join(","), by)
162+
page.evaluate_func(function, self, values.flatten, by)
163163
end
164164
end
165165

spec/node_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ module Ferrum
153153
.to eq(%w[Ruby SQL])
154154
end
155155

156-
it "picks options in select by match arguments as string" do
157-
expect(browser.at_xpath("//*[@id='form_languages']").select("SQL, Ruby").selected.map(&:text))
156+
it "picks options in select by match arguments as strings" do
157+
expect(browser.at_xpath("//*[@id='form_languages']").select("SQL", "Ruby").selected.map(&:text))
158158
.to eq(%w[Ruby SQL])
159159
end
160160
end
@@ -174,8 +174,23 @@ module Ferrum
174174

175175
context "when option with text and value" do
176176
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"])
177+
expect(browser.at_xpath("//select[@id='form_locale']").select("Swedish", by: :text).selected.map(&:value))
178+
.to eq(["sv"])
179+
end
180+
end
181+
182+
context "when option with empty text/value" do
183+
it "picks option in select by match string argument" do
184+
expect(browser.at_xpath("//select[@id='empty_option']").select("AU").selected.map(&:value)).to eq(["AU"])
185+
end
186+
187+
it "picks empty option by match empty value argument" do
188+
expect(browser.at_xpath("//select[@id='empty_option']").select("").selected.map(&:value)).to eq([""])
189+
end
190+
191+
it "picks empty option by match empty text argument" do
192+
expect(browser.at_xpath("//select[@id='empty_option']").select("", by: :text).selected.map(&:text))
193+
.to eq([""])
179194
end
180195
end
181196
end

spec/support/views/form.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,3 +672,8 @@ New line after and before textarea tag
672672
<p>
673673
<input id="hidden_input" style="display: none"/>
674674
</p>
675+
676+
<select id="empty_option">
677+
<option value=""></option>
678+
<option value="AU">Australia</option>
679+
</select>

0 commit comments

Comments
 (0)