fix: split page.frame() into overloaded methods (#11)
This commit is contained in:
@@ -307,6 +307,12 @@ class Method extends Element {
|
||||
"void route(Pattern url, BiConsumer<Route, Request> handler);",
|
||||
"void route(Predicate<String> url, BiConsumer<Route, Request> handler);",
|
||||
});
|
||||
customSignature.put("Page.frame", new String[]{
|
||||
"Frame frameByName(String name);",
|
||||
"Frame frameByUrl(String glob);",
|
||||
"Frame frameByUrl(Pattern pattern);",
|
||||
"Frame frameByUrl(Predicate<String> predicate);",
|
||||
});
|
||||
customSignature.put("Page.route", new String[]{
|
||||
"void route(String url, BiConsumer<Route, Request> handler);",
|
||||
"void route(Pattern url, BiConsumer<Route, Request> handler);",
|
||||
@@ -614,30 +620,6 @@ class Interface extends TypeDefinition {
|
||||
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": {
|
||||
|
||||
@@ -54,29 +54,6 @@ 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 EventType {
|
||||
CLOSE,
|
||||
@@ -853,7 +830,10 @@ public interface Page {
|
||||
focus(selector, null);
|
||||
}
|
||||
void focus(String selector, FocusOptions options);
|
||||
Frame frame(FrameOptions options);
|
||||
Frame frameByName(String name);
|
||||
Frame frameByUrl(String glob);
|
||||
Frame frameByUrl(Pattern pattern);
|
||||
Frame frameByUrl(Predicate<String> predicate);
|
||||
List<Frame> frames();
|
||||
default String getAttribute(String selector, String name) {
|
||||
return getAttribute(selector, name, null);
|
||||
|
||||
@@ -267,21 +267,33 @@ public class PageImpl extends ChannelOwner implements Page {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frame frame(FrameOptions options) {
|
||||
if (options == null) {
|
||||
throw new IllegalArgumentException("Frame criteria should be specified");
|
||||
}
|
||||
public Frame frameByName(String name) {
|
||||
for (Frame frame : frames) {
|
||||
if (options.name != null && options.name.equals(frame.name())) {
|
||||
if (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 null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frame frameByUrl(String glob) {
|
||||
return frameFor(new UrlMatcher(glob));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frame frameByUrl(Pattern pattern) {
|
||||
return frameFor(new UrlMatcher(pattern));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Frame frameByUrl(Predicate<String> predicate) {
|
||||
return frameFor(new UrlMatcher(predicate));
|
||||
}
|
||||
|
||||
private Frame frameFor(UrlMatcher matcher) {
|
||||
for (Frame frame : frames) {
|
||||
if (matcher.test(frame.url())) {
|
||||
return frame;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -259,8 +259,8 @@ public class TestPageBasic {
|
||||
@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"));
|
||||
assertNull(page.frameByName("bogus"));
|
||||
Frame frame = page.frameByName("target");
|
||||
assertNotNull(frame);
|
||||
assertEquals(page.mainFrame().childFrames().get(0), frame);
|
||||
}
|
||||
@@ -268,8 +268,8 @@ public class TestPageBasic {
|
||||
@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.*")));
|
||||
assertNull(page.frameByUrl(Pattern.compile("bogus")));
|
||||
Frame frame = page.frameByUrl(Pattern.compile(".*empty.*"));
|
||||
assertNotNull(frame);
|
||||
assertEquals(server.EMPTY_PAGE, frame.url());
|
||||
}
|
||||
@@ -321,7 +321,7 @@ public class TestPageBasic {
|
||||
@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 frame = page.frameByName("inner");
|
||||
frame.press("textarea", "a");
|
||||
assertEquals("a", frame.evaluate("() => document.querySelector('textarea').value"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user