1
0
mirror of synced 2026-08-05 15:06:54 +00:00

feat(api): implement basic interfaces, add a test

This commit is contained in:
Yury Semikhatsky
2020-09-28 22:25:35 -07:00
parent d12f982828
commit 4771dc2850
33 changed files with 1568 additions and 471 deletions
@@ -74,7 +74,7 @@ class TypeRef extends Element {
void createCustomType() {
boolean isEnum = jsonName.contains("|\"");
boolean isClass = jsonName.equals("Object");
boolean isClass = jsonName.replace("null|", "").equals("Object");
// Use path to the corresponding method, param of field as the key.
String parentPath = parent.jsonPath;
Types.Mapping mapping = TypeDefinition.types.findForPath(parentPath);
@@ -238,6 +238,11 @@ class Types {
add("WebKitBrowser.newPage.options.geolocation.longitude", "number", "double");
add("WebKitBrowser.newPage.options.geolocation.accuracy", "number", "double");
add("BrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation");
add("ChromiumBrowserContext.setGeolocation.geolocation", "null|Object", "Geolocation");
add("BrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials");
add("ChromiumBrowserContext.setHTTPCredentials.httpCredentials", "null|Object", "HTTPCredentials");
add("Page.setViewportSize.viewportSize", "Object", "ViewportSize");
// node.js types
add("BrowserServer.process", "ChildProcess", "Object");
+6
View File
@@ -31,5 +31,11 @@
<version>2.8.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -22,6 +22,25 @@ import java.util.function.BiConsumer;
public interface Browser {
class NewContextOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewContextOptions done() {
return NewContextOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -87,7 +106,7 @@ public interface Browser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -119,9 +138,9 @@ public interface Browser {
this.bypassCSP = bypassCSP;
return this;
}
public NewContextOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewContextOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -198,6 +217,25 @@ public interface Browser {
}
class NewPageOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewPageOptions done() {
return NewPageOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -263,7 +301,7 @@ public interface Browser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -295,9 +333,9 @@ public interface Browser {
this.bypassCSP = bypassCSP;
return this;
}
public NewPageOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewPageOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -28,6 +28,37 @@ public interface BrowserContext {
return this;
}
}
class Geolocation {
int latitude;
int longitude;
Integer accuracy;
public Geolocation withLatitude(int latitude) {
this.latitude = latitude;
return this;
}
public Geolocation withLongitude(int longitude) {
this.longitude = longitude;
return this;
}
public Geolocation withAccuracy(Integer accuracy) {
this.accuracy = accuracy;
return this;
}
}
class HTTPCredentials {
String username;
String password;
public HTTPCredentials withUsername(String username) {
this.username = username;
return this;
}
public HTTPCredentials withPassword(String password) {
this.password = password;
return this;
}
}
void close();
void addCookies(List<Object> cookies);
void addInitScript(String script, Object arg);
@@ -44,8 +75,8 @@ public interface BrowserContext {
void setDefaultNavigationTimeout(int timeout);
void setDefaultTimeout(int timeout);
void setExtraHTTPHeaders(Map<String, String> headers);
void setGeolocation(Object geolocation);
void setHTTPCredentials(Object httpCredentials);
void setGeolocation(Geolocation geolocation);
void setHTTPCredentials(HTTPCredentials httpCredentials);
void setOffline(boolean offline);
void unroute(String url, BiConsumer<Route, Request> handler);
Object waitForEvent(String event, String optionsOrPredicate);
@@ -191,6 +191,25 @@ public interface BrowserType {
return this;
}
}
public class Viewport {
int width;
int height;
Viewport() {
}
public LaunchPersistentContextOptions done() {
return LaunchPersistentContextOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -272,7 +291,7 @@ public interface BrowserType {
Integer slowMo;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -367,9 +386,9 @@ public interface BrowserType {
this.bypassCSP = bypassCSP;
return this;
}
public LaunchPersistentContextOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public LaunchPersistentContextOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -40,6 +40,25 @@ public interface ChromiumBrowser {
}
class NewContextOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewContextOptions done() {
return NewContextOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -105,7 +124,7 @@ public interface ChromiumBrowser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -137,9 +156,9 @@ public interface ChromiumBrowser {
this.bypassCSP = bypassCSP;
return this;
}
public NewContextOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewContextOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -216,6 +235,25 @@ public interface ChromiumBrowser {
}
class NewPageOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewPageOptions done() {
return NewPageOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -281,7 +319,7 @@ public interface ChromiumBrowser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -313,9 +351,9 @@ public interface ChromiumBrowser {
this.bypassCSP = bypassCSP;
return this;
}
public NewPageOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewPageOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -28,6 +28,37 @@ public interface ChromiumBrowserContext {
return this;
}
}
class Geolocation {
int latitude;
int longitude;
Integer accuracy;
public Geolocation withLatitude(int latitude) {
this.latitude = latitude;
return this;
}
public Geolocation withLongitude(int longitude) {
this.longitude = longitude;
return this;
}
public Geolocation withAccuracy(Integer accuracy) {
this.accuracy = accuracy;
return this;
}
}
class HTTPCredentials {
String username;
String password;
public HTTPCredentials withUsername(String username) {
this.username = username;
return this;
}
public HTTPCredentials withPassword(String password) {
this.password = password;
return this;
}
}
List<Page> backgroundPages();
CDPSession newCDPSession(Page page);
List<Worker> serviceWorkers();
@@ -47,8 +78,8 @@ public interface ChromiumBrowserContext {
void setDefaultNavigationTimeout(int timeout);
void setDefaultTimeout(int timeout);
void setExtraHTTPHeaders(Map<String, String> headers);
void setGeolocation(Object geolocation);
void setHTTPCredentials(Object httpCredentials);
void setGeolocation(Geolocation geolocation);
void setHTTPCredentials(HTTPCredentials httpCredentials);
void setOffline(boolean offline);
void unroute(String url, BiConsumer<Route, Request> handler);
Object waitForEvent(String event, String optionsOrPredicate);
@@ -22,6 +22,25 @@ import java.util.function.BiConsumer;
public interface FirefoxBrowser {
class NewContextOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewContextOptions done() {
return NewContextOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -87,7 +106,7 @@ public interface FirefoxBrowser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -119,9 +138,9 @@ public interface FirefoxBrowser {
this.bypassCSP = bypassCSP;
return this;
}
public NewContextOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewContextOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -198,6 +217,25 @@ public interface FirefoxBrowser {
}
class NewPageOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewPageOptions done() {
return NewPageOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -263,7 +301,7 @@ public interface FirefoxBrowser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -295,9 +333,9 @@ public interface FirefoxBrowser {
this.bypassCSP = bypassCSP;
return this;
}
public NewPageOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewPageOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -606,15 +606,15 @@ public interface Page {
return this;
}
}
class SetViewportSizeViewportSize {
class ViewportSize {
int width;
int height;
public SetViewportSizeViewportSize withWidth(int width) {
public ViewportSize withWidth(int width) {
this.width = width;
return this;
}
public SetViewportSizeViewportSize withHeight(int height) {
public ViewportSize withHeight(int height) {
this.height = height;
return this;
}
@@ -663,6 +663,19 @@ public interface Page {
return this;
}
}
class PageViewportSize {
int width;
int height;
public PageViewportSize withWidth(int width) {
this.width = width;
return this;
}
public PageViewportSize withHeight(int height) {
this.height = height;
return this;
}
}
class WaitForFunctionOptions {
double polling;
Integer timeout;
@@ -778,14 +791,14 @@ public interface Page {
void setDefaultTimeout(int timeout);
void setExtraHTTPHeaders(Map<String, String> headers);
void setInputFiles(String selector, String files, SetInputFilesOptions options);
void setViewportSize(SetViewportSizeViewportSize viewportSize);
void setViewportSize(ViewportSize viewportSize);
String textContent(String selector, TextContentOptions options);
String title();
void type(String selector, String text, TypeOptions options);
void uncheck(String selector, UncheckOptions options);
void unroute(String url, BiConsumer<Route, Request> handler);
String url();
Object viewportSize();
PageViewportSize viewportSize();
Object waitForEvent(String event, String optionsOrPredicate);
JSHandle waitForFunction(String pageFunction, Object arg, WaitForFunctionOptions options);
void waitForLoadState(LoadState state, WaitForLoadStateOptions options);
@@ -1,12 +1,12 @@
/**
* Copyright (c) Microsoft Corporation.
*
* <p>
* 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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.
@@ -14,12 +14,18 @@
* limitations under the License.
*/
package com.microsoft.playwright.impl;
package com.microsoft.playwright;
import com.google.gson.JsonObject;
import com.microsoft.playwright.impl.PlaywrightImpl;
public class Request extends ChannelOwner {
Request(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
import java.io.IOException;
public interface Playwright {
static Playwright create() {
return PlaywrightImpl.create();
}
BrowserType chromium();
BrowserType firefox();
BrowserType webkit();
}
@@ -20,14 +20,25 @@ import java.util.*;
import java.util.function.BiConsumer;
public interface Request {
Object failure();
class RequestFailure {
String errorText;
public RequestFailure withErrorText(String errorText) {
this.errorText = errorText;
return this;
}
}
class RequestPostDataJSON {
}
RequestFailure failure();
Frame frame();
Map<String, String> headers();
boolean isNavigationRequest();
String method();
String postData();
byte[] postDataBuffer();
Object postDataJSON();
RequestPostDataJSON postDataJSON();
Request redirectedFrom();
Request redirectedTo();
String resourceType();
@@ -22,6 +22,25 @@ import java.util.function.BiConsumer;
public interface WebKitBrowser {
class NewContextOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewContextOptions done() {
return NewContextOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -87,7 +106,7 @@ public interface WebKitBrowser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -119,9 +138,9 @@ public interface WebKitBrowser {
this.bypassCSP = bypassCSP;
return this;
}
public NewContextOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewContextOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -198,6 +217,25 @@ public interface WebKitBrowser {
}
class NewPageOptions {
enum ColorScheme { DARK, LIGHT, NO_PREFERENCE}
public class Viewport {
int width;
int height;
Viewport() {
}
public NewPageOptions done() {
return NewPageOptions.this;
}
public Viewport withWidth(int width) {
this.width = width;
return this;
}
public Viewport withHeight(int height) {
this.height = height;
return this;
}
}
public class Geolocation {
double latitude;
double longitude;
@@ -263,7 +301,7 @@ public interface WebKitBrowser {
Boolean acceptDownloads;
Boolean ignoreHTTPSErrors;
Boolean bypassCSP;
Object viewport;
Viewport viewport;
String userAgent;
Integer deviceScaleFactor;
Boolean isMobile;
@@ -295,9 +333,9 @@ public interface WebKitBrowser {
this.bypassCSP = bypassCSP;
return this;
}
public NewPageOptions withViewport(Object viewport) {
this.viewport = viewport;
return this;
public Viewport setViewport() {
this.viewport = new Viewport();
return this.viewport;
}
public NewPageOptions withUserAgent(String userAgent) {
this.userAgent = userAgent;
@@ -0,0 +1,105 @@
/**
* 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.example;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.microsoft.playwright.*;
import com.microsoft.playwright.impl.*;
import java.io.*;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
Playwright playwright = Playwright.create();
BrowserType.LaunchOptions options = new BrowserType.LaunchOptions()
// .withSlowMo(1000)
.withHeadless(false);
System.out.println("options = " + new Gson().toJson(options));
Browser browser = playwright.chromium().launch(options);
System.out.println("browser = " + browser);
Browser.NewContextOptions contextOptions = new Browser.NewContextOptions();
contextOptions.setViewport().withWidth(800).withHeight(600);
BrowserContext context = browser.newContext(contextOptions);
Page page = context.newPage();
// page.navigate("http://example.com", null);
page.navigate("https://webkit.org", null);
page.click("text=web browser engine", new Page.ClickOptions());
// Supplier<PageImpl> popupSupplier = page.waitForPopup();
// Supplier<PageImpl> pageSupplier = context.waitForPage();
page.evaluate("window.open('http://example.com'); 13", null);
{
Object r = page.evaluate("function foo(a) { return a + 1; }", 20);
System.out.println("r = " + new Gson().toJson(r));
}
// {
// List<Integer> r = page.evalTyped("function foo() { return [1,2,3]; }");
// System.out.println("r = " + new Gson().toJson(r));
// int p = r.get(0).intValue() + 1;
// }
//
// {
// int r = page.evalTyped("function foo() { return 7; }");
// System.out.println("int r = " + new Gson().toJson(r));
// }
//
// {
// double r = page.evalTyped("function foo() { return 7.2; }");
// System.out.println("double r = " + new Gson().toJson(r));
// }
//
// PageImpl popup = popupSupplier.get();
// System.out.println("popup = " + popup);
// PageImpl page2 = pageSupplier.get();
// System.out.println(page2 == popup);
//
//
//
//
// page.addDialogHandler(d -> {
// System.out.println("Got dialog type: " + d.type());
// System.out.println(" message = " + d.message());
// d.accept("abc");
// });
// page.evaluate("alert('Hi there!')");
// System.out.println("After alert");
//
//
//
// page.addConsoleListener(m -> {
// System.out.println("Got console message type: " + m.type());
// System.out.println(" text = " + m.text());
// System.out.println(" location = " + m.location());
// });
// page.evaluate("console.log('A message')");
// Thread.sleep(1000);
browser.close();
// Disconnect and terminate the threads?
// playwright.close();
System.out.println("\nDONE.");
System.exit(0);
}
}
@@ -1,44 +0,0 @@
/**
* 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.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.function.Supplier;
public class BrowserContext extends ChannelOwner {
protected BrowserContext(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
Page newPage() {
JsonObject params = new JsonObject();
JsonElement result = sendMessage("newPage", params);
return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("page").get("guid").getAsString());
}
public Supplier<Page> waitForPage() {
Supplier<JsonObject> pageSupplier = waitForEvent("page");
return () -> {
JsonObject params = pageSupplier.get();
String guid = params.getAsJsonObject("page").get("guid").getAsString();
return connection.getExistingObject(guid);
};
}
}
@@ -0,0 +1,150 @@
/**
* 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.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
class BrowserContextImpl extends ChannelOwner implements BrowserContext {
protected BrowserContextImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@Override
public void close() {
}
@Override
public void addCookies(List<Object> cookies) {
}
@Override
public void addInitScript(String script, Object arg) {
}
@Override
public Browser browser() {
return null;
}
@Override
public void clearCookies() {
}
@Override
public void clearPermissions() {
}
@Override
public List<Object> cookies(String urls) {
return null;
}
@Override
public void exposeBinding(String name, String playwrightBinding) {
}
@Override
public void exposeFunction(String name, String playwrightFunction) {
}
@Override
public void grantPermissions(List<String> permissions, GrantPermissionsOptions options) {
}
@Override
public PageImpl newPage() {
JsonObject params = new JsonObject();
JsonElement result = sendMessage("newPage", params);
return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("page").get("guid").getAsString());
}
@Override
public List<Page> pages() {
return null;
}
@Override
public void route(String url, BiConsumer<Route, Request> handler) {
}
@Override
public void setDefaultNavigationTimeout(int timeout) {
}
@Override
public void setDefaultTimeout(int timeout) {
}
@Override
public void setExtraHTTPHeaders(Map<String, String> headers) {
}
@Override
public void setGeolocation(Geolocation geolocation) {
}
@Override
public void setHTTPCredentials(HTTPCredentials httpCredentials) {
}
@Override
public void setOffline(boolean offline) {
}
@Override
public void unroute(String url, BiConsumer<Route, Request> handler) {
}
@Override
public Object waitForEvent(String event, String optionsOrPredicate) {
return null;
}
public Supplier<PageImpl> waitForPage() {
Supplier<JsonObject> pageSupplier = waitForEvent("page");
return () -> {
JsonObject params = pageSupplier.get();
String guid = params.getAsJsonObject("page").get("guid").getAsString();
return connection.getExistingObject(guid);
};
}
}
@@ -19,23 +19,50 @@ package com.microsoft.playwright.impl;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.Page;
public class Browser extends ChannelOwner {
Browser(ChannelOwner parent, String type, String guid, JsonObject initializer) {
import java.util.List;
class BrowserImpl extends ChannelOwner implements Browser {
BrowserImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
BrowserContext newContext() {
return newContext(new BrowserNewContextOptions());
@Override
public void close() {
sendMessage("close", new JsonObject());
}
BrowserContext newContext(BrowserNewContextOptions options) {
@Override
public List<BrowserContext> contexts() {
return null;
}
@Override
public boolean isConnected() {
return false;
}
public BrowserContextImpl newContext() {
return newContext(new NewContextOptions());
}
@Override
public BrowserContextImpl newContext(NewContextOptions options) {
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
JsonElement result = sendMessage("newContext", params);
return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("context").get("guid").getAsString());
}
void close() {
sendMessage("close", new JsonObject());
@Override
public Page newPage(NewPageOptions options) {
return null;
}
@Override
public String version() {
return initializer.get("version").getAsString();
}
}
@@ -19,47 +19,47 @@ package com.microsoft.playwright.impl;
import java.util.LinkedHashMap;
public class BrowserNewContextOptions {
Boolean noDefaultViewport;
public Boolean noDefaultViewport;
public static class Viewport {
// TODO: int is preferred here
int width;
int height;
public int width;
public int height;
}
Viewport viewport;
Boolean ignoreHTTPSErrors;
Boolean javaScriptEnabled;
Boolean bypassCSP;
String userAgent;
String locale;
String timezoneId;
public Viewport viewport;
public Boolean ignoreHTTPSErrors;
public Boolean javaScriptEnabled;
public Boolean bypassCSP;
public String userAgent;
public String locale;
public String timezoneId;
public static class Geolocation {
// TODO: can we use int somehow?
Double longitude;
Double latitude;
Double accuracy;
public Double longitude;
public Double latitude;
public Double accuracy;
};
Geolocation geolocation;
String[] permissions;
LinkedHashMap<String, String> extraHTTPHeaders;
Boolean offline;
public Geolocation geolocation;
public String[] permissions;
public LinkedHashMap<String, String> extraHTTPHeaders;
public Boolean offline;
public static class HttpCredentials {
String username;
String password;
public String username;
public String password;
}
HttpCredentials httpCredentials;
Integer deviceScaleFactor;
Boolean isMobile;
Boolean hasTouch;
enum ColorScheme {
public HttpCredentials httpCredentials;
public Integer deviceScaleFactor;
public Boolean isMobile;
public Boolean hasTouch;
public enum ColorScheme {
// TODO: noPreference => no-preference
dark, light, noPreference
}
ColorScheme colorScheme;
Boolean acceptDownloads;
Boolean _recordVideos;
public ColorScheme colorScheme;
public Boolean acceptDownloads;
public Boolean _recordVideos;
public static class _VideoSize {
int width;
int height;
public int width;
public int height;
}
_VideoSize _videoSize;
public _VideoSize _videoSize;
}
@@ -19,27 +19,47 @@ package com.microsoft.playwright.impl;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserServer;
import com.microsoft.playwright.BrowserType;
public class BrowserType extends ChannelOwner {
BrowserType(ChannelOwner parent, String type, String guid, JsonObject initializer) {
class BrowserTypeImpl extends ChannelOwner implements BrowserType {
BrowserTypeImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@Override
public Browser connect(ConnectOptions options) {
return null;
}
public BrowserImpl launch() {
return launch(new LaunchOptions());
}
@Override
public BrowserImpl launch(LaunchOptions options) {
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
JsonElement result = sendMessage("launch", params);
return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("browser").get("guid").getAsString());
}
public String executablePath() {
return initializer.get("executablePath").getAsString();
}
@Override
public BrowserContext launchPersistentContext(String userDataDir, LaunchPersistentContextOptions options) {
return null;
}
@Override
public BrowserServer launchServer(LaunchServerOptions options) {
return null;
}
public String name() {
return initializer.get("name").getAsString();
}
Browser launch() {
return launch(new BrowserTypeLaunchOptions());
}
Browser launch(BrowserTypeLaunchOptions options) {
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
JsonElement result = sendMessage("launch", params);
return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("browser").get("guid").getAsString());
}
}
@@ -21,27 +21,27 @@ import com.google.gson.JsonObject;
import java.util.LinkedHashMap;
public class BrowserTypeLaunchOptions {
String executablePath;
String[] args;
Boolean ignoreAllDefaultArgs;
String[] ignoreDefaultArgs;
Boolean handleSIGINT;
Boolean handleSIGTERM;
Boolean handleSIGHUP;
Integer timeout;
LinkedHashMap<String, String> env;
Boolean headless;
Boolean devtools;
public String executablePath;
public String[] args;
public Boolean ignoreAllDefaultArgs;
public String[] ignoreDefaultArgs;
public Boolean handleSIGINT;
public Boolean handleSIGTERM;
public Boolean handleSIGHUP;
public Integer timeout;
public LinkedHashMap<String, String> env;
public Boolean headless;
public Boolean devtools;
public static class Proxy {
String server;
String bypass;
String username;
String password;
}
Proxy proxy;
String downloadsPath;
String _videosPath;
JsonObject firefoxUserPrefs;
Boolean chromiumSandbox;
Integer slowMo;
public Proxy proxy;
public String downloadsPath;
public String _videosPath;
public JsonObject firefoxUserPrefs;
public Boolean chromiumSandbox;
public Integer slowMo;
}
@@ -174,43 +174,43 @@ public class Connection {
// initializer = this._replaceGuidsWithChannels(initializer);
switch (type) {
case "BrowserType":
result = new BrowserType(parent, type, guid, initializer);
result = new BrowserTypeImpl(parent, type, guid, initializer);
break;
case "Browser":
result = new Browser(parent, type, guid, initializer);
result = new BrowserImpl(parent, type, guid, initializer);
break;
case "BrowserContext":
result = new BrowserContext(parent, type, guid, initializer);
result = new BrowserContextImpl(parent, type, guid, initializer);
break;
case "ConsoleMessage":
result = new ConsoleMessage(parent, type, guid, initializer);
result = new ConsoleMessageImpl(parent, type, guid, initializer);
break;
case "Dialog":
result = new Dialog(parent, type, guid, initializer);
result = new DialogImpl(parent, type, guid, initializer);
break;
case "Download":
result = new Download(parent, type, guid, initializer);
result = new DownloadImpl(parent, type, guid, initializer);
break;
case "Electron":
// result = new Playwright(parent, type, guid, initializer);
break;
case "Frame":
result = new Frame(parent, type, guid, initializer);
result = new FrameImpl(parent, type, guid, initializer);
break;
case "JSHandle":
// result = new JSHandle(parent, type, guid, initializer);
break;
case "Page":
result = new Page(parent, type, guid, initializer);
result = new PageImpl(parent, type, guid, initializer);
break;
case "Playwright":
result = new Playwright(parent, type, guid, initializer);
result = new PlaywrightImpl(parent, type, guid, initializer);
break;
case "Request":
result = new Request(parent, type, guid, initializer);
result = new RequestImpl(parent, type, guid, initializer);
break;
case "Response":
result = new Response(parent, type, guid, initializer);
result = new ResponseImpl(parent, type, guid, initializer);
break;
case "Selectors":
// result = new Playwright(parent, type, guid, initializer);
@@ -19,8 +19,8 @@ package com.microsoft.playwright.impl;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class ConsoleMessage extends ChannelOwner {
public ConsoleMessage(ChannelOwner parent, String type, String guid, JsonObject initializer) {
public class ConsoleMessageImpl extends ChannelOwner {
public ConsoleMessageImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@@ -18,9 +18,9 @@ package com.microsoft.playwright.impl;
import com.google.gson.JsonObject;
public class Dialog extends ChannelOwner {
public class DialogImpl extends ChannelOwner {
private boolean handled;
Dialog(ChannelOwner parent, String type, String guid, JsonObject initializer) {
DialogImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@@ -19,8 +19,8 @@ package com.microsoft.playwright.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class Download extends ChannelOwner {
public Download(ChannelOwner parent, String type, String guid, JsonObject initializer) {
public class DownloadImpl extends ChannelOwner {
public DownloadImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@@ -19,36 +19,19 @@ package com.microsoft.playwright.impl;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import java.util.*;
import static com.microsoft.playwright.impl.Helpers.isFunctionBody;
public class Frame extends ChannelOwner {
Page page;
public class FrameImpl extends ChannelOwner implements Frame {
PageImpl page;
Frame(ChannelOwner parent, String type, String guid, JsonObject initializer) {
FrameImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
public Response navigate(String url) {
return navigate(url, new NavigateOptions());
}
public Response navigate(String url, NavigateOptions options) {
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
params.addProperty("url", url);
JsonElement result = sendMessage("goto", params);
System.out.println("result = " + new Gson().toJson(result));
return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("response").get("guid").getAsString());
}
public void click(String selector) {
JsonObject params = new JsonObject();
params.addProperty("selector", selector);
JsonElement result = sendMessage("click", params);
}
private static SerializedValue serializeValue(Object value) {
SerializedValue result = new SerializedValue();
if (value == null)
@@ -148,7 +131,7 @@ public class Frame extends ChannelOwner {
return deserialize(value);
}
public JsonElement evaluate(String expression, Object arg, boolean forceExpression) {
JsonElement evaluate(String expression, Object arg, boolean forceExpression) {
JsonObject params = new JsonObject();
params.addProperty("expression", expression);
params.addProperty("world", "main");
@@ -158,4 +141,218 @@ public class Frame extends ChannelOwner {
params.add("arg", new Gson().toJsonTree(serializeArgument(arg)));
return sendMessage("evaluateExpression", params);
}
@Override
public ElementHandle querySelector(String selector) {
return null;
}
@Override
public List<ElementHandle> querySelectorAll(String selector) {
return null;
}
@Override
public Object evalOnSelector(String selector, String pageFunction, Object arg) {
return null;
}
@Override
public Object evalOnSelectorAll(String selector, String pageFunction, Object arg) {
return null;
}
@Override
public ElementHandle addScriptTag(AddScriptTagOptions options) {
return null;
}
@Override
public ElementHandle addStyleTag(AddStyleTagOptions options) {
return null;
}
@Override
public void check(String selector, CheckOptions options) {
}
@Override
public List<Frame> childFrames() {
return null;
}
public void click(String selector) {
click(selector, new ClickOptions());
}
@Override
public void click(String selector, ClickOptions options) {
JsonObject params = new JsonObject();
params.addProperty("selector", selector);
JsonElement result = sendMessage("click", params);
}
@Override
public String content() {
return null;
}
@Override
public void dblclick(String selector, DblclickOptions options) {
}
@Override
public void dispatchEvent(String selector, String type, Object eventInit, DispatchEventOptions options) {
}
@Override
public Object evaluate(String pageFunction, Object arg) {
return evaluate(pageFunction, arg, false);
}
@Override
public JSHandle evaluateHandle(String pageFunction, Object arg) {
return null;
}
@Override
public void fill(String selector, String value, FillOptions options) {
}
@Override
public void focus(String selector, FocusOptions options) {
}
@Override
public ElementHandle frameElement() {
return null;
}
@Override
public String getAttribute(String selector, String name, GetAttributeOptions options) {
return null;
}
public ResponseImpl navigate(String url) {
return navigate(url, new GotoOptions());
}
@Override
public ResponseImpl navigate(String url, GotoOptions options) {
JsonObject params = new Gson().toJsonTree(options).getAsJsonObject();
params.addProperty("url", url);
JsonElement result = sendMessage("goto", params);
System.out.println("result = " + new Gson().toJson(result));
return connection.getExistingObject(result.getAsJsonObject().getAsJsonObject("response").get("guid").getAsString());
}
@Override
public void hover(String selector, HoverOptions options) {
}
@Override
public String innerHTML(String selector, InnerHTMLOptions options) {
return null;
}
@Override
public String innerText(String selector, InnerTextOptions options) {
return null;
}
@Override
public boolean isDetached() {
return false;
}
@Override
public String name() {
return null;
}
@Override
public Page page() {
return null;
}
@Override
public Frame parentFrame() {
return null;
}
@Override
public void press(String selector, String key, PressOptions options) {
}
@Override
public List<String> selectOption(String selector, String values, SelectOptionOptions options) {
return null;
}
@Override
public void setContent(String html, SetContentOptions options) {
}
@Override
public void setInputFiles(String selector, String files, SetInputFilesOptions options) {
}
@Override
public String textContent(String selector, TextContentOptions options) {
return null;
}
@Override
public String title() {
return null;
}
@Override
public void type(String selector, String text, TypeOptions options) {
}
@Override
public void uncheck(String selector, UncheckOptions options) {
}
@Override
public String url() {
return null;
}
@Override
public JSHandle waitForFunction(String pageFunction, Object arg, WaitForFunctionOptions options) {
return null;
}
@Override
public void waitForLoadState(LoadState state, WaitForLoadStateOptions options) {
}
@Override
public Response waitForNavigation(WaitForNavigationOptions options) {
return null;
}
@Override
public ElementHandle waitForSelector(String selector, WaitForSelectorOptions options) {
return null;
}
@Override
public void waitForTimeout(int timeout) {
}
}
@@ -1,102 +0,0 @@
/**
* 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.impl;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import java.io.*;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.function.Supplier;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
Playwright playwright = Playwright.create();
BrowserTypeLaunchOptions options = new BrowserTypeLaunchOptions();
options.headless = false;
// options.slowMo = 1000;
System.out.println("options = " + new Gson().toJson(options));
Browser browser = playwright.chromium.launch(options);
System.out.println("browser = " + browser);
BrowserNewContextOptions contextOptions = new BrowserNewContextOptions();
contextOptions.viewport = new BrowserNewContextOptions.Viewport();
contextOptions.viewport.width = 800;
contextOptions.viewport.height = 600;
BrowserContext context = browser.newContext(contextOptions);
Page page = context.newPage();
page.navigate("http://example.com");
// page.click("text=web browser engine");
Supplier<Page> popupSupplier = page.waitForPopup();
Supplier<Page> pageSupplier = context.waitForPage();
page.evaluate("window.open('http://example.com'); 13");
{
JsonElement r = page.evaluate("function foo(a) { return a + 1; }", 20);
System.out.println("r = " + new Gson().toJson(r));
}
{
List<Integer> r = page.evalTyped("function foo() { return [1,2,3]; }");
System.out.println("r = " + new Gson().toJson(r));
int p = r.get(0).intValue() + 1;
}
{
int r = page.evalTyped("function foo() { return 7; }");
System.out.println("int r = " + new Gson().toJson(r));
}
{
double r = page.evalTyped("function foo() { return 7.2; }");
System.out.println("double r = " + new Gson().toJson(r));
}
Page popup = popupSupplier.get();
System.out.println("popup = " + popup);
Page page2 = pageSupplier.get();
System.out.println(page2 == popup);
page.addDialogHandler(d -> {
System.out.println("Got dialog type: " + d.type());
System.out.println(" message = " + d.message());
d.accept("abc");
});
page.evaluate("alert('Hi there!')");
System.out.println("After alert");
page.addConsoleListener(m -> {
System.out.println("Got console message type: " + m.type());
System.out.println(" text = " + m.text());
System.out.println(" location = " + m.location());
});
page.evaluate("console.log('A message')");
browser.close();
// Disconnect and terminate the threads?
// playwright.close();
System.out.println("\nDONE.");
System.exit(0);
}
}
@@ -1,114 +0,0 @@
/**
* 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.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
public class Page extends ChannelOwner {
private final Frame mainFrame;
private final List<DialogHandler> dialogHandlers = new ArrayList<>();
private final List<ConsoleListener> consoleListeners = new ArrayList<>();
Page(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
mainFrame = connection.getExistingObject(initializer.getAsJsonObject("mainFrame").get("guid").getAsString());
mainFrame.page = this;
}
public Response navigate(String url) {
return navigate(url, new NavigateOptions());
}
public Response navigate(String url, NavigateOptions options) {
return mainFrame.navigate(url, options);
}
public void click(String selector) {
mainFrame.click(selector);
}
public Supplier<Page> waitForPopup() {
Supplier<JsonObject> popupSupplier = waitForEvent("popup");
return () -> {
JsonObject params = popupSupplier.get();
String guid = params.getAsJsonObject("page").get("guid").getAsString();
return connection.getExistingObject(guid);
};
}
public interface DialogHandler {
void handle(Dialog d);
}
public void addDialogHandler(DialogHandler handler) {
dialogHandlers.add(handler);
}
public void removeDialogHandler(DialogHandler handler) {
dialogHandlers.remove(handler);
}
protected void handleEvent(String event, JsonObject params) {
if ("dialog".equals(event)) {
String guid = params.getAsJsonObject("dialog").get("guid").getAsString();
Dialog dialog = connection.getExistingObject(guid);
for (DialogHandler handler: new ArrayList<>(dialogHandlers))
handler.handle(dialog);
// If no action taken dismiss dialog to not hang.
if (!dialog.isHandled())
dialog.dismiss();
} else if ("console".equals(event)) {
String guid = params.getAsJsonObject("message").get("guid").getAsString();
ConsoleMessage message = connection.getExistingObject(guid);
for (ConsoleListener listener: new ArrayList<>(consoleListeners))
listener.handle(message);
}
}
public interface ConsoleListener {
void handle(ConsoleMessage m);
}
public void addConsoleListener(ConsoleListener listener) {
consoleListeners.add(listener);
}
public void removeConsoleListener(ConsoleListener listener) {
consoleListeners.remove(listener);
}
public <T> T evalTyped(String expression) {
return mainFrame.evalTyped(expression);
}
public JsonElement evaluate(String expression) {
return evaluate(expression, null);
}
public JsonElement evaluate(String expression, Object arg) {
return evaluate(expression, arg, false);
}
public JsonElement evaluate(String expression, Object arg, boolean forceExpression) {
return mainFrame.evaluate(expression, arg, forceExpression);
}
}
@@ -0,0 +1,421 @@
/**
* 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.impl;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.microsoft.playwright.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
public class PageImpl extends ChannelOwner implements Page {
private final FrameImpl mainFrame;
private final List<DialogHandler> dialogHandlers = new ArrayList<>();
private final List<ConsoleListener> consoleListeners = new ArrayList<>();
PageImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
mainFrame = connection.getExistingObject(initializer.getAsJsonObject("mainFrame").get("guid").getAsString());
mainFrame.page = this;
}
public Supplier<PageImpl> waitForPopup() {
Supplier<JsonObject> popupSupplier = waitForEvent("popup");
return () -> {
JsonObject params = popupSupplier.get();
String guid = params.getAsJsonObject("page").get("guid").getAsString();
return connection.getExistingObject(guid);
};
}
public interface DialogHandler {
void handle(DialogImpl d);
}
public void addDialogHandler(DialogHandler handler) {
dialogHandlers.add(handler);
}
public void removeDialogHandler(DialogHandler handler) {
dialogHandlers.remove(handler);
}
protected void handleEvent(String event, JsonObject params) {
if ("dialog".equals(event)) {
String guid = params.getAsJsonObject("dialog").get("guid").getAsString();
DialogImpl dialog = connection.getExistingObject(guid);
for (DialogHandler handler: new ArrayList<>(dialogHandlers))
handler.handle(dialog);
// If no action taken dismiss dialog to not hang.
if (!dialog.isHandled())
dialog.dismiss();
} else if ("console".equals(event)) {
String guid = params.getAsJsonObject("message").get("guid").getAsString();
ConsoleMessageImpl message = connection.getExistingObject(guid);
for (ConsoleListener listener: new ArrayList<>(consoleListeners))
listener.handle(message);
}
}
public interface ConsoleListener {
void handle(ConsoleMessageImpl m);
}
public void addConsoleListener(ConsoleListener listener) {
consoleListeners.add(listener);
}
public void removeConsoleListener(ConsoleListener listener) {
consoleListeners.remove(listener);
}
public <T> T evalTyped(String expression) {
return mainFrame.evalTyped(expression);
}
public JsonElement evaluate(String expression) {
return evaluate(expression, null);
}
@Override
public void close(CloseOptions options) {
}
@Override
public ElementHandle querySelector(String selector) {
return null;
}
@Override
public List<ElementHandle> querySelectorAll(String selector) {
return null;
}
@Override
public Object evalOnSelector(String selector, String pageFunction, Object arg) {
return null;
}
@Override
public Object evalOnSelectorAll(String selector, String pageFunction, Object arg) {
return null;
}
@Override
public void addInitScript(String script, Object arg) {
}
@Override
public ElementHandle addScriptTag(AddScriptTagOptions options) {
return null;
}
@Override
public ElementHandle addStyleTag(AddStyleTagOptions options) {
return null;
}
@Override
public void bringToFront() {
}
@Override
public void check(String selector, CheckOptions options) {
}
public void click(String selector) {
mainFrame.click(selector);
}
@Override
public void click(String selector, ClickOptions options) {
}
@Override
public String content() {
return null;
}
@Override
public BrowserContext context() {
return null;
}
@Override
public void dblclick(String selector, DblclickOptions options) {
}
@Override
public void dispatchEvent(String selector, String type, Object eventInit, DispatchEventOptions options) {
}
@Override
public void emulateMedia(EmulateMediaOptions options) {
}
@Override
public JsonElement evaluate(String expression, Object arg) {
return evaluate(expression, arg, false);
}
@Override
public JSHandle evaluateHandle(String pageFunction, Object arg) {
return null;
}
@Override
public void exposeBinding(String name, String playwrightBinding) {
}
@Override
public void exposeFunction(String name, String playwrightFunction) {
}
@Override
public void fill(String selector, String value, FillOptions options) {
}
@Override
public void focus(String selector, FocusOptions options) {
}
@Override
public Frame frame(String options) {
return null;
}
@Override
public List<Frame> frames() {
return null;
}
@Override
public String getAttribute(String selector, String name, GetAttributeOptions options) {
return null;
}
@Override
public Response goBack(GoBackOptions options) {
return null;
}
@Override
public Response goForward(GoForwardOptions options) {
return null;
}
public ResponseImpl navigate(String url) {
return navigate(url, new GotoOptions());
}
@Override
public ResponseImpl navigate(String url, GotoOptions options) {
// TODO: convert params
return mainFrame.navigate(url, new Frame.GotoOptions());
}
@Override
public void hover(String selector, HoverOptions options) {
}
@Override
public String innerHTML(String selector, InnerHTMLOptions options) {
return null;
}
@Override
public String innerText(String selector, InnerTextOptions options) {
return null;
}
@Override
public boolean isClosed() {
return false;
}
@Override
public Frame mainFrame() {
return null;
}
@Override
public Page opener() {
return null;
}
@Override
public byte[] pdf(PdfOptions options) {
return new byte[0];
}
@Override
public void press(String selector, String key, PressOptions options) {
}
@Override
public Response reload(ReloadOptions options) {
return null;
}
@Override
public void route(String url, BiConsumer<Route, Request> handler) {
}
@Override
public byte[] screenshot(ScreenshotOptions options) {
return new byte[0];
}
@Override
public List<String> selectOption(String selector, String values, SelectOptionOptions options) {
return null;
}
@Override
public void setContent(String html, SetContentOptions options) {
}
@Override
public void setDefaultNavigationTimeout(int timeout) {
}
@Override
public void setDefaultTimeout(int timeout) {
}
@Override
public void setExtraHTTPHeaders(Map<String, String> headers) {
}
@Override
public void setInputFiles(String selector, String files, SetInputFilesOptions options) {
}
@Override
public void setViewportSize(ViewportSize viewportSize) {
}
@Override
public String textContent(String selector, TextContentOptions options) {
return null;
}
@Override
public String title() {
return null;
}
@Override
public void type(String selector, String text, TypeOptions options) {
}
@Override
public void uncheck(String selector, UncheckOptions options) {
}
@Override
public void unroute(String url, BiConsumer<Route, Request> handler) {
}
@Override
public String url() {
return null;
}
@Override
public PageViewportSize viewportSize() {
return null;
}
@Override
public Object waitForEvent(String event, String optionsOrPredicate) {
return null;
}
@Override
public JSHandle waitForFunction(String pageFunction, Object arg, WaitForFunctionOptions options) {
return null;
}
@Override
public void waitForLoadState(LoadState state, WaitForLoadStateOptions options) {
}
@Override
public Response waitForNavigation(WaitForNavigationOptions options) {
return null;
}
@Override
public Request waitForRequest(String urlOrPredicate, WaitForRequestOptions options) {
return null;
}
@Override
public Response waitForResponse(String urlOrPredicate, WaitForResponseOptions options) {
return null;
}
@Override
public ElementHandle waitForSelector(String selector, WaitForSelectorOptions options) {
return null;
}
@Override
public void waitForTimeout(int timeout) {
}
@Override
public List<Worker> workers() {
return null;
}
public JsonElement evaluate(String expression, Object arg, boolean forceExpression) {
return mainFrame.evaluate(expression, arg, forceExpression);
}
}
@@ -1,49 +0,0 @@
/**
* 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.impl;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
public class Playwright extends ChannelOwner {
static Playwright create() throws IOException {
File cwd = FileSystems.getDefault().getPath(".").toFile();
File driver = new File(cwd, "../driver/main.js");
System.out.println("driver = " + driver.getCanonicalPath());
ProcessBuilder pb = new ProcessBuilder("node", driver.getCanonicalPath());
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
// pb.environment().put("DEBUG", "pw:pro*");
Process p = pb.start();
Connection connection = new Connection(p.getInputStream(), p.getOutputStream());
Playwright playwright = (Playwright)connection.waitForObjectWithKnownName("Playwright");
return playwright;
}
public final BrowserType chromium;
public final BrowserType firefox;
public final BrowserType webkit;
public Playwright(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
chromium = parent.connection.getExistingObject(initializer.getAsJsonObject("chromium").get("guid").getAsString());
firefox = parent.connection.getExistingObject(initializer.getAsJsonObject("firefox").get("guid").getAsString());
webkit = parent.connection.getExistingObject(initializer.getAsJsonObject("webkit").get("guid").getAsString());
}
}
@@ -0,0 +1,70 @@
/**
* 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.impl;
import com.google.gson.JsonObject;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Playwright;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
public class PlaywrightImpl extends ChannelOwner implements Playwright {
public static PlaywrightImpl create() {
try {
File cwd = FileSystems.getDefault().getPath(".").toFile();
File driver = new File(cwd, "../driver/main.js");
System.out.println("driver = " + driver.getCanonicalPath());
ProcessBuilder pb = new ProcessBuilder("node", driver.getCanonicalPath());
pb.redirectError(ProcessBuilder.Redirect.INHERIT);
// pb.environment().put("DEBUG", "pw:pro*");
Process p = pb.start();
Connection connection = new Connection(p.getInputStream(), p.getOutputStream());
PlaywrightImpl playwright = (PlaywrightImpl)connection.waitForObjectWithKnownName("Playwright");
return playwright;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private final BrowserTypeImpl chromium;
private final BrowserTypeImpl firefox;
private final BrowserTypeImpl webkit;
public PlaywrightImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
chromium = parent.connection.getExistingObject(initializer.getAsJsonObject("chromium").get("guid").getAsString());
firefox = parent.connection.getExistingObject(initializer.getAsJsonObject("firefox").get("guid").getAsString());
webkit = parent.connection.getExistingObject(initializer.getAsJsonObject("webkit").get("guid").getAsString());
}
@Override
public BrowserTypeImpl chromium() {
return chromium;
}
@Override
public BrowserTypeImpl firefox() {
return firefox;
}
@Override
public BrowserTypeImpl webkit() {
return webkit;
}
}
@@ -18,8 +18,8 @@ package com.microsoft.playwright.impl;
import com.google.gson.JsonObject;
public class Response extends ChannelOwner {
Response(ChannelOwner parent, String type, String guid, JsonObject initializer) {
public class RequestImpl extends ChannelOwner {
RequestImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
}
@@ -0,0 +1,85 @@
/**
* 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.impl;
import com.google.gson.JsonObject;
import com.microsoft.playwright.Frame;
import com.microsoft.playwright.Request;
import com.microsoft.playwright.Response;
import java.util.Map;
public class ResponseImpl extends ChannelOwner implements Response {
ResponseImpl(ChannelOwner parent, String type, String guid, JsonObject initializer) {
super(parent, type, guid, initializer);
}
@Override
public byte[] body() {
return new byte[0];
}
@Override
public Error finished() {
return null;
}
@Override
public Frame frame() {
return null;
}
@Override
public Map<String, String> headers() {
return null;
}
@Override
public Object json() {
return null;
}
@Override
public boolean ok() {
return false;
}
@Override
public Request request() {
return null;
}
@Override
public int status() {
return 0;
}
@Override
public String statusText() {
return null;
}
@Override
public String text() {
return null;
}
@Override
public String url() {
return null;
}
}
@@ -0,0 +1,57 @@
/**
* Copyright (c) Microsoft Corporation.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TestClick {
private static Playwright playwright;
private Browser browser;
private boolean isChromium;
@org.junit.jupiter.api.BeforeAll
static void beforeAll() {
playwright = Playwright.create();
}
@org.junit.jupiter.api.BeforeEach
void setUp() {
BrowserType.LaunchOptions options = new BrowserType.LaunchOptions();
browser = playwright.chromium().launch(options);
isChromium = true;
}
@org.junit.jupiter.api.AfterEach
void tearDown() {
browser.close();
}
@Test
void version_should_work() {
System.out.println("browser = " + browser);
System.out.println("version = " + browser.version());
if (isChromium)
assertTrue(Pattern.matches("^\\d+\\.\\d+\\.\\d+\\.\\d+$", browser.version()));
else
assertTrue(Pattern.matches("^\\d+\\.\\d+", browser.version()));
}
}