feat: selectOption (#40)

This commit is contained in:
Yury Semikhatsky 2020-10-23 16:36:21 -07:00 committed by GitHub
parent 114fd54060
commit 8258b1ac49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 520 additions and 24 deletions

View File

@ -312,6 +312,54 @@ class Method extends Element {
}; };
customSignature.put("Page.waitForEvent", waitForEvent); customSignature.put("Page.waitForEvent", waitForEvent);
customSignature.put("BrowserContext.waitForEvent", waitForEvent); customSignature.put("BrowserContext.waitForEvent", waitForEvent);
String[] selectOption = {
"default List<String> selectOption(String selector, String value) {",
" return selectOption(selector, value, null);",
"}",
"default List<String> selectOption(String selector, String value, SelectOptionOptions options) {",
" String[] values = value == null ? null : new String[]{ value };",
" return selectOption(selector, values, options);",
"}",
"default List<String> selectOption(String selector, String[] values) {",
" return selectOption(selector, values, null);",
"}",
"default List<String> selectOption(String selector, String[] values, SelectOptionOptions options) {",
" if (values == null) {",
" return selectOption(selector, new ElementHandle.SelectOption[0], options);",
" }",
" return selectOption(selector, Arrays.asList(values).stream().map(",
" v -> new ElementHandle.SelectOption().withValue(v)).toArray(ElementHandle.SelectOption[]::new), options);",
"}",
"default List<String> selectOption(String selector, ElementHandle.SelectOption value) {",
" return selectOption(selector, value, null);",
"}",
"default List<String> selectOption(String selector, ElementHandle.SelectOption value, SelectOptionOptions options) {",
" ElementHandle.SelectOption[] values = value == null ? null : new ElementHandle.SelectOption[]{value};",
" return selectOption(selector, values, options);",
"}",
"default List<String> selectOption(String selector, ElementHandle.SelectOption[] values) {",
" return selectOption(selector, values, null);",
"}",
"List<String> selectOption(String selector, ElementHandle.SelectOption[] values, SelectOptionOptions options);",
"default List<String> selectOption(String selector, ElementHandle value) {",
" return selectOption(selector, value, null);",
"}",
"default List<String> selectOption(String selector, ElementHandle value, SelectOptionOptions options) {",
" ElementHandle[] values = value == null ? null : new ElementHandle[]{value};",
" return selectOption(selector, values, options);",
"}",
"default List<String> selectOption(String selector, ElementHandle[] values) {",
" return selectOption(selector, values, null);",
"}",
"List<String> selectOption(String selector, ElementHandle[] values, SelectOptionOptions options);",
};
customSignature.put("Page.selectOption", selectOption);
customSignature.put("Frame.selectOption", selectOption);
customSignature.put("ElementHandle.selectOption", Arrays.stream(selectOption).map(s -> s
.replace("String selector, ", "")
.replace("(selector, ", "(")
.replace("ElementHandle.", "")).toArray(String[]::new));
} }
Method(TypeDefinition parent, JsonObject jsonElement) { Method(TypeDefinition parent, JsonObject jsonElement) {
@ -502,7 +550,7 @@ class Field extends Element {
class Interface extends TypeDefinition { class Interface extends TypeDefinition {
private final List<Method> methods = new ArrayList<>(); private final List<Method> methods = new ArrayList<>();
private final List<Event> events = new ArrayList<>(); private final List<Event> events = new ArrayList<>();
private static String header = "/**\n" + private static String header = "/*\n" +
" * Copyright (c) Microsoft Corporation.\n" + " * Copyright (c) Microsoft Corporation.\n" +
" *\n" + " *\n" +
" * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
@ -686,6 +734,25 @@ class Interface extends TypeDefinition {
output.add(offset + " public double height;"); output.add(offset + " public double height;");
output.add(offset + "}"); output.add(offset + "}");
output.add(""); output.add("");
output.add(offset + "class SelectOption {");
output.add(offset + " public String value;");
output.add(offset + " public String label;");
output.add(offset + " public Integer index;");
output.add("");
output.add(offset + " public SelectOption withValue(String value) {");
output.add(offset + " this.value = value;");
output.add(offset + " return this;");
output.add(offset + " }");
output.add(offset + " public SelectOption withLabel(String label) {");
output.add(offset + " this.label = label;");
output.add(offset + " return this;");
output.add(offset + " }");
output.add(offset + " public SelectOption withIndex(int index) {");
output.add(offset + " this.index = index;");
output.add(offset + " return this;");
output.add(offset + " }");
output.add(offset + "}");
output.add("");
break; break;
} }
case "FileChooser": { case "FileChooser": {

View File

@ -27,6 +27,25 @@ public interface ElementHandle extends JSHandle {
public double height; public double height;
} }
class SelectOption {
public String value;
public String label;
public Integer index;
public SelectOption withValue(String value) {
this.value = value;
return this;
}
public SelectOption withLabel(String label) {
this.label = label;
return this;
}
public SelectOption withIndex(int index) {
this.index = index;
return this;
}
}
enum ElementState { DISABLED, ENABLED, HIDDEN, STABLE, VISIBLE } enum ElementState { DISABLED, ENABLED, HIDDEN, STABLE, VISIBLE }
class CheckOptions { class CheckOptions {
public Boolean force; public Boolean force;
@ -420,10 +439,45 @@ public interface ElementHandle extends JSHandle {
scrollIntoViewIfNeeded(null); scrollIntoViewIfNeeded(null);
} }
void scrollIntoViewIfNeeded(ScrollIntoViewIfNeededOptions options); void scrollIntoViewIfNeeded(ScrollIntoViewIfNeededOptions options);
default List<String> selectOption(String values) { default List<String> selectOption(String value) {
return selectOption(value, null);
}
default List<String> selectOption(String value, SelectOptionOptions options) {
String[] values = value == null ? null : new String[]{ value };
return selectOption(values, options);
}
default List<String> selectOption(String[] values) {
return selectOption(values, null); return selectOption(values, null);
} }
List<String> selectOption(String values, SelectOptionOptions options); default List<String> selectOption(String[] values, SelectOptionOptions options) {
if (values == null) {
return selectOption(new SelectOption[0], options);
}
return selectOption(Arrays.asList(values).stream().map(
v -> new SelectOption().withValue(v)).toArray(SelectOption[]::new), options);
}
default List<String> selectOption(SelectOption value) {
return selectOption(value, null);
}
default List<String> selectOption(SelectOption value, SelectOptionOptions options) {
SelectOption[] values = value == null ? null : new SelectOption[]{value};
return selectOption(values, options);
}
default List<String> selectOption(SelectOption[] values) {
return selectOption(values, null);
}
List<String> selectOption(SelectOption[] values, SelectOptionOptions options);
default List<String> selectOption(ElementHandle value) {
return selectOption(value, null);
}
default List<String> selectOption(ElementHandle value, SelectOptionOptions options) {
ElementHandle[] values = value == null ? null : new ElementHandle[]{value};
return selectOption(values, options);
}
default List<String> selectOption(ElementHandle[] values) {
return selectOption(values, null);
}
List<String> selectOption(ElementHandle[] values, SelectOptionOptions options);
default void selectText() { default void selectText() {
selectText(null); selectText(null);
} }

View File

@ -560,10 +560,45 @@ public interface Frame {
press(selector, key, null); press(selector, key, null);
} }
void press(String selector, String key, PressOptions options); void press(String selector, String key, PressOptions options);
default List<String> selectOption(String selector, String values) { default List<String> selectOption(String selector, String value) {
return selectOption(selector, value, null);
}
default List<String> selectOption(String selector, String value, SelectOptionOptions options) {
String[] values = value == null ? null : new String[]{ value };
return selectOption(selector, values, options);
}
default List<String> selectOption(String selector, String[] values) {
return selectOption(selector, values, null); return selectOption(selector, values, null);
} }
List<String> selectOption(String selector, String values, SelectOptionOptions options); default List<String> selectOption(String selector, String[] values, SelectOptionOptions options) {
if (values == null) {
return selectOption(selector, new ElementHandle.SelectOption[0], options);
}
return selectOption(selector, Arrays.asList(values).stream().map(
v -> new ElementHandle.SelectOption().withValue(v)).toArray(ElementHandle.SelectOption[]::new), options);
}
default List<String> selectOption(String selector, ElementHandle.SelectOption value) {
return selectOption(selector, value, null);
}
default List<String> selectOption(String selector, ElementHandle.SelectOption value, SelectOptionOptions options) {
ElementHandle.SelectOption[] values = value == null ? null : new ElementHandle.SelectOption[]{value};
return selectOption(selector, values, options);
}
default List<String> selectOption(String selector, ElementHandle.SelectOption[] values) {
return selectOption(selector, values, null);
}
List<String> selectOption(String selector, ElementHandle.SelectOption[] values, SelectOptionOptions options);
default List<String> selectOption(String selector, ElementHandle value) {
return selectOption(selector, value, null);
}
default List<String> selectOption(String selector, ElementHandle value, SelectOptionOptions options) {
ElementHandle[] values = value == null ? null : new ElementHandle[]{value};
return selectOption(selector, values, options);
}
default List<String> selectOption(String selector, ElementHandle[] values) {
return selectOption(selector, values, null);
}
List<String> selectOption(String selector, ElementHandle[] values, SelectOptionOptions options);
default void setContent(String html) { default void setContent(String html) {
setContent(html, null); setContent(html, null);
} }

View File

@ -931,10 +931,45 @@ public interface Page {
return screenshot(null); return screenshot(null);
} }
byte[] screenshot(ScreenshotOptions options); byte[] screenshot(ScreenshotOptions options);
default List<String> selectOption(String selector, String values) { default List<String> selectOption(String selector, String value) {
return selectOption(selector, value, null);
}
default List<String> selectOption(String selector, String value, SelectOptionOptions options) {
String[] values = value == null ? null : new String[]{ value };
return selectOption(selector, values, options);
}
default List<String> selectOption(String selector, String[] values) {
return selectOption(selector, values, null); return selectOption(selector, values, null);
} }
List<String> selectOption(String selector, String values, SelectOptionOptions options); default List<String> selectOption(String selector, String[] values, SelectOptionOptions options) {
if (values == null) {
return selectOption(selector, new ElementHandle.SelectOption[0], options);
}
return selectOption(selector, Arrays.asList(values).stream().map(
v -> new ElementHandle.SelectOption().withValue(v)).toArray(ElementHandle.SelectOption[]::new), options);
}
default List<String> selectOption(String selector, ElementHandle.SelectOption value) {
return selectOption(selector, value, null);
}
default List<String> selectOption(String selector, ElementHandle.SelectOption value, SelectOptionOptions options) {
ElementHandle.SelectOption[] values = value == null ? null : new ElementHandle.SelectOption[]{value};
return selectOption(selector, values, options);
}
default List<String> selectOption(String selector, ElementHandle.SelectOption[] values) {
return selectOption(selector, values, null);
}
List<String> selectOption(String selector, ElementHandle.SelectOption[] values, SelectOptionOptions options);
default List<String> selectOption(String selector, ElementHandle value) {
return selectOption(selector, value, null);
}
default List<String> selectOption(String selector, ElementHandle value, SelectOptionOptions options) {
ElementHandle[] values = value == null ? null : new ElementHandle[]{value};
return selectOption(selector, values, options);
}
default List<String> selectOption(String selector, ElementHandle[] values) {
return selectOption(selector, values, null);
}
List<String> selectOption(String selector, ElementHandle[] values, SelectOptionOptions options);
default void setContent(String html) { default void setContent(String html) {
setContent(html, null); setContent(html, null);
} }

View File

@ -20,18 +20,17 @@ import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.microsoft.playwright.*; import com.microsoft.playwright.Deferred;
import com.microsoft.playwright.ElementHandle;
import com.microsoft.playwright.FileChooser;
import com.microsoft.playwright.Frame;
import java.io.DataOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.List; import java.util.List;
import static com.microsoft.playwright.impl.Serialization.deserialize; import static com.microsoft.playwright.impl.Serialization.*;
import static com.microsoft.playwright.impl.Serialization.serializeArgument;
import static com.microsoft.playwright.impl.Utils.isFunctionBody; import static com.microsoft.playwright.impl.Utils.isFunctionBody;
class ElementHandleImpl extends JSHandleImpl implements ElementHandle { class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
@ -277,9 +276,32 @@ class ElementHandleImpl extends JSHandleImpl implements ElementHandle {
} }
@Override @Override
public List<String> selectOption(String values, SelectOptionOptions options) { public List<String> selectOption(SelectOption[] values, SelectOptionOptions options) {
// TODO: if (options == null) {
return null; options = new SelectOptionOptions();
}
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
if (values != null) {
params.add("options", new Gson().toJsonTree(values));
}
return selectOption(params);
}
@Override
public List<String> selectOption(ElementHandle[] values, SelectOptionOptions options) {
if (options == null) {
options = new SelectOptionOptions();
}
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
if (values != null) {
params.add("elements", Serialization.toProtocol(values));
}
return selectOption(params);
}
private List<String> selectOption(JsonObject params) {
JsonObject json = sendMessage("selectOption", params).getAsJsonObject();
return parseStringList(json.getAsJsonArray("values"));
} }
@Override @Override

View File

@ -26,7 +26,6 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*; import java.util.*;
import static com.microsoft.playwright.Frame.LoadState.*; import static com.microsoft.playwright.Frame.LoadState.*;
@ -358,10 +357,35 @@ public class FrameImpl extends ChannelOwner implements Frame {
} }
@Override @Override
public List<String> selectOption(String selector, String values, SelectOptionOptions options) { public List<String> selectOption(String selector, ElementHandle.SelectOption[] values, SelectOptionOptions options) {
return null; if (options == null) {
options = new SelectOptionOptions();
}
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
params.addProperty("selector", selector);
if (values != null) {
params.add("options", new Gson().toJsonTree(values));
}
return selectOption(params);
} }
@Override
public List<String> selectOption(String selector, ElementHandle[] values, SelectOptionOptions options) {
if (options == null) {
options = new SelectOptionOptions();
}
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
params.addProperty("selector", selector);
if (values != null) {
params.add("elements", Serialization.toProtocol(values));
}
return selectOption(params);
}
private List<String> selectOption(JsonObject params) {
JsonObject json = sendMessage("selectOption", params).getAsJsonObject();
return parseStringList(json.getAsJsonArray("values"));
}
static String toProtocol(LoadState waitUntil) { static String toProtocol(LoadState waitUntil) {
if (waitUntil == null) { if (waitUntil == null) {

View File

@ -586,7 +586,12 @@ public class PageImpl extends ChannelOwner implements Page {
} }
@Override @Override
public List<String> selectOption(String selector, String values, SelectOptionOptions options) { public List<String> selectOption(String selector, ElementHandle.SelectOption[] values, SelectOptionOptions options) {
return mainFrame.selectOption(selector, values, convertViaJson(options, Frame.SelectOptionOptions.class));
}
@Override
public List<String> selectOption(String selector, ElementHandle[] values, SelectOptionOptions options) {
return mainFrame.selectOption(selector, values, convertViaJson(options, Frame.SelectOptionOptions.class)); return mainFrame.selectOption(selector, values, convertViaJson(options, Frame.SelectOptionOptions.class));
} }

View File

@ -18,16 +18,16 @@ package com.microsoft.playwright.impl;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.microsoft.playwright.FileChooser; import com.microsoft.playwright.*;
import com.microsoft.playwright.JSHandle;
import com.microsoft.playwright.Keyboard;
import com.microsoft.playwright.Mouse;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
class Serialization { class Serialization {
static SerializedError serializeError(Throwable e) { static SerializedError serializeError(Throwable e) {
@ -199,4 +199,22 @@ class Serialization {
} }
return jsonFiles; return jsonFiles;
} }
static JsonArray toProtocol(ElementHandle[] handles) {
JsonArray jsonElements = new JsonArray();
for (ElementHandle handle : handles) {
JsonObject jsonHandle = new JsonObject();
jsonHandle.addProperty("guid", ((ElementHandleImpl) handle).guid);
jsonElements.add(jsonHandle);
}
return jsonElements;
}
static List<String> parseStringList(JsonArray array) {
List<String> result = new ArrayList<>();
for (JsonElement e : array) {
result.add(e.getAsString());
}
return result;
}
} }

View File

@ -0,0 +1,236 @@
/*
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.playwright;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.junit.jupiter.api.Assertions.*;
public class TestPageSelectOption extends TestBase {
@Test
void shouldSelectSingleOption() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", "blue");
assertEquals(asList("blue"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("blue"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldSelectSingleOptionByValue() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", new ElementHandle.SelectOption().withValue("blue"));
assertEquals(asList("blue"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("blue"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldSelectSingleOptionByLabel() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", new ElementHandle.SelectOption().withLabel("Indigo"));
assertEquals(asList("indigo"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("indigo"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldSelectSingleOptionByHandle() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", page.querySelector("[id=whiteOption]"));
assertEquals(asList("white"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("white"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldSelectSingleOptionByIndex() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", new ElementHandle.SelectOption().withIndex(2));
assertEquals(asList("brown"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("brown"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldSelectSingleOptionByMultipleAttributes() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", new ElementHandle.SelectOption().withValue("green").withLabel("Green"));
assertEquals(asList("green"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("green"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldNotSelectSingleOptionWhenSomeAttributesDoNotMatch() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", new ElementHandle.SelectOption().withValue("green").withLabel("Brown"));
assertEquals("", page.evaluate("() => document.querySelector('select').value"));
}
@Test
void shouldSelectOnlyFirstOption() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", new String[]{"blue", "green", "red"});
assertEquals(asList("blue"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("blue"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldNotThrowWhenSelectCausesNavigation() {
page.navigate(server.PREFIX + "/input/select.html");
page.evalOnSelector("select", "select => select.addEventListener('input', () => window.location.href = '/empty.html')");
Deferred<Response> response = page.waitForNavigation();
page.selectOption("select", "blue");
response.get();
assertTrue(page.url().contains("empty.html"));
}
@Test
void shouldSelectMultipleOptions() {
page.navigate(server.PREFIX + "/input/select.html");
page.evaluate("() => window['makeMultiple']()");
page.selectOption("select", new String[]{"blue", "green", "red"});
assertEquals(asList("blue", "green", "red"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("blue", "green", "red"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldSelectMultipleOptionsWithAttributes() {
page.navigate(server.PREFIX + "/input/select.html");
page.evaluate("() => window['makeMultiple']()");
page.selectOption("select", new ElementHandle.SelectOption[] {
new ElementHandle.SelectOption().withValue("blue"),
new ElementHandle.SelectOption().withLabel("Green"),
new ElementHandle.SelectOption().withIndex(4),
});
assertEquals(asList("blue", "gray", "green"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("blue", "gray", "green"), page.evaluate("() => window['result'].onChange"));
}
@Test
void shouldRespectEventBubbling() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", "blue");
assertEquals(asList("blue"), page.evaluate("() => window['result'].onBubblingInput"));
assertEquals(asList("blue"), page.evaluate("() => window['result'].onBubblingChange"));
}
@Test
void shouldThrowWhenElementIsNotASelect() {
page.navigate(server.PREFIX + "/input/select.html");
try {
page.selectOption("body", "");
fail("did not throw");
} catch (RuntimeException e) {
assertTrue(e.getMessage().contains("Element is not a <select> element."));
}
}
@Test
void shouldReturnOnNoMatchedValues() {
page.navigate(server.PREFIX + "/input/select.html");
List<String> result = page.selectOption("select", new String[]{"42", "abc"});
assertEquals(emptyList(), result);
}
@Test
void shouldReturnAnArrayOfMatchedValues() {
page.navigate(server.PREFIX + "/input/select.html");
page.evaluate("() => window['makeMultiple']()");
List<String> result = page.selectOption("select", new String[]{"blue", "black", "magenta"});
Collections.sort(result);
List<String> expected = asList("blue","black","magenta");
Collections.sort(expected);
assertEquals(expected, result);
}
@Test
void shouldReturnAnArrayOfOneElementWhenMultipleIsNotSet() {
page.navigate(server.PREFIX + "/input/select.html");
List<String> result = page.selectOption("select", new String[]{"42", "blue", "black", "magenta"});
assertEquals(1, result.size());
}
@Test
void shouldReturnOnNoValues() {
page.navigate(server.PREFIX + "/input/select.html");
Object result = page.selectOption("select", new String[0]);
assertEquals(emptyList(), result);
}
// @Test
void shouldNotAllowNullItems() {
page.navigate(server.PREFIX + "/input/select.html");
page.evaluate("() => window['makeMultiple']()");
try {
page.selectOption("select", new String[]{"blue", null, "black","magenta"});
fail("did not throw");
} catch (RuntimeException e) {
assertTrue(e.getMessage().contains("options.get(1): expected object, got null"));
}
}
@Test
void shouldUnselectWithNull() {
page.navigate(server.PREFIX + "/input/select.html");
page.evaluate("() => window['makeMultiple']()");
List<String> result = page.selectOption("select", new String[]{"blue", "black", "magenta"});
Collections.sort(result);
List<String> expected = asList("blue","black","magenta");
Collections.sort(expected);
assertEquals(expected, result);
page.selectOption("select", (ElementHandle[]) null);
assertEquals(true, page.evalOnSelector("select", "select => Array.from(select.options).every(option => !option.selected)"));
page.selectOption("select", (String[]) null);
assertEquals(true, page.evalOnSelector("select", "select => Array.from(select.options).every(option => !option.selected)"));
page.selectOption("select", (ElementHandle.SelectOption[]) null);
assertEquals(true, page.evalOnSelector("select", "select => Array.from(select.options).every(option => !option.selected)"));
}
@Test
void shouldDeselectAllOptionsWhenPassedNoValuesForAMultipleSelect() {
page.navigate(server.PREFIX + "/input/select.html");
page.evaluate("() => window['makeMultiple']()");
page.selectOption("select", new String[]{"blue", "black", "magenta"});
page.selectOption("select", new String[0]);
assertEquals(true, page.evalOnSelector("select", "select => Array.from(select.options).every(option => !option.selected)"));
}
@Test
void shouldDeselectAllOptionsWhenPassedNoValuesForASelectWithoutMultiple() {
page.navigate(server.PREFIX + "/input/select.html");
page.selectOption("select", new String[]{"blue", "black", "magenta"});
page.selectOption("select", new String[0]);
assertEquals(true, page.evalOnSelector("select", "select => Array.from(select.options).every(option => !option.selected)"));
}
void shouldThrowIfPassedWrongTypes() {
// Checked by compiler in Java.
}
// @see https://github.com/GoogleChrome/puppeteer/issues/3327
@Test
void shouldWorkWhenReDefiningTopLevelEventClass() {
page.navigate(server.PREFIX + "/input/select.html");
page.evaluate("() => window.Event = null");
page.selectOption("select", "blue");
assertEquals(asList("blue"), page.evaluate("() => window['result'].onInput"));
assertEquals(asList("blue"), page.evaluate("() => window['result'].onChange"));
}
}