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);
diff --git a/lib/src/main/java/com/microsoft/playwright/impl/Request.java b/lib/src/main/java/com/microsoft/playwright/Playwright.java
similarity index 66%
rename from lib/src/main/java/com/microsoft/playwright/impl/Request.java
rename to lib/src/main/java/com/microsoft/playwright/Playwright.java
index 147cfa9f..af63c94b 100644
--- a/lib/src/main/java/com/microsoft/playwright/impl/Request.java
+++ b/lib/src/main/java/com/microsoft/playwright/Playwright.java
@@ -1,12 +1,12 @@
/**
* 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.
@@ -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();
}
diff --git a/lib/src/main/java/com/microsoft/playwright/Request.java b/lib/src/main/java/com/microsoft/playwright/Request.java
index 12345d8b..e581d8fe 100644
--- a/lib/src/main/java/com/microsoft/playwright/Request.java
+++ b/lib/src/main/java/com/microsoft/playwright/Request.java
@@ -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 headers();
boolean isNavigationRequest();
String method();
String postData();
byte[] postDataBuffer();
- Object postDataJSON();
+ RequestPostDataJSON postDataJSON();
Request redirectedFrom();
Request redirectedTo();
String resourceType();
diff --git a/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java b/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java
index 20f26af6..28703b05 100644
--- a/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java
+++ b/lib/src/main/java/com/microsoft/playwright/WebKitBrowser.java
@@ -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;
diff --git a/lib/src/main/java/com/microsoft/playwright/example/Main.java b/lib/src/main/java/com/microsoft/playwright/example/Main.java
new file mode 100644
index 00000000..c1a11844
--- /dev/null
+++ b/lib/src/main/java/com/microsoft/playwright/example/Main.java
@@ -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 popupSupplier = page.waitForPopup();
+// Supplier 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 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);
+ }
+}
diff --git a/lib/src/main/java/com/microsoft/playwright/impl/BrowserContext.java b/lib/src/main/java/com/microsoft/playwright/impl/BrowserContext.java
deleted file mode 100644
index 41921a4d..00000000
--- a/lib/src/main/java/com/microsoft/playwright/impl/BrowserContext.java
+++ /dev/null
@@ -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 waitForPage() {
- Supplier pageSupplier = waitForEvent("page");
- return () -> {
- JsonObject params = pageSupplier.get();
- String guid = params.getAsJsonObject("page").get("guid").getAsString();
- return connection.getExistingObject(guid);
- };
- }
-
-}
diff --git a/lib/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java b/lib/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java
new file mode 100644
index 00000000..dce19b33
--- /dev/null
+++ b/lib/src/main/java/com/microsoft/playwright/impl/BrowserContextImpl.java
@@ -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