Skip to content

Commit 569d7c0

Browse files
authored
fix(select): handle empty values and labels in select options (microsoft#2661)
1 parent c4df71c commit 569d7c0

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

playwright/_impl/_element_handle.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,15 @@ def convert_select_option_values(
392392

393393
options: Any = None
394394
elements: Any = None
395-
if value:
395+
if value is not None:
396396
if isinstance(value, str):
397397
value = [value]
398398
options = (options or []) + list(map(lambda e: dict(valueOrLabel=e), value))
399-
if index:
399+
if index is not None:
400400
if isinstance(index, int):
401401
index = [index]
402402
options = (options or []) + list(map(lambda e: dict(index=e), index))
403-
if label:
403+
if label is not None:
404404
if isinstance(label, str):
405405
label = [label]
406406
options = (options or []) + list(map(lambda e: dict(label=e), label))

tests/async/test_page_select_option.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ async def test_select_option_should_select_single_option_by_label(
4545
assert await page.evaluate("result.onChange") == ["indigo"]
4646

4747

48+
async def test_select_option_should_select_single_option_by_empty_label(
49+
page: Page, server: Server
50+
) -> None:
51+
await page.set_content(
52+
"""
53+
<select>
54+
<option value="indigo">Indigo</option>
55+
<option value="violet"></option>
56+
</select>
57+
"""
58+
)
59+
assert await page.locator("select").input_value() == "indigo"
60+
await page.select_option("select", label="")
61+
assert await page.locator("select").input_value() == "violet"
62+
63+
4864
async def test_select_option_should_select_single_option_by_handle(
4965
page: Page, server: Server
5066
) -> None:
@@ -65,6 +81,14 @@ async def test_select_option_should_select_single_option_by_index(
6581
assert await page.evaluate("result.onChange") == ["brown"]
6682

6783

84+
async def test_select_option_should_select_single_option_by_index_0(
85+
page: Page, server: Server
86+
) -> None:
87+
await page.goto(server.PREFIX + "/input/select.html")
88+
await page.select_option("select", index=0)
89+
assert await page.evaluate("result.onInput") == ["black"]
90+
91+
6892
async def test_select_option_should_select_only_first_option(
6993
page: Page, server: Server
7094
) -> None:
@@ -112,6 +136,23 @@ async def test_select_option_should_select_multiple_options_with_attributes(
112136
assert await page.evaluate("result.onChange") == ["blue", "gray", "green"]
113137

114138

139+
async def test_select_option_should_select_option_with_empty_value(
140+
page: Page, server: Server
141+
) -> None:
142+
await page.goto(server.EMPTY_PAGE)
143+
await page.set_content(
144+
"""
145+
<select>
146+
<option value="first">First</option>
147+
<option value="">Second</option>
148+
</select>
149+
"""
150+
)
151+
assert await page.locator("select").input_value() == "first"
152+
await page.select_option("select", value="")
153+
assert await page.locator("select").input_value() == ""
154+
155+
115156
async def test_select_option_should_respect_event_bubbling(
116157
page: Page, server: Server
117158
) -> None:

tests/sync/test_page_select_option.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def test_select_option_should_select_single_option_by_label(
4343
assert page.evaluate("result.onChange") == ["indigo"]
4444

4545

46+
def test_select_option_should_select_single_option_by_empty_label(
47+
page: Page, server: Server
48+
) -> None:
49+
page.set_content(
50+
"""
51+
<select>
52+
<option value="indigo">Indigo</option>
53+
<option value="violet"></option>
54+
</select>
55+
"""
56+
)
57+
assert page.locator("select").input_value() == "indigo"
58+
page.select_option("select", label="")
59+
assert page.locator("select").input_value() == "violet"
60+
61+
4662
def test_select_option_should_select_single_option_by_handle(
4763
server: Server, page: Page
4864
) -> None:
@@ -61,6 +77,14 @@ def test_select_option_should_select_single_option_by_index(
6177
assert page.evaluate("result.onChange") == ["brown"]
6278

6379

80+
def test_select_option_should_select_single_option_by_index_0(
81+
page: Page, server: Server
82+
) -> None:
83+
page.goto(server.PREFIX + "/input/select.html")
84+
page.select_option("select", index=0)
85+
assert page.evaluate("result.onInput") == ["black"]
86+
87+
6488
def test_select_option_should_select_only_first_option(
6589
server: Server, page: Page
6690
) -> None:
@@ -108,6 +132,23 @@ def test_select_option_should_select_multiple_options_with_attributes(
108132
assert page.evaluate("result.onChange") == ["blue", "gray", "green"]
109133

110134

135+
def test_select_option_should_select_option_with_empty_value(
136+
page: Page, server: Server
137+
) -> None:
138+
page.goto(server.EMPTY_PAGE)
139+
page.set_content(
140+
"""
141+
<select>
142+
<option value="first">First</option>
143+
<option value="">Second</option>
144+
</select>
145+
"""
146+
)
147+
assert page.locator("select").input_value() == "first"
148+
page.select_option("select", value="")
149+
assert page.locator("select").input_value() == ""
150+
151+
111152
def test_select_option_should_respect_event_bubbling(
112153
server: Server, page: Page
113154
) -> None:

0 commit comments

Comments
 (0)