@@ -19,6 +19,16 @@ await page.goto('https://example.com');
19
19
await context .close ();
20
20
```
21
21
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
+
22
32
``` python async
23
33
# create a new incognito browser context
24
34
context = await browser.new_context()
@@ -58,11 +68,18 @@ popup with `window.open('http://example.com')`, this event will fire when the ne
58
68
done and its response has started loading in the popup.
59
69
60
70
``` js
61
- const [page ] = await Promise .all ([
71
+ const [newPage ] = await Promise .all ([
62
72
context .waitForEvent (' page' ),
63
73
page .click (' a[target=_blank]' ),
64
74
]);
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" ));
66
83
```
67
84
68
85
``` python async
@@ -93,6 +110,10 @@ obtained via [`method: BrowserContext.cookies`].
93
110
await browserContext .addCookies ([cookieObject1, cookieObject2]);
94
111
```
95
112
113
+ ``` java
114
+ browserContext. addCookies(Arrays . asList(cookieObject1, cookieObject2));
115
+ ```
116
+
96
117
``` python async
97
118
await browser_context.add_cookies([cookie_object1, cookie_object2])
98
119
```
@@ -137,6 +158,11 @@ await browserContext.addInitScript({
137
158
});
138
159
```
139
160
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
+
140
166
``` python async
141
167
# in your playwright script, assuming the preload.js file is in same directory.
142
168
await browser_context.add_init_script(path = " preload.js" )
@@ -193,6 +219,13 @@ await context.grantPermissions(['clipboard-read']);
193
219
context .clearPermissions ();
194
220
```
195
221
222
+ ``` java
223
+ BrowserContext context = browser. newContext();
224
+ context. grantPermissions(Arrays . asList(" clipboard-read" ));
225
+ // do stuff ..
226
+ context. clearPermissions();
227
+ ```
228
+
196
229
``` python async
197
230
context = await browser.new_context()
198
231
await context.grant_permissions([" clipboard-read" ])
@@ -268,6 +301,30 @@ const { webkit } = require('playwright'); // Or 'chromium' or 'firefox'.
268
301
})();
269
302
```
270
303
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
+
271
328
``` python async
272
329
import asyncio
273
330
from playwright.async_api import async_playwright
@@ -334,6 +391,20 @@ await page.setContent(`
334
391
` );
335
392
```
336
393
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
+
337
408
``` python async
338
409
async def print (source , element ):
339
410
print (await element.text_content())
@@ -412,6 +483,44 @@ const crypto = require('crypto');
412
483
})();
413
484
```
414
485
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
+
415
524
``` python async
416
525
import asyncio
417
526
import hashlib
@@ -544,6 +653,14 @@ await page.goto('https://example.com');
544
653
await browser .close ();
545
654
```
546
655
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
+
547
664
``` python async
548
665
context = await browser.new_context()
549
666
page = await context.new_page()
@@ -570,6 +687,14 @@ await page.goto('https://example.com');
570
687
await browser .close ();
571
688
```
572
689
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
+
573
698
``` python async
574
699
context = await browser.new_context()
575
700
page = await context.new_page()
@@ -670,6 +795,10 @@ Sets the context's geolocation. Passing `null` or `undefined` emulates position
670
795
await browserContext .setGeolocation ({latitude: 59.95 , longitude: 30.31667 });
671
796
```
672
797
798
+ ``` java
799
+ browserContext. setGeolocation(new Geolocation (59.95 , 30.31667 ));
800
+ ```
801
+
673
802
``` python async
674
803
await browser_context.set_geolocation({" latitude" : 59.95 , " longitude" : 30.31667 })
675
804
```
@@ -774,6 +903,10 @@ const [page, _] = await Promise.all([
774
903
]);
775
904
```
776
905
906
+ ``` java
907
+ Page newPage = context. waitForPage(() - > page. click(" button" ));
908
+ ```
909
+
777
910
``` python async
778
911
async with context.expect_event(" page" ) as event_info:
779
912
await page.click(" button" )
0 commit comments