Skip to content

Commit

Permalink
docs: java snippets for api classes (microsoft#5629) (microsoft#5657)
Browse files Browse the repository at this point in the history
  • Loading branch information
yury-s authored Mar 1, 2021
1 parent 92bbdbe commit 948e658
Show file tree
Hide file tree
Showing 22 changed files with 1,213 additions and 40 deletions.
12 changes: 11 additions & 1 deletion docs/src/api/class-accessibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ const snapshot = await page.accessibility.snapshot();
console.log(snapshot);
```

```java
String snapshot = page.accessibility().snapshot();
System.out.println(snapshot);
```

```python async
snapshot = await page.accessibility.snapshot()
print(snapshot)
Expand Down Expand Up @@ -86,6 +91,11 @@ function findFocusedNode(node) {
}
```

```java
// FIXME
String snapshot = page.accessibility().snapshot();
```

```python async
def find_focused_node(node):
if (node.get("focused"))
Expand Down Expand Up @@ -128,4 +138,4 @@ Prune uninteresting nodes from the tree. Defaults to `true`.
### option: Accessibility.snapshot.root
- `root` <[ElementHandle]>

The root DOM element for the snapshot. Defaults to the whole page.
The root DOM element for the snapshot. Defaults to the whole page.
32 changes: 32 additions & 0 deletions docs/src/api/class-browser.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'.
})();
```

```java
import com.microsoft.playwright.*;

public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType firefox = playwright.firefox()
Browser browser = firefox.launch();
Page page = browser.newPage();
page.navigate('https://example.com');
browser.close();
}
}
}
```

```python async
import asyncio
from playwright.async_api import async_playwright
Expand Down Expand Up @@ -75,6 +91,13 @@ const context = await browser.newContext();
console.log(browser.contexts().length); // prints `1`
```

```java
Browser browser = pw.webkit().launch();
System.out.println(browser.contexts().size()); // prints "0"
BrowserContext context = browser.newContext();
System.out.println(browser.contexts().size()); // prints "1"
```

```python async
browser = await pw.webkit.launch()
print(len(browser.contexts())) # prints `0`
Expand Down Expand Up @@ -110,6 +133,15 @@ Creates a new browser context. It won't share cookies/cache with other browser c
})();
```

```java
Browser browser = playwright.firefox().launch(); // Or 'chromium' or 'webkit'.
// Create a new incognito browser context.
BrowserContext context = browser.newContext();
// Create a new page in a pristine context.
Page page = context.newPage();
page.navigate('https://example.com');
```

```python async
browser = await playwright.firefox.launch() # or "chromium" or "webkit".
# create a new incognito browser context.
Expand Down
137 changes: 135 additions & 2 deletions docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ await page.goto('https://example.com');
await context.close();
```

```java
// Create a new incognito browser context
BrowserContext context = browser.newContext();
// Create a new page inside context.
Page page = context.newPage();
page.navigate("https://example.com");
// Dispose context once it"s no longer needed.
context.close();
```

```python async
# create a new incognito browser context
context = await browser.new_context()
Expand Down Expand Up @@ -58,11 +68,18 @@ popup with `window.open('http://example.com')`, this event will fire when the ne
done and its response has started loading in the popup.

```js
const [page] = await Promise.all([
const [newPage] = await Promise.all([
context.waitForEvent('page'),
page.click('a[target=_blank]'),
]);
console.log(await page.evaluate('location.href'));
console.log(await newPage.evaluate('location.href'));
```

```java
Page newPage = context.waitForPage(() -> {
page.click("a[target=_blank]");
});
System.out.println(newPage.evaluate("location.href"));
```

```python async
Expand Down Expand Up @@ -93,6 +110,10 @@ obtained via [`method: BrowserContext.cookies`].
await browserContext.addCookies([cookieObject1, cookieObject2]);
```

```java
browserContext.addCookies(Arrays.asList(cookieObject1, cookieObject2));
```

```python async
await browser_context.add_cookies([cookie_object1, cookie_object2])
```
Expand Down Expand Up @@ -137,6 +158,11 @@ await browserContext.addInitScript({
});
```

```java
// In your playwright script, assuming the preload.js file is in same directory.
browserContext.addInitScript(Paths.get("preload.js"));
```

```python async
# in your playwright script, assuming the preload.js file is in same directory.
await browser_context.add_init_script(path="preload.js")
Expand Down Expand Up @@ -193,6 +219,13 @@ await context.grantPermissions(['clipboard-read']);
context.clearPermissions();
```

```java
BrowserContext context = browser.newContext();
context.grantPermissions(Arrays.asList("clipboard-read"));
// do stuff ..
context.clearPermissions();
```

```python async
context = await browser.new_context()
await context.grant_permissions(["clipboard-read"])
Expand Down Expand Up @@ -268,6 +301,30 @@ const { webkit } = require('playwright'); // Or 'chromium' or 'firefox'.
})();
```

```java
import com.microsoft.playwright.*;

public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType webkit = playwright.webkit()
Browser browser = webkit.launch(new BrowserType.LaunchOptions().withHeadless(false));
BrowserContext context = browser.newContext();
context.exposeBinding("pageURL", (source, args) -> source.page().url());
Page page = context.newPage();
page.setContent("<script>\n" +
" async function onClick() {\n" +
" document.querySelector('div').textContent = await window.pageURL();\n" +
" }\n" +
"</script>\n" +
"<button onclick=\"onClick()\">Click me</button>\n" +
"<div></div>");
page.click("button");
}
}
}
```

```python async
import asyncio
from playwright.async_api import async_playwright
Expand Down Expand Up @@ -334,6 +391,20 @@ await page.setContent(`
`);
```

```java
context.exposeBinding("clicked", (source, args) -> {
ElementHandle element = (ElementHandle) args[0];
System.out.println(element.textContent());
return null;
}, new BrowserContext.ExposeBindingOptions().withHandle(true));
page.setContent("" +
"<script>\n" +
" document.addEventListener('click', event => window.clicked(event.target));\n" +
"</script>\n" +
"<div>Click me</div>\n" +
"<div>Or click me</div>\n");
```

```python async
async def print(source, element):
print(await element.text_content())
Expand Down Expand Up @@ -412,6 +483,44 @@ const crypto = require('crypto');
})();
```

```java
import com.microsoft.playwright.*;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType webkit = playwright.webkit()
Browser browser = webkit.launch(new BrowserType.LaunchOptions().withHeadless(false));
context.exposeFunction("sha1", args -> {
String text = (String) args[0];
MessageDigest crypto;
try {
crypto = MessageDigest.getInstance("SHA-1");
} catch (NoSuchAlgorithmException e) {
return null;
}
byte[] token = crypto.digest(text.getBytes(StandardCharsets.UTF_8));
return Base64.getEncoder().encodeToString(token);
});
Page page = context.newPage();
page.setContent("<script>\n" +
" async function onClick() {\n" +
" document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');\n" +
" }\n" +
"</script>\n" +
"<button onclick=\"onClick()\">Click me</button>\n" +
"<div></div>\n");
page.click("button");
}
}
}
```

```python async
import asyncio
import hashlib
Expand Down Expand Up @@ -544,6 +653,14 @@ await page.goto('https://example.com');
await browser.close();
```

```java
BrowserContext context = browser.newContext();
context.route("**/*.{png,jpg,jpeg}", route -> route.abort());
Page page = context.newPage();
page.navigate("https://example.com");
browser.close();
```

```python async
context = await browser.new_context()
page = await context.new_page()
Expand All @@ -570,6 +687,14 @@ await page.goto('https://example.com');
await browser.close();
```

```java
BrowserContext context = browser.newContext();
context.route(Pattern.compile("(\\.png$)|(\\.jpg$)"), route -> route.abort());
Page page = context.newPage();
page.navigate("https://example.com");
browser.close();
```

```python async
context = await browser.new_context()
page = await context.new_page()
Expand Down Expand Up @@ -670,6 +795,10 @@ Sets the context's geolocation. Passing `null` or `undefined` emulates position
await browserContext.setGeolocation({latitude: 59.95, longitude: 30.31667});
```

```java
browserContext.setGeolocation(new Geolocation(59.95, 30.31667));
```

```python async
await browser_context.set_geolocation({"latitude": 59.95, "longitude": 30.31667})
```
Expand Down Expand Up @@ -774,6 +903,10 @@ const [page, _] = await Promise.all([
]);
```

```java
Page newPage = context.waitForPage(() -> page.click("button"));
```

```python async
async with context.expect_event("page") as event_info:
await page.click("button")
Expand Down
23 changes: 23 additions & 0 deletions docs/src/api/class-browsertype.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
})();
```

```java
import com.microsoft.playwright.*;

public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
Browser browser = chromium.launch();
Page page = browser.newPage();
page.navigate("https://example.com");
// other actions...
browser.close();
}
}
}
```

```python async
import asyncio
from playwright.async_api import async_playwright
Expand Down Expand Up @@ -102,6 +119,12 @@ const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
});
```

```java
// Or "firefox" or "webkit".
Browser browser = chromium.launch(new BrowserType.LaunchOptions()
.withIgnoreDefaultArgs(Arrays.asList("--mute-audio")));
```

```python async
browser = await playwright.chromium.launch( # or "firefox" or "webkit".
ignore_default_args=["--mute-audio"]
Expand Down
24 changes: 22 additions & 2 deletions docs/src/api/class-dialog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,32 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
page.on('dialog', async dialog => {
console.log(dialog.message());
await dialog.dismiss();
await browser.close();
});
page.evaluate(() => alert('1'));
await page.evaluate(() => alert('1'));
await browser.close();
})();
```

```java
import com.microsoft.playwright.*;

public class Example {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium();
Browser browser = chromium.launch();
Page page = browser.newPage();
page.onDialog(dialog -> {
System.out.println(dialog.message());
dialog.dismiss();
});
page.evaluate("alert('1')");
browser.close();
}
}
}
```

```python async
import asyncio
from playwright.async_api import async_playwright
Expand Down
Loading

0 comments on commit 948e658

Please sign in to comment.