feat: support page.frame(predicate)
This commit is contained in:
@@ -490,7 +490,13 @@ class Interface extends TypeDefinition {
|
||||
void writeTo(List<String> output, String offset) {
|
||||
output.add(header);
|
||||
output.add("import java.util.*;");
|
||||
output.add("import java.util.function.BiConsumer;");
|
||||
if (jsonName.equals("Page")) {
|
||||
output.add("import java.util.function.BiConsumer;");
|
||||
output.add("import java.util.function.Predicate;");
|
||||
output.add("import java.util.regex.Pattern;");
|
||||
} else if (jsonName.equals("BrowserContext")) {
|
||||
output.add("import java.util.function.BiConsumer;");
|
||||
}
|
||||
output.add("");
|
||||
|
||||
String implementsClause = "";
|
||||
@@ -563,6 +569,31 @@ class Interface extends TypeDefinition {
|
||||
output.add("");
|
||||
output.add(offset + " Object call(Source source, Object... args);");
|
||||
output.add(offset + "}");
|
||||
output.add("");
|
||||
|
||||
output.add(offset + "class FrameOptions {");
|
||||
output.add(offset + " public String name;");
|
||||
output.add(offset + " public String url;");
|
||||
output.add(offset + " public Pattern urlPattern;");
|
||||
output.add(offset + " public Predicate<String> urlPredicate;");
|
||||
output.add("");
|
||||
output.add(offset + " FrameOptions withName(String name) {");
|
||||
output.add(offset + " this.name = name;");
|
||||
output.add(offset + " return this;");
|
||||
output.add(offset + " }");
|
||||
output.add(offset + " FrameOptions withUrl(String url) {");
|
||||
output.add(offset + " this.url = url;");
|
||||
output.add(offset + " return this;");
|
||||
output.add(offset + " }");
|
||||
output.add(offset + " FrameOptions withUrl(Pattern pattern) {");
|
||||
output.add(offset + " urlPattern = pattern;");
|
||||
output.add(offset + " return this;");
|
||||
output.add(offset + " }");
|
||||
output.add(offset + " FrameOptions withUrl(Predicate<String> predicate) {");
|
||||
output.add(offset + " urlPredicate = predicate;");
|
||||
output.add(offset + " return this;");
|
||||
output.add(offset + " }");
|
||||
output.add(offset + "}");
|
||||
break;
|
||||
}
|
||||
case "BrowserContext": {
|
||||
|
||||
@@ -205,7 +205,7 @@ class Types {
|
||||
add("BrowserContext.unroute.url", "string|RegExp|function(URL):boolean", "String");
|
||||
add("BrowserContext.waitForEvent.optionsOrPredicate", "Function|Object", "String");
|
||||
add("Page.waitForNavigation.options.url", "string|RegExp|Function", "String");
|
||||
add("Page.frame.options", "string|Object", "String");
|
||||
add("Page.frame.options", "string|Object", "FrameOptions", new Empty());
|
||||
add("Page.route.url", "string|RegExp|function(URL):boolean", "String");
|
||||
add("Page.selectOption.values", "null|string|ElementHandle|Array<string>|Object|Array<ElementHandle>|Array<Object>", "String");
|
||||
add("Page.setInputFiles.files", "string|Array<string>|Object|Array<Object>", "String");
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Accessibility {
|
||||
class SnapshotOptions {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Browser {
|
||||
class NewContextOptions {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface BrowserServer {
|
||||
void close();
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface BrowserType {
|
||||
class ConnectOptions {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface CDPSession {
|
||||
void detach();
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface ChromiumBrowser extends Browser {
|
||||
class StartTracingOptions {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface ChromiumBrowserContext extends BrowserContext {
|
||||
List<Page> backgroundPages();
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface ChromiumCoverage {
|
||||
class StartCSSCoverageOptions {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface ConsoleMessage {
|
||||
class Location {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Dialog {
|
||||
default void accept() {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Download {
|
||||
Readable createReadStream();
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface ElementHandle extends JSHandle {
|
||||
enum ElementState { DISABLED, ENABLED, HIDDEN, STABLE, VISIBLE }
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface FileChooser {
|
||||
class SetFilesOptions {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface FirefoxBrowser extends Browser {
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Frame {
|
||||
enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface JSHandle {
|
||||
ElementHandle asElement();
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Keyboard {
|
||||
enum Modifier { ALT, CONTROL, META, SHIFT }
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Logger {
|
||||
enum Severity { ERROR, INFO, VERBOSE, WARNING }
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Mouse {
|
||||
enum Button { LEFT, MIDDLE, RIGHT }
|
||||
|
||||
@@ -18,6 +18,8 @@ package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public interface Page {
|
||||
class Viewport {
|
||||
@@ -52,6 +54,30 @@ public interface Page {
|
||||
Object call(Source source, Object... args);
|
||||
}
|
||||
|
||||
class FrameOptions {
|
||||
public String name;
|
||||
public String url;
|
||||
public Pattern urlPattern;
|
||||
public Predicate<String> urlPredicate;
|
||||
|
||||
FrameOptions withName(String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
FrameOptions withUrl(String url) {
|
||||
this.url = url;
|
||||
return this;
|
||||
}
|
||||
FrameOptions withUrl(Pattern pattern) {
|
||||
urlPattern = pattern;
|
||||
return this;
|
||||
}
|
||||
FrameOptions withUrl(Predicate<String> predicate) {
|
||||
urlPredicate = predicate;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
enum LoadState { DOMCONTENTLOADED, LOAD, NETWORKIDLE }
|
||||
class CloseOptions {
|
||||
public Boolean runBeforeUnload;
|
||||
@@ -815,7 +841,7 @@ public interface Page {
|
||||
focus(selector, null);
|
||||
}
|
||||
void focus(String selector, FocusOptions options);
|
||||
Frame frame(String options);
|
||||
Frame frame(FrameOptions options);
|
||||
List<Frame> frames();
|
||||
default String getAttribute(String selector, String name) {
|
||||
return getAttribute(selector, name, null);
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Request {
|
||||
class RequestFailure {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Response {
|
||||
byte[] body();
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Route {
|
||||
class ContinueOverrides {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Selectors {
|
||||
class RegisterOptions {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface TimeoutError {
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface WebKitBrowser extends Browser {
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.microsoft.playwright;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
public interface Worker {
|
||||
default Object evaluate(String pageFunction) {
|
||||
|
||||
@@ -125,10 +125,9 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
|
||||
@Override
|
||||
public List<Frame> childFrames() {
|
||||
return null;
|
||||
return new ArrayList<>(childFrames);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void click(String selector, ClickOptions options) {
|
||||
if (options == null) {
|
||||
@@ -210,7 +209,12 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
|
||||
@Override
|
||||
public void focus(String selector, FocusOptions options) {
|
||||
|
||||
if (options == null) {
|
||||
options = new FocusOptions();
|
||||
}
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("selector", selector);
|
||||
sendMessage("focus", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -284,7 +288,13 @@ public class FrameImpl extends ChannelOwner implements Frame {
|
||||
|
||||
@Override
|
||||
public void press(String selector, String key, PressOptions options) {
|
||||
|
||||
if (options == null) {
|
||||
options = new PressOptions();
|
||||
}
|
||||
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
|
||||
params.addProperty("selector", selector);
|
||||
params.addProperty("key", key);
|
||||
sendMessage("press", params);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -268,11 +268,28 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
|
||||
@Override
|
||||
public void focus(String selector, FocusOptions options) {
|
||||
|
||||
mainFrame.focus(selector, convertViaJson(options, Frame.FocusOptions.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frame frame(String options) {
|
||||
public Frame frame(FrameOptions options) {
|
||||
if (options == null) {
|
||||
throw new IllegalArgumentException("Frame criteria should be cpecified");
|
||||
}
|
||||
for (Frame frame : frames) {
|
||||
if (options.name != null && options.name.equals(frame.name())) {
|
||||
return frame;
|
||||
}
|
||||
if (options.url != null && options.url.equals(frame.url())) {
|
||||
return frame;
|
||||
}
|
||||
if (options.urlPattern != null && options.urlPattern.matcher(frame.url()).matches()) {
|
||||
return frame;
|
||||
}
|
||||
if (options.urlPredicate != null && options.urlPredicate.test(frame.url())) {
|
||||
return frame;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -352,7 +369,7 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
|
||||
@Override
|
||||
public void press(String selector, String key, PressOptions options) {
|
||||
|
||||
mainFrame.press(selector, key, convertViaJson(options, Frame.PressOptions.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,6 +19,10 @@ package com.microsoft.playwright;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.microsoft.playwright.Page.LoadState.DOMCONTENTLOADED;
|
||||
import static com.microsoft.playwright.Page.LoadState.LOAD;
|
||||
@@ -246,4 +250,63 @@ public class TestPageBasic {
|
||||
void pageContextShouldReturnTheCorrectInstance() {
|
||||
assertEquals(context, page.context());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageFrameShouldRespectName() {
|
||||
page.setContent("<iframe name=target></iframe>");
|
||||
assertNull(page.frame(new Page.FrameOptions().withName("bogus")));
|
||||
Frame frame = page.frame(new Page.FrameOptions().withName("target"));
|
||||
assertNotNull(frame);
|
||||
assertEquals(page.mainFrame().childFrames().get(0), frame);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pageFrameShouldRespectUrl() {
|
||||
page.setContent("<iframe src='" + server.EMPTY_PAGE + "'></iframe>");
|
||||
assertNull(page.frame(new Page.FrameOptions().withUrl(Pattern.compile("bogus"))));
|
||||
Frame frame = page.frame(new Page.FrameOptions().withUrl(Pattern.compile(".*empty.*")));
|
||||
assertNotNull(frame);
|
||||
assertEquals(server.EMPTY_PAGE, frame.url());
|
||||
}
|
||||
|
||||
// TODO:
|
||||
void shouldHaveSaneUserAgent() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void pagePressShouldWork() {
|
||||
page.navigate(server.PREFIX + "/input/textarea.html");
|
||||
page.press("textarea", "a");
|
||||
assertEquals("a", page.evaluate("() => document.querySelector('textarea').value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void pagePressShouldWorkForEnter() {
|
||||
page.setContent("<input onkeypress='console.log(\"press\")'></input>");
|
||||
List<ConsoleMessage> messages = new ArrayList<>();
|
||||
page.addConsoleListener(message -> messages.add(message));
|
||||
page.press("input", "Enter");
|
||||
assertEquals("press", messages.get(0).text());
|
||||
}
|
||||
|
||||
@Test
|
||||
void framePressShouldWork() {
|
||||
page.setContent("<iframe name=inner src='" + server.PREFIX + "/input/textarea.html'></iframe>");
|
||||
Frame frame = page.frame(new Page.FrameOptions().withName("inner"));
|
||||
frame.press("textarea", "a");
|
||||
assertEquals("a", frame.evaluate("() => document.querySelector('textarea').value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void frameFocusShouldWorkMultipleTimes() {
|
||||
// TODO: test.fail(browserName === "firefox");
|
||||
Page page1 = context.newPage();
|
||||
Page page2 = context.newPage();
|
||||
for (Page page : Arrays.asList(page1, page2)) {
|
||||
page.setContent("<button id='foo' onfocus='window.gotFocus=true'></button>");
|
||||
page.focus("#foo");
|
||||
assertEquals(true, page.evaluate("() => !!window['gotFocus']"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user