From 7f8b2a9a103f3406ca9face5ca3a30f005e38e72 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Thu, 22 Oct 2020 17:45:14 -0700 Subject: [PATCH] docs: update docs, add basic example (#35) --- CONTRIBUTING.md | 12 ++- README.md | 32 +++++++ .../microsoft/playwright/example/Main.java | 85 ++----------------- 3 files changed, 48 insertions(+), 81 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c53257d0..4fb8d41f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,7 +11,7 @@ git clone https://github.com/microsoft/playwright-java cd playwright-java ``` -2. Checkout and install playwright-cli node.js package into `driver/` directory +2. Checkout and install playwright-cli node.js package into `driver/` directory. This installs Playwright and browser binaries for Chromium, Firefox and WebKit. ```bash mkdir driver @@ -20,10 +20,18 @@ curl -O https://playwright.azureedge.net/builds/cli/playwright-cli-0.151.0-linux unzip playwright-cli-0.151.0-linux.zip -d . ./playwright-cli install ``` +Replace `-linux.zip` in the file name with `-win32_x64.zip` or `-mac.zip` for other platforms. + +### Compiling and running the tests with Maven + +```bash +mvn compile +mvn test +``` ### Generating API -Public Java API is generated from [api.json](https://github.com/microsoft/playwright-java/blob/master/api-generator/src/main/resources/api.json) which in turn is created by [`npm run generate-api-json`](https://github.com/microsoft/playwright/blob/2df6425254232125e46347d5e8cddd71c3cecce6/package.json#L29). +Public Java API is generated from [api.json](https://github.com/microsoft/playwright-java/blob/master/api-generator/src/main/resources/api.json) which in turn is created by `playwright-cli print-api-json`. ### Code Style diff --git a/README.md b/README.md index 587bd7b6..4ee8ba17 100644 --- a/README.md +++ b/README.md @@ -1 +1,33 @@ # 🎭 [Playwright](https://github.com/microsoft/playwright) for Java + +### _The project is in early developement phase, some of the APIs are not implemented yet, others may change._ + + +## Usage + +Follow [the instructions](https://github.com/microsoft/playwright-java/blob/master/CONTRIBUTING.md#getting-code) to build the project from source and install driver. + + +Simple example: + +```java +package com.microsoft.playwright.example; + +import com.microsoft.playwright.*; + +public class Main { + public static void main(String[] args) { + Playwright playwright = Playwright.create(); + Browser browser = playwright.chromium().launch( + new BrowserType.LaunchOptions().withHeadless(false)); + BrowserContext context = browser.newContext( + new Browser.NewContextOptions().withViewport(800, 600)); + Page page = context.newPage(); + page.navigate("https://webkit.org", null); + page.click("text=check feature status"); + browser.close(); + } +} +``` + +Original Playwright [documentation](https://playwright.dev/). We will convert it to Javadoc eventually. diff --git a/playwright/src/main/java/com/microsoft/playwright/example/Main.java b/playwright/src/main/java/com/microsoft/playwright/example/Main.java index 69658c27..a81e514e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/example/Main.java +++ b/playwright/src/main/java/com/microsoft/playwright/example/Main.java @@ -15,91 +15,18 @@ */ 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 { + public static void main(String[] args) { 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.withViewport(800, 600); - BrowserContext context = browser.newContext(contextOptions); + Browser browser = playwright.chromium().launch( + new BrowserType.LaunchOptions().withHeadless(false).withSlowMo(1000)); + BrowserContext context = browser.newContext( + new Browser.NewContextOptions().withViewport(800, 600)); 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); + page.click("text=check feature status"); browser.close(); - - // Disconnect and terminate the threads? - // playwright.close(); - System.out.println("\nDONE."); - System.exit(0); } }