Skip to content

Commit 948e658

Browse files
authored
docs: java snippets for api classes (#5629) (#5657)
1 parent 92bbdbe commit 948e658

22 files changed

+1213
-40
lines changed

docs/src/api/class-accessibility.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ const snapshot = await page.accessibility.snapshot();
5858
console.log(snapshot);
5959
```
6060

61+
```java
62+
String snapshot = page.accessibility().snapshot();
63+
System.out.println(snapshot);
64+
```
65+
6166
```python async
6267
snapshot = await page.accessibility.snapshot()
6368
print(snapshot)
@@ -86,6 +91,11 @@ function findFocusedNode(node) {
8691
}
8792
```
8893

94+
```java
95+
// FIXME
96+
String snapshot = page.accessibility().snapshot();
97+
```
98+
8999
```python async
90100
def find_focused_node(node):
91101
if (node.get("focused"))
@@ -128,4 +138,4 @@ Prune uninteresting nodes from the tree. Defaults to `true`.
128138
### option: Accessibility.snapshot.root
129139
- `root` <[ElementHandle]>
130140

131-
The root DOM element for the snapshot. Defaults to the whole page.
141+
The root DOM element for the snapshot. Defaults to the whole page.

docs/src/api/class-browser.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ const { firefox } = require('playwright'); // Or 'chromium' or 'webkit'.
1414
})();
1515
```
1616

17+
```java
18+
import com.microsoft.playwright.*;
19+
20+
public class Example {
21+
public static void main(String[] args) {
22+
try (Playwright playwright = Playwright.create()) {
23+
BrowserType firefox = playwright.firefox()
24+
Browser browser = firefox.launch();
25+
Page page = browser.newPage();
26+
page.navigate('https://example.com');
27+
browser.close();
28+
}
29+
}
30+
}
31+
```
32+
1733
```python async
1834
import asyncio
1935
from playwright.async_api import async_playwright
@@ -75,6 +91,13 @@ const context = await browser.newContext();
7591
console.log(browser.contexts().length); // prints `1`
7692
```
7793

94+
```java
95+
Browser browser = pw.webkit().launch();
96+
System.out.println(browser.contexts().size()); // prints "0"
97+
BrowserContext context = browser.newContext();
98+
System.out.println(browser.contexts().size()); // prints "1"
99+
```
100+
78101
```python async
79102
browser = await pw.webkit.launch()
80103
print(len(browser.contexts())) # prints `0`
@@ -110,6 +133,15 @@ Creates a new browser context. It won't share cookies/cache with other browser c
110133
})();
111134
```
112135

136+
```java
137+
Browser browser = playwright.firefox().launch(); // Or 'chromium' or 'webkit'.
138+
// Create a new incognito browser context.
139+
BrowserContext context = browser.newContext();
140+
// Create a new page in a pristine context.
141+
Page page = context.newPage();
142+
page.navigate('https://example.com');
143+
```
144+
113145
```python async
114146
browser = await playwright.firefox.launch() # or "chromium" or "webkit".
115147
# create a new incognito browser context.

docs/src/api/class-browsercontext.md

Lines changed: 135 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ await page.goto('https://example.com');
1919
await context.close();
2020
```
2121

22+
```java
23+
// Create a new incognito browser context
24+
BrowserContext context = browser.newContext();
25+
// Create a new page inside context.
26+
Page page = context.newPage();
27+
page.navigate("https://example.com");
28+
// Dispose context once it"s no longer needed.
29+
context.close();
30+
```
31+
2232
```python async
2333
# create a new incognito browser context
2434
context = await browser.new_context()
@@ -58,11 +68,18 @@ popup with `window.open('http://example.com')`, this event will fire when the ne
5868
done and its response has started loading in the popup.
5969

6070
```js
61-
const [page] = await Promise.all([
71+
const [newPage] = await Promise.all([
6272
context.waitForEvent('page'),
6373
page.click('a[target=_blank]'),
6474
]);
65-
console.log(await page.evaluate('location.href'));
75+
console.log(await newPage.evaluate('location.href'));
76+
```
77+
78+
```java
79+
Page newPage = context.waitForPage(() -> {
80+
page.click("a[target=_blank]");
81+
});
82+
System.out.println(newPage.evaluate("location.href"));
6683
```
6784

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

113+
```java
114+
browserContext.addCookies(Arrays.asList(cookieObject1, cookieObject2));
115+
```
116+
96117
```python async
97118
await browser_context.add_cookies([cookie_object1, cookie_object2])
98119
```
@@ -137,6 +158,11 @@ await browserContext.addInitScript({
137158
});
138159
```
139160

161+
```java
162+
// In your playwright script, assuming the preload.js file is in same directory.
163+
browserContext.addInitScript(Paths.get("preload.js"));
164+
```
165+
140166
```python async
141167
# in your playwright script, assuming the preload.js file is in same directory.
142168
await browser_context.add_init_script(path="preload.js")
@@ -193,6 +219,13 @@ await context.grantPermissions(['clipboard-read']);
193219
context.clearPermissions();
194220
```
195221

222+
```java
223+
BrowserContext context = browser.newContext();
224+
context.grantPermissions(Arrays.asList("clipboard-read"));
225+
// do stuff ..
226+
context.clearPermissions();
227+
```
228+
196229
```python async
197230
context = await browser.new_context()
198231
await context.grant_permissions(["clipboard-read"])
@@ -268,6 +301,30 @@ const { webkit } = require('playwright'); // Or 'chromium' or 'firefox'.
268301
})();
269302
```
270303

304+
```java
305+
import com.microsoft.playwright.*;
306+
307+
public class Example {
308+
public static void main(String[] args) {
309+
try (Playwright playwright = Playwright.create()) {
310+
BrowserType webkit = playwright.webkit()
311+
Browser browser = webkit.launch(new BrowserType.LaunchOptions().withHeadless(false));
312+
BrowserContext context = browser.newContext();
313+
context.exposeBinding("pageURL", (source, args) -> source.page().url());
314+
Page page = context.newPage();
315+
page.setContent("<script>\n" +
316+
" async function onClick() {\n" +
317+
" document.querySelector('div').textContent = await window.pageURL();\n" +
318+
" }\n" +
319+
"</script>\n" +
320+
"<button onclick=\"onClick()\">Click me</button>\n" +
321+
"<div></div>");
322+
page.click("button");
323+
}
324+
}
325+
}
326+
```
327+
271328
```python async
272329
import asyncio
273330
from playwright.async_api import async_playwright
@@ -334,6 +391,20 @@ await page.setContent(`
334391
`);
335392
```
336393

394+
```java
395+
context.exposeBinding("clicked", (source, args) -> {
396+
ElementHandle element = (ElementHandle) args[0];
397+
System.out.println(element.textContent());
398+
return null;
399+
}, new BrowserContext.ExposeBindingOptions().withHandle(true));
400+
page.setContent("" +
401+
"<script>\n" +
402+
" document.addEventListener('click', event => window.clicked(event.target));\n" +
403+
"</script>\n" +
404+
"<div>Click me</div>\n" +
405+
"<div>Or click me</div>\n");
406+
```
407+
337408
```python async
338409
async def print(source, element):
339410
print(await element.text_content())
@@ -412,6 +483,44 @@ const crypto = require('crypto');
412483
})();
413484
```
414485

486+
```java
487+
import com.microsoft.playwright.*;
488+
489+
import java.nio.charset.StandardCharsets;
490+
import java.security.MessageDigest;
491+
import java.security.NoSuchAlgorithmException;
492+
import java.util.Base64;
493+
494+
public class Example {
495+
public static void main(String[] args) {
496+
try (Playwright playwright = Playwright.create()) {
497+
BrowserType webkit = playwright.webkit()
498+
Browser browser = webkit.launch(new BrowserType.LaunchOptions().withHeadless(false));
499+
context.exposeFunction("sha1", args -> {
500+
String text = (String) args[0];
501+
MessageDigest crypto;
502+
try {
503+
crypto = MessageDigest.getInstance("SHA-1");
504+
} catch (NoSuchAlgorithmException e) {
505+
return null;
506+
}
507+
byte[] token = crypto.digest(text.getBytes(StandardCharsets.UTF_8));
508+
return Base64.getEncoder().encodeToString(token);
509+
});
510+
Page page = context.newPage();
511+
page.setContent("<script>\n" +
512+
" async function onClick() {\n" +
513+
" document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');\n" +
514+
" }\n" +
515+
"</script>\n" +
516+
"<button onclick=\"onClick()\">Click me</button>\n" +
517+
"<div></div>\n");
518+
page.click("button");
519+
}
520+
}
521+
}
522+
```
523+
415524
```python async
416525
import asyncio
417526
import hashlib
@@ -544,6 +653,14 @@ await page.goto('https://example.com');
544653
await browser.close();
545654
```
546655

656+
```java
657+
BrowserContext context = browser.newContext();
658+
context.route("**/*.{png,jpg,jpeg}", route -> route.abort());
659+
Page page = context.newPage();
660+
page.navigate("https://example.com");
661+
browser.close();
662+
```
663+
547664
```python async
548665
context = await browser.new_context()
549666
page = await context.new_page()
@@ -570,6 +687,14 @@ await page.goto('https://example.com');
570687
await browser.close();
571688
```
572689

690+
```java
691+
BrowserContext context = browser.newContext();
692+
context.route(Pattern.compile("(\\.png$)|(\\.jpg$)"), route -> route.abort());
693+
Page page = context.newPage();
694+
page.navigate("https://example.com");
695+
browser.close();
696+
```
697+
573698
```python async
574699
context = await browser.new_context()
575700
page = await context.new_page()
@@ -670,6 +795,10 @@ Sets the context's geolocation. Passing `null` or `undefined` emulates position
670795
await browserContext.setGeolocation({latitude: 59.95, longitude: 30.31667});
671796
```
672797

798+
```java
799+
browserContext.setGeolocation(new Geolocation(59.95, 30.31667));
800+
```
801+
673802
```python async
674803
await browser_context.set_geolocation({"latitude": 59.95, "longitude": 30.31667})
675804
```
@@ -774,6 +903,10 @@ const [page, _] = await Promise.all([
774903
]);
775904
```
776905

906+
```java
907+
Page newPage = context.waitForPage(() -> page.click("button"));
908+
```
909+
777910
```python async
778911
async with context.expect_event("page") as event_info:
779912
await page.click("button")

docs/src/api/class-browsertype.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
1515
})();
1616
```
1717

18+
```java
19+
import com.microsoft.playwright.*;
20+
21+
public class Example {
22+
public static void main(String[] args) {
23+
try (Playwright playwright = Playwright.create()) {
24+
BrowserType chromium = playwright.chromium();
25+
Browser browser = chromium.launch();
26+
Page page = browser.newPage();
27+
page.navigate("https://example.com");
28+
// other actions...
29+
browser.close();
30+
}
31+
}
32+
}
33+
```
34+
1835
```python async
1936
import asyncio
2037
from playwright.async_api import async_playwright
@@ -102,6 +119,12 @@ const browser = await chromium.launch({ // Or 'firefox' or 'webkit'.
102119
});
103120
```
104121

122+
```java
123+
// Or "firefox" or "webkit".
124+
Browser browser = chromium.launch(new BrowserType.LaunchOptions()
125+
.withIgnoreDefaultArgs(Arrays.asList("--mute-audio")));
126+
```
127+
105128
```python async
106129
browser = await playwright.chromium.launch( # or "firefox" or "webkit".
107130
ignore_default_args=["--mute-audio"]

docs/src/api/class-dialog.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,32 @@ const { chromium } = require('playwright'); // Or 'firefox' or 'webkit'.
1313
page.on('dialog', async dialog => {
1414
console.log(dialog.message());
1515
await dialog.dismiss();
16-
await browser.close();
1716
});
18-
page.evaluate(() => alert('1'));
17+
await page.evaluate(() => alert('1'));
18+
await browser.close();
1919
})();
2020
```
2121

22+
```java
23+
import com.microsoft.playwright.*;
24+
25+
public class Example {
26+
public static void main(String[] args) {
27+
try (Playwright playwright = Playwright.create()) {
28+
BrowserType chromium = playwright.chromium();
29+
Browser browser = chromium.launch();
30+
Page page = browser.newPage();
31+
page.onDialog(dialog -> {
32+
System.out.println(dialog.message());
33+
dialog.dismiss();
34+
});
35+
page.evaluate("alert('1')");
36+
browser.close();
37+
}
38+
}
39+
}
40+
```
41+
2242
```python async
2343
import asyncio
2444
from playwright.async_api import async_playwright

0 commit comments

Comments
 (0)