1
0
mirror of synced 2026-09-11 00:49:39 +00:00

fix: generate overloaded builders for options (#293)

This commit is contained in:
Yury Semikhatsky
2021-02-19 13:46:08 -08:00
committed by GitHub
parent b7a822ee41
commit ab5cd1c511
7 changed files with 69 additions and 62 deletions
@@ -765,9 +765,7 @@ public interface Frame {
/**
* URL string, URL regex pattern or predicate receiving [URL] to match while waiting for the navigation.
*/
public String glob;
public Pattern pattern;
public Predicate<String> predicate;
public Object url;
/**
* When to consider operation succeeded, defaults to {@code load}. Events can be either:
* - {@code 'domcontentloaded'} - consider operation to be finished when the {@code DOMContentLoaded} event is fired.
@@ -780,16 +778,16 @@ public interface Frame {
this.timeout = timeout;
return this;
}
public WaitForNavigationOptions withUrl(String glob) {
this.glob = glob;
public WaitForNavigationOptions withUrl(String url) {
this.url = url;
return this;
}
public WaitForNavigationOptions withUrl(Pattern pattern) {
this.pattern = pattern;
public WaitForNavigationOptions withUrl(Pattern url) {
this.url = url;
return this;
}
public WaitForNavigationOptions withUrl(Predicate<String> predicate) {
this.predicate = predicate;
public WaitForNavigationOptions withUrl(Predicate<String> url) {
this.url = url;
return this;
}
public WaitForNavigationOptions withWaitUntil(WaitUntilState waitUntil) {
@@ -1197,9 +1197,7 @@ public interface Page extends AutoCloseable {
/**
* A glob pattern, regex pattern or predicate receiving [URL] to match while waiting for the navigation.
*/
public String glob;
public Pattern pattern;
public Predicate<String> predicate;
public Object url;
/**
* When to consider operation succeeded, defaults to {@code load}. Events can be either:
* - {@code 'domcontentloaded'} - consider operation to be finished when the {@code DOMContentLoaded} event is fired.
@@ -1212,16 +1210,16 @@ public interface Page extends AutoCloseable {
this.timeout = timeout;
return this;
}
public WaitForNavigationOptions withUrl(String glob) {
this.glob = glob;
public WaitForNavigationOptions withUrl(String url) {
this.url = url;
return this;
}
public WaitForNavigationOptions withUrl(Pattern pattern) {
this.pattern = pattern;
public WaitForNavigationOptions withUrl(Pattern url) {
this.url = url;
return this;
}
public WaitForNavigationOptions withUrl(Predicate<String> predicate) {
this.predicate = predicate;
public WaitForNavigationOptions withUrl(Predicate<String> url) {
this.url = url;
return this;
}
public WaitForNavigationOptions withWaitUntil(WaitUntilState waitUntil) {
@@ -841,7 +841,7 @@ public class FrameImpl extends ChannelOwner implements Frame {
}
List<Waitable<Response>> waitables = new ArrayList<>();
UrlMatcher matcher = UrlMatcher.forOneOf(options.glob, options.pattern, options.predicate);
UrlMatcher matcher = UrlMatcher.forOneOf(options.url);
waitables.add(new WaitForNavigationHelper(matcher, convertViaJson(options.waitUntil, LoadState.class)));
waitables.add(page.createWaitForCloseHelper());
waitables.add(page.createWaitableFrameDetach(this));
@@ -1169,9 +1169,7 @@ public class PageImpl extends ChannelOwner implements Page {
if (options != null) {
frameOptions.timeout = options.timeout;
frameOptions.waitUntil = options.waitUntil;
frameOptions.glob = options.glob;
frameOptions.pattern = options.pattern;
frameOptions.predicate = options.predicate;
frameOptions.url = options.url;
}
return mainFrame.waitForNavigationImpl(code, frameOptions);
}
@@ -16,6 +16,8 @@
package com.microsoft.playwright.impl;
import com.microsoft.playwright.PlaywrightException;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Pattern;
@@ -34,25 +36,20 @@ class UrlMatcher {
return new UrlMatcher(null, null);
}
static UrlMatcher forOneOf(String glob, Pattern pattern, Predicate<String> predicate) {
UrlMatcher result = UrlMatcher.any();
int conditionCount = 0;
if (glob != null) {
conditionCount += 1;
result = new UrlMatcher(glob);
static UrlMatcher forOneOf(Object object) {
if (object == null) {
return UrlMatcher.any();
}
if (pattern != null) {
conditionCount += 1;
result = new UrlMatcher(pattern);
if (object instanceof String) {
return new UrlMatcher((String) object);
}
if (predicate != null) {
conditionCount += 1;
result = new UrlMatcher(predicate);
if (object instanceof Pattern) {
return new UrlMatcher((Pattern) object);
}
if (conditionCount > 1) {
throw new IllegalArgumentException("Only one of glob, pattern and predicate can be specified");
if (object instanceof Predicate) {
return new UrlMatcher((Predicate<String>) object);
}
return result;
throw new PlaywrightException("Url must be String, Pattern or Predicate<String>, found: " + object.getClass().getTypeName());
}
UrlMatcher(String url) {
@@ -235,5 +235,29 @@ public class TestPageWaitForNavigation extends TestBase {
}
}
@Test
void shouldThrowOnInvalidUrlMatcherTypeInPage() {
try {
Page.WaitForNavigationOptions options = new Page.WaitForNavigationOptions();
options.url = new Object();
page.waitForNavigation(options, () -> {});
fail("did not throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("Url must be String, Pattern or Predicate<String>"));
}
}
@Test
void shouldThrowOnInvalidUrlMatcherTypeInFrame() {
page.navigate(server.PREFIX + "/frames/one-frame.html");
Frame frame = page.frames().get(1);
try {
Frame.WaitForNavigationOptions options = new Frame.WaitForNavigationOptions();
options.url = new Object();
frame.waitForNavigation(options, () -> {});
fail("did not throw");
} catch (PlaywrightException e) {
assertTrue(e.getMessage().contains("Url must be String, Pattern or Predicate<String>"));
}
}
}
@@ -306,6 +306,9 @@ class TypeRef extends Element {
String name = jsonType.get("name").getAsString();
if (jsonType.has("union")) {
if (name.isEmpty()) {
if (parent instanceof Field) {
return "Object";
}
throw new RuntimeException("Unexpected enum without name: " + jsonType);
}
return name;
@@ -690,13 +693,6 @@ class Field extends Element {
void writeTo(List<String> output, String offset) {
writeJavadoc(output, offset, comment());
if (asList("Frame.waitForNavigation.options.url",
"Page.waitForNavigation.options.url").contains(jsonPath)) {
output.add(offset + "public String glob;");
output.add(offset + "public Pattern pattern;");
output.add(offset + "public Predicate<String> predicate;");
return;
}
String typeStr = type.toJava();
if (type.isNullable()) {
typeStr = "Optional<" + typeStr + ">";
@@ -715,20 +711,10 @@ class Field extends Element {
}
void writeBuilderMethod(List<String> output, String offset, String parentClass) {
if (asList("Frame.waitForNavigation.options.url",
"Page.waitForNavigation.options.url").contains(jsonPath)) {
output.add(offset + "public WaitForNavigationOptions withUrl(String glob) {");
output.add(offset + " this.glob = glob;");
output.add(offset + " return this;");
output.add(offset + "}");
output.add(offset + "public WaitForNavigationOptions withUrl(Pattern pattern) {");
output.add(offset + " this.pattern = pattern;");
output.add(offset + " return this;");
output.add(offset + "}");
output.add(offset + "public WaitForNavigationOptions withUrl(Predicate<String> predicate) {");
output.add(offset + " this.predicate = predicate;");
output.add(offset + " return this;");
output.add(offset + "}");
if (type.customType == null && type.isTypeUnion()) {
for (int i = 0; i < type.unionSize(); i++) {
writeGenericBuilderMethod(output, offset, parentClass, type.formatTypeFromUnion(i));
}
return;
}
if (asList("Page.click.options.position",
@@ -781,13 +767,19 @@ class Field extends Element {
} else if ("Double".equals(paramType)) {
paramType = "double";
}
output.add(offset + "public " + parentClass + " with" + toTitle(name) + "(" + paramType + " " + name + ") {");
String rvalue = type.isNullable() ? "Optional.ofNullable(" + name + ")" : name;
output.add(offset + " this." + name + " = " + rvalue + ";");
output.add(offset + " return this;");
writeGenericBuilderMethod(output, offset, parentClass, paramType);
return;
}
output.add(offset + "}");
}
private void writeGenericBuilderMethod(List<String> output, String offset, String parentClass, String paramType) {
output.add(offset + "public " + parentClass + " with" + toTitle(name) + "(" + paramType + " " + name + ") {");
String rvalue = type.isNullable() ? "Optional.ofNullable(" + name + ")" : name;
output.add(offset + " this." + name + " = " + rvalue + ";");
output.add(offset + " return this;");
output.add(offset + "}");
}
}
class Interface extends TypeDefinition {