mirror of
https://github.com/microsoft/playwright-java.git
synced 2025-09-08 21:01:00 +00:00
docs: update docs, add basic example (#35)
This commit is contained in:
parent
a02e70ad84
commit
7f8b2a9a10
@ -11,7 +11,7 @@ git clone https://github.com/microsoft/playwright-java
|
|||||||
cd 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
|
```bash
|
||||||
mkdir driver
|
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 .
|
unzip playwright-cli-0.151.0-linux.zip -d .
|
||||||
./playwright-cli install
|
./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
|
### 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
|
### Code Style
|
||||||
|
|
||||||
|
32
README.md
32
README.md
@ -1 +1,33 @@
|
|||||||
# 🎭 [Playwright](https://github.com/microsoft/playwright) for Java
|
# 🎭 [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.
|
||||||
|
@ -15,91 +15,18 @@
|
|||||||
*/
|
*/
|
||||||
package com.microsoft.playwright.example;
|
package com.microsoft.playwright.example;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonElement;
|
|
||||||
import com.microsoft.playwright.*;
|
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 class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
|
|
||||||
Playwright playwright = Playwright.create();
|
Playwright playwright = Playwright.create();
|
||||||
BrowserType.LaunchOptions options = new BrowserType.LaunchOptions()
|
Browser browser = playwright.chromium().launch(
|
||||||
// .withSlowMo(1000)
|
new BrowserType.LaunchOptions().withHeadless(false).withSlowMo(1000));
|
||||||
.withHeadless(false);
|
BrowserContext context = browser.newContext(
|
||||||
|
new Browser.NewContextOptions().withViewport(800, 600));
|
||||||
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);
|
|
||||||
Page page = context.newPage();
|
Page page = context.newPage();
|
||||||
// page.navigate("http://example.com", null);
|
|
||||||
page.navigate("https://webkit.org", null);
|
page.navigate("https://webkit.org", null);
|
||||||
page.click("text=web browser engine", new Page.ClickOptions());
|
page.click("text=check feature status");
|
||||||
|
|
||||||
// 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();
|
browser.close();
|
||||||
|
|
||||||
// Disconnect and terminate the threads?
|
|
||||||
// playwright.close();
|
|
||||||
System.out.println("\nDONE.");
|
|
||||||
System.exit(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user