Skip to content

Commit b5241dc

Browse files
committed
issue#229 Node#selected - returns nodes instead texts
1 parent 8e13c8e commit b5241dc

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ frame.at_css("//a[text() = 'Log in']") # => Node
10981098
#### property
10991099
#### attribute
11001100
#### evaluate
1101-
#### selected : `Array`
1101+
#### selected : `Array<Node>`
11021102
#### select
11031103

11041104

lib/ferrum/node.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def selected
136136
if (element.nodeName.toLowerCase() !== 'select') {
137137
throw new Error('Element is not a <select> element.');
138138
}
139-
return Array.from(element).filter(option => option.selected).map((option) => option.text);
139+
return Array.from(element).filter(option => option.selected);
140140
}
141141
JS
142142
page.evaluate_func(function, self)

spec/node_spec.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,24 +89,24 @@ module Ferrum
8989
end
9090

9191
it "returns texts of selected options" do
92-
expect(browser.at_xpath("//*[@id='form_region']").selected).to eq(["Norway"])
92+
expect(browser.at_xpath("//*[@id='form_region']").selected.map(&:text)).to eq(["Norway"])
9393
end
9494

9595
context "when options exists but no selected option" do
9696
it "returns first option text as default value" do
97-
expect(browser.at_xpath("//*[@id='form_title']").selected).to eq(["Mrs"])
97+
expect(browser.at_xpath("//*[@id='form_title']").selected.map(&:text)).to eq(["Mrs"])
9898
end
9999
end
100100

101101
context "when no selected options" do
102102
it "returns empty array" do
103-
expect(browser.at_xpath("//*[@id='form_tendency']").selected).to eq([])
103+
expect(browser.at_xpath("//*[@id='form_tendency']").selected.map(&:text)).to eq([])
104104
end
105105
end
106106

107107
context "when selector is not <select>" do
108108
it "raises JavaScriptError with proper message" do
109-
expect { browser.at_xpath("//*[@id='customer_name']").selected }
109+
expect { browser.at_xpath("//*[@id='customer_name']").selected.map(&:text) }
110110
.to raise_exception(Ferrum::JavaScriptError, /Element is not a <select> element/)
111111
end
112112
end
@@ -118,13 +118,13 @@ module Ferrum
118118
end
119119

120120
it "picks option in select by match string argument" do
121-
expect(browser.at_xpath("//*[@id='form_title']").select("Miss").selected).to eq(["Miss"])
121+
expect(browser.at_xpath("//*[@id='form_title']").select("Miss").selected.map(&:text)).to eq(["Miss"])
122122
end
123123

124124
shared_examples "clears selected options with no exception" do |options|
125125
it "clears selected options with no exception" do
126-
expect(browser.at_xpath("//*[@id='form_title']").selected).to eq(["Mrs"])
127-
expect(browser.at_xpath("//*[@id='form_title']").select(options).selected).to eq([])
126+
expect(browser.at_xpath("//*[@id='form_title']").selected.map(&:text)).to eq(["Mrs"])
127+
expect(browser.at_xpath("//*[@id='form_title']").select(options).selected.map(&:text)).to eq([])
128128
end
129129
end
130130

@@ -142,18 +142,20 @@ module Ferrum
142142

143143
context "when one of option with provided texts does not exist" do
144144
it "picks only existed options with no exception" do
145-
expect(browser.at_xpath("//*[@id='form_title']").selected).to eq(["Mrs"])
146-
expect(browser.at_xpath("//*[@id='form_title']").select(%w[Mrs SQL]).selected).to eq(["Mrs"])
145+
expect(browser.at_xpath("//*[@id='form_title']").selected.map(&:text)).to eq(["Mrs"])
146+
expect(browser.at_xpath("//*[@id='form_title']").select(%w[Mrs SQL]).selected.map(&:text)).to eq(["Mrs"])
147147
end
148148
end
149149

150150
context "when select has multiple property" do
151151
it "picks options in select by match arguments as array" do
152-
expect(browser.at_xpath("//*[@id='form_languages']").select(%w[SQL Ruby]).selected).to eq(%w[Ruby SQL])
152+
expect(browser.at_xpath("//*[@id='form_languages']").select(%w[SQL Ruby]).selected.map(&:text))
153+
.to eq(%w[Ruby SQL])
153154
end
154155

155156
it "picks options in select by match arguments as string" do
156-
expect(browser.at_xpath("//*[@id='form_languages']").select("SQL, Ruby").selected).to eq(%w[Ruby SQL])
157+
expect(browser.at_xpath("//*[@id='form_languages']").select("SQL, Ruby").selected.map(&:text))
158+
.to eq(%w[Ruby SQL])
157159
end
158160
end
159161

@@ -166,7 +168,7 @@ module Ferrum
166168

167169
context "when provided texts of disabled option" do
168170
it "picks disabled option with no exception" do
169-
expect(browser.at_xpath("//*[@id='form_title']").select(["Other"]).selected).to eq(["Other"])
171+
expect(browser.at_xpath("//*[@id='form_title']").select(["Other"]).selected.map(&:text)).to eq(["Other"])
170172
end
171173
end
172174
end

0 commit comments

Comments
 (0)