playwright-ruby-client 0.6.1 → 0.6.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/documentation/docs/api/browser.md +4 -1
- data/documentation/docs/api/browser_context.md +3 -4
- data/documentation/docs/api/browser_type.md +54 -1
- data/documentation/docs/api/dialog.md +15 -18
- data/documentation/docs/api/element_handle.md +28 -50
- data/documentation/docs/api/experimental/android.md +3 -2
- data/documentation/docs/api/experimental/android_device.md +1 -0
- data/documentation/docs/api/file_chooser.md +4 -5
- data/documentation/docs/api/frame.md +3 -4
- data/documentation/docs/api/js_handle.md +11 -14
- data/documentation/docs/api/page.md +163 -230
- data/documentation/docs/api/route.md +20 -21
- data/documentation/docs/api/tracing.md +8 -15
- data/documentation/docs/api/web_socket.md +1 -1
- data/documentation/docs/api/worker.md +18 -1
- data/documentation/docs/article/guides/rails_integration.md +156 -2
- data/documentation/docs/article/guides/recording_video.md +79 -0
- data/documentation/docs/include/api_coverage.md +5 -4
- data/documentation/package.json +1 -1
- data/documentation/yarn.lock +478 -498
- data/lib/playwright/channel_owners/binding_call.rb +1 -1
- data/lib/playwright/channel_owners/browser.rb +15 -27
- data/lib/playwright/channel_owners/browser_context.rb +13 -5
- data/lib/playwright/channel_owners/browser_type.rb +23 -8
- data/lib/playwright/channel_owners/page.rb +8 -7
- data/lib/playwright/channel_owners/web_socket.rb +4 -0
- data/lib/playwright/channel_owners/worker.rb +4 -0
- data/lib/playwright/playwright_api.rb +16 -1
- data/lib/playwright/tracing_impl.rb +9 -9
- data/lib/playwright/version.rb +1 -1
- data/lib/playwright_api/android.rb +9 -8
- data/lib/playwright_api/android_device.rb +8 -7
- data/lib/playwright_api/browser.rb +12 -9
- data/lib/playwright_api/browser_context.rb +13 -14
- data/lib/playwright_api/browser_type.rb +13 -11
- data/lib/playwright_api/console_message.rb +6 -6
- data/lib/playwright_api/dialog.rb +6 -6
- data/lib/playwright_api/element_handle.rb +6 -6
- data/lib/playwright_api/frame.rb +6 -6
- data/lib/playwright_api/js_handle.rb +6 -6
- data/lib/playwright_api/page.rb +38 -24
- data/lib/playwright_api/playwright.rb +6 -6
- data/lib/playwright_api/request.rb +6 -6
- data/lib/playwright_api/response.rb +8 -8
- data/lib/playwright_api/route.rb +6 -6
- data/lib/playwright_api/selectors.rb +6 -6
- data/lib/playwright_api/tracing.rb +6 -12
- data/lib/playwright_api/web_socket.rb +22 -0
- data/lib/playwright_api/worker.rb +22 -0
- metadata +5 -2
@@ -224,9 +224,9 @@ The snippet below dispatches the `click` event on the element. Regardless of the
|
|
224
224
|
`click` is dispatched. This is equivalent to calling
|
225
225
|
[element.click()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/click).
|
226
226
|
|
227
|
-
```
|
227
|
+
```ruby
|
228
|
+
page.content = '<button id="submit">Send</button>'
|
228
229
|
page.dispatch_event("button#submit", "click")
|
229
|
-
|
230
230
|
```
|
231
231
|
|
232
232
|
Under the hood, it creates an instance of an event based on the given `type`, initializes it with `eventInit` properties
|
@@ -243,11 +243,12 @@ Since `eventInit` is event-specific, please refer to the events documentation fo
|
|
243
243
|
|
244
244
|
You can also specify [JSHandle](./js_handle) as the property value if you want live objects to be passed into the event:
|
245
245
|
|
246
|
-
```
|
246
|
+
```ruby
|
247
|
+
page.content = '<div id="source">Drag</div>'
|
248
|
+
|
247
249
|
# note you can only create data_transfer in chromium and firefox
|
248
250
|
data_transfer = page.evaluate_handle("new DataTransfer()")
|
249
|
-
page.dispatch_event("#source", "dragstart", {
|
250
|
-
|
251
|
+
page.dispatch_event("#source", "dragstart", eventInit: { dataTransfer: data_transfer })
|
251
252
|
```
|
252
253
|
|
253
254
|
|
@@ -255,40 +256,30 @@ page.dispatch_event("#source", "dragstart", { "dataTransfer": data_transfer })
|
|
255
256
|
## emulate_media
|
256
257
|
|
257
258
|
```
|
258
|
-
def emulate_media(colorScheme: nil, media: nil)
|
259
|
+
def emulate_media(colorScheme: nil, media: nil, reducedMotion: nil)
|
259
260
|
```
|
260
261
|
|
261
262
|
This method changes the `CSS media type` through the `media` argument, and/or the `'prefers-colors-scheme'` media
|
262
263
|
feature, using the `colorScheme` argument.
|
263
264
|
|
264
|
-
```
|
265
|
-
page.evaluate("matchMedia('screen').matches")
|
266
|
-
#
|
267
|
-
page.evaluate("matchMedia('print').matches")
|
268
|
-
# → False
|
269
|
-
|
270
|
-
page.emulate_media(media="print")
|
271
|
-
page.evaluate("matchMedia('screen').matches")
|
272
|
-
# → False
|
273
|
-
page.evaluate("matchMedia('print').matches")
|
274
|
-
# → True
|
265
|
+
```ruby
|
266
|
+
page.evaluate("matchMedia('screen').matches") # => true
|
267
|
+
page.evaluate("matchMedia('print').matches") # => false
|
275
268
|
|
276
|
-
page.emulate_media()
|
277
|
-
page.evaluate("matchMedia('screen').matches")
|
278
|
-
#
|
279
|
-
page.evaluate("matchMedia('print').matches")
|
280
|
-
# → False
|
269
|
+
page.emulate_media(media: "print")
|
270
|
+
page.evaluate("matchMedia('screen').matches") # => false
|
271
|
+
page.evaluate("matchMedia('print').matches") # => true
|
281
272
|
|
273
|
+
page.emulate_media
|
274
|
+
page.evaluate("matchMedia('screen').matches") # => true
|
275
|
+
page.evaluate("matchMedia('print').matches") # => false
|
282
276
|
```
|
283
277
|
|
284
|
-
```
|
285
|
-
page.emulate_media(
|
286
|
-
page.evaluate("matchMedia('(prefers-color-scheme: dark)').matches")
|
287
|
-
#
|
288
|
-
page.evaluate("matchMedia('(prefers-color-scheme:
|
289
|
-
# → False
|
290
|
-
page.evaluate("matchMedia('(prefers-color-scheme: no-preference)').matches")
|
291
|
-
|
278
|
+
```ruby
|
279
|
+
page.emulate_media(colorScheme="dark")
|
280
|
+
page.evaluate("matchMedia('(prefers-color-scheme: dark)').matches") # => true
|
281
|
+
page.evaluate("matchMedia('(prefers-color-scheme: light)').matches") # => false
|
282
|
+
page.evaluate("matchMedia('(prefers-color-scheme: no-preference)').matches") # => false
|
292
283
|
```
|
293
284
|
|
294
285
|
|
@@ -370,7 +361,7 @@ puts page.evaluate("1 + #{x}") # => "11"
|
|
370
361
|
```ruby
|
371
362
|
body_handle = page.query_selector("body")
|
372
363
|
html = page.evaluate("([body, suffix]) => body.innerHTML + suffix", arg: [body_handle, "hello"])
|
373
|
-
body_handle.dispose
|
364
|
+
body_handle.dispose
|
374
365
|
```
|
375
366
|
|
376
367
|
Shortcut for main frame's [Frame#evaluate](./frame#evaluate).
|
@@ -389,17 +380,15 @@ The only difference between [Page#evaluate](./page#evaluate) and [Page#evaluate_
|
|
389
380
|
If the function passed to the [Page#evaluate_handle](./page#evaluate_handle) returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), then [Page#evaluate_handle](./page#evaluate_handle)
|
390
381
|
would wait for the promise to resolve and return its value.
|
391
382
|
|
392
|
-
```
|
383
|
+
```ruby
|
393
384
|
a_window_handle = page.evaluate_handle("Promise.resolve(window)")
|
394
385
|
a_window_handle # handle for the window object.
|
395
|
-
|
396
386
|
```
|
397
387
|
|
398
388
|
A string can also be passed in instead of a function:
|
399
389
|
|
400
|
-
```
|
390
|
+
```ruby
|
401
391
|
a_handle = page.evaluate_handle("document") # handle for the "document"
|
402
|
-
|
403
392
|
```
|
404
393
|
|
405
394
|
[JSHandle](./js_handle) instances can be passed as an argument to the [Page#evaluate_handle](./page#evaluate_handle):
|
@@ -407,8 +396,8 @@ a_handle = page.evaluate_handle("document") # handle for the "document"
|
|
407
396
|
```ruby
|
408
397
|
body_handle = page.evaluate_handle("document.body")
|
409
398
|
result_handle = page.evaluate_handle("body => body.innerHTML", arg: body_handle)
|
410
|
-
puts result_handle.json_value
|
411
|
-
result_handle.dispose
|
399
|
+
puts result_handle.json_value
|
400
|
+
result_handle.dispose
|
412
401
|
```
|
413
402
|
|
414
403
|
|
@@ -423,8 +412,7 @@ The method adds a function called `name` on the `window` object of every frame i
|
|
423
412
|
executes `callback` and returns a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) which resolves to the return value of `callback`. If the `callback` returns
|
424
413
|
a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), it will be awaited.
|
425
414
|
|
426
|
-
The first argument of the `callback` function contains information about the caller: `{
|
427
|
-
page: Page, frame: Frame }`.
|
415
|
+
The first argument of the `callback` function contains information about the caller: `{ browser_context: BrowserContext, page: Page, frame: Frame }`.
|
428
416
|
|
429
417
|
See [BrowserContext#expose_binding](./browser_context#expose_binding) for the context-wide version.
|
430
418
|
|
@@ -432,46 +420,39 @@ See [BrowserContext#expose_binding](./browser_context#expose_binding) for the co
|
|
432
420
|
|
433
421
|
An example of exposing page URL to all frames in a page:
|
434
422
|
|
435
|
-
```
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
document.querySelector('div').textContent = await window.pageURL();
|
448
|
-
}
|
449
|
-
</script>
|
450
|
-
<button onclick="onClick()">Click me</button>
|
451
|
-
<div></div>
|
452
|
-
""")
|
453
|
-
page.click("button")
|
454
|
-
|
455
|
-
with sync_playwright() as playwright:
|
456
|
-
run(playwright)
|
457
|
-
|
423
|
+
```ruby
|
424
|
+
page.expose_binding("pageURL", ->(source) { source[:page].url })
|
425
|
+
page.content = <<~HTML
|
426
|
+
<script>
|
427
|
+
async function onClick() {
|
428
|
+
document.querySelector('div').textContent = await window.pageURL();
|
429
|
+
}
|
430
|
+
</script>
|
431
|
+
<button onclick="onClick()">Click me</button>
|
432
|
+
<div></div>
|
433
|
+
HTML
|
434
|
+
page.click("button")
|
458
435
|
```
|
459
436
|
|
460
437
|
An example of passing an element handle:
|
461
438
|
|
462
|
-
```
|
463
|
-
def
|
464
|
-
|
439
|
+
```ruby
|
440
|
+
def print_text(source, element)
|
441
|
+
element.text_content
|
442
|
+
end
|
465
443
|
|
466
|
-
page.expose_binding("clicked",
|
467
|
-
page.
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
444
|
+
page.expose_binding("clicked", method(:print_text), handle: true)
|
445
|
+
page.content = <<~HTML
|
446
|
+
<script>
|
447
|
+
document.addEventListener('click', async (event) => {
|
448
|
+
alert(await window.clicked(event.target));
|
449
|
+
})
|
450
|
+
</script>
|
451
|
+
<div>Click me</div>
|
452
|
+
<div>Or click me</div>
|
453
|
+
HTML
|
474
454
|
|
455
|
+
page.click('div')
|
475
456
|
```
|
476
457
|
|
477
458
|
|
@@ -491,37 +472,26 @@ See [BrowserContext#expose_function](./browser_context#expose_function) for cont
|
|
491
472
|
|
492
473
|
> NOTE: Functions installed via [Page#expose_function](./page#expose_function) survive navigations.
|
493
474
|
|
494
|
-
An example of adding
|
495
|
-
|
496
|
-
```python sync title=example_496ab45e0c5f4c47869f66c2b738fbd9eef0ef4065fa923caf9c929e50e14c21.py
|
497
|
-
import hashlib
|
498
|
-
from playwright.sync_api import sync_playwright
|
475
|
+
An example of adding a `sha256` function to the page:
|
499
476
|
|
500
|
-
|
501
|
-
|
502
|
-
m.update(bytes(text, "utf8"))
|
503
|
-
return m.hexdigest()
|
504
|
-
|
505
|
-
|
506
|
-
def run(playwright):
|
507
|
-
webkit = playwright.webkit
|
508
|
-
browser = webkit.launch(headless=False)
|
509
|
-
page = browser.new_page()
|
510
|
-
page.expose_function("sha1", sha1)
|
511
|
-
page.set_content("""
|
512
|
-
<script>
|
513
|
-
async function onClick() {
|
514
|
-
document.querySelector('div').textContent = await window.sha1('PLAYWRIGHT');
|
515
|
-
}
|
516
|
-
</script>
|
517
|
-
<button onclick="onClick()">Click me</button>
|
518
|
-
<div></div>
|
519
|
-
""")
|
520
|
-
page.click("button")
|
477
|
+
```ruby
|
478
|
+
require 'digest'
|
521
479
|
|
522
|
-
|
523
|
-
|
480
|
+
def sha1(text)
|
481
|
+
Digest::SHA256.hexdigest(text)
|
482
|
+
end
|
524
483
|
|
484
|
+
page.expose_function("sha256", method(:sha256))
|
485
|
+
page.content = <<~HTML
|
486
|
+
<script>
|
487
|
+
async function onClick() {
|
488
|
+
document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
|
489
|
+
}
|
490
|
+
</script>
|
491
|
+
<button onclick="onClick()">Click me</button>
|
492
|
+
<div></div>
|
493
|
+
HTML
|
494
|
+
page.click("button")
|
525
495
|
```
|
526
496
|
|
527
497
|
|
@@ -564,14 +534,12 @@ def frame(name: nil, url: nil)
|
|
564
534
|
|
565
535
|
Returns frame matching the specified criteria. Either `name` or `url` must be specified.
|
566
536
|
|
567
|
-
```
|
568
|
-
frame = page.frame(name
|
569
|
-
|
537
|
+
```ruby
|
538
|
+
frame = page.frame(name: "frame-name")
|
570
539
|
```
|
571
540
|
|
572
|
-
```
|
573
|
-
frame = page.frame(url
|
574
|
-
|
541
|
+
```ruby
|
542
|
+
frame = page.frame(url: /.*domain.*/)
|
575
543
|
```
|
576
544
|
|
577
545
|
|
@@ -786,11 +754,10 @@ Returns the PDF buffer.
|
|
786
754
|
[`-webkit-print-color-adjust`](https://developer.mozilla.org/en-US/docs/Web/CSS/-webkit-print-color-adjust) property to
|
787
755
|
force rendering of exact colors.
|
788
756
|
|
789
|
-
```
|
757
|
+
```ruby
|
790
758
|
# generates a pdf with "screen" media type.
|
791
|
-
page.emulate_media(media
|
792
|
-
page.pdf(path
|
793
|
-
|
759
|
+
page.emulate_media(media: "screen")
|
760
|
+
page.pdf(path: "page.pdf")
|
794
761
|
```
|
795
762
|
|
796
763
|
The `width`, `height`, and `margin` options accept values labeled with units. Unlabeled values are treated as pixels.
|
@@ -852,17 +819,14 @@ texts.
|
|
852
819
|
Shortcuts such as `key: "Control+o"` or `key: "Control+Shift+T"` are supported as well. When specified with the
|
853
820
|
modifier, modifier is pressed and being held while the subsequent key is being pressed.
|
854
821
|
|
855
|
-
```
|
856
|
-
page = browser.new_page()
|
822
|
+
```ruby
|
857
823
|
page.goto("https://keycode.info")
|
858
824
|
page.press("body", "A")
|
859
|
-
page.screenshot(path
|
825
|
+
page.screenshot(path: "a.png")
|
860
826
|
page.press("body", "ArrowLeft")
|
861
|
-
page.screenshot(path
|
827
|
+
page.screenshot(path: "arrow_left.png")
|
862
828
|
page.press("body", "Shift+O")
|
863
|
-
page.screenshot(path
|
864
|
-
browser.close()
|
865
|
-
|
829
|
+
page.screenshot(path: "o.png")
|
866
830
|
```
|
867
831
|
|
868
832
|
|
@@ -912,35 +876,31 @@ Once routing is enabled, every request matching the url pattern will stall unles
|
|
912
876
|
|
913
877
|
An example of a naive handler that aborts all image requests:
|
914
878
|
|
915
|
-
```
|
916
|
-
page
|
917
|
-
page.route("**/*.{png,jpg,jpeg}", lambda route: route.abort())
|
879
|
+
```ruby
|
880
|
+
page.route("**/*.{png,jpg,jpeg}", ->(route, request) { route.abort })
|
918
881
|
page.goto("https://example.com")
|
919
|
-
browser.close()
|
920
|
-
|
921
882
|
```
|
922
883
|
|
923
884
|
or the same snippet using a regex pattern instead:
|
924
885
|
|
925
|
-
```
|
926
|
-
page
|
927
|
-
page.route(re.compile(r"(\.png$)|(\.jpg$)"), lambda route: route.abort())
|
886
|
+
```ruby
|
887
|
+
page.route(/\.(png|jpg)$/, ->(route, request) { route.abort })
|
928
888
|
page.goto("https://example.com")
|
929
|
-
browser.close()
|
930
|
-
|
931
889
|
```
|
932
890
|
|
933
891
|
It is possible to examine the request to decide the route action. For example, mocking all requests that contain some
|
934
892
|
post data, and leaving all other requests as is:
|
935
893
|
|
936
|
-
```
|
937
|
-
def handle_route(route)
|
938
|
-
if
|
939
|
-
|
894
|
+
```ruby
|
895
|
+
def handle_route(route, request)
|
896
|
+
if request.post_data["my-string"]
|
897
|
+
mocked_data = request.post_data.merge({ "my-string" => 'mocked-data'})
|
898
|
+
route.fulfill(postData: mocked_data)
|
940
899
|
else
|
941
|
-
route.
|
942
|
-
|
943
|
-
|
900
|
+
route.continue
|
901
|
+
end
|
902
|
+
end
|
903
|
+
page.route("/api/**", method(:handle_route))
|
944
904
|
```
|
945
905
|
|
946
906
|
Page routes take precedence over browser context routes (set up with [BrowserContext#route](./browser_context#route)) when request
|
@@ -989,14 +949,13 @@ Returns the array of option values that have been successfully selected.
|
|
989
949
|
|
990
950
|
Triggers a `change` and `input` event once all the provided options have been selected.
|
991
951
|
|
992
|
-
```
|
952
|
+
```ruby
|
993
953
|
# single selection matching the value
|
994
|
-
page.select_option("select#colors", "blue")
|
954
|
+
page.select_option("select#colors", value: "blue")
|
995
955
|
# single selection matching both the label
|
996
|
-
page.select_option("select#colors", label
|
956
|
+
page.select_option("select#colors", label: "blue")
|
997
957
|
# multiple selection
|
998
|
-
page.select_option("select#colors", value
|
999
|
-
|
958
|
+
page.select_option("select#colors", value: ["red", "green", "blue"])
|
1000
959
|
```
|
1001
960
|
|
1002
961
|
Shortcut for main frame's [Frame#select_option](./frame#select_option).
|
@@ -1076,11 +1035,9 @@ In the case of multiple pages in a single browser, each page can have its own vi
|
|
1076
1035
|
`page.setViewportSize` will resize the page. A lot of websites don't expect phones to change size, so you should set the
|
1077
1036
|
viewport size before navigating to the page.
|
1078
1037
|
|
1079
|
-
```
|
1080
|
-
page =
|
1081
|
-
page.set_viewport_size({"width": 640, "height": 480})
|
1038
|
+
```ruby
|
1039
|
+
page.viewport_size = { width: 640, height: 480 }
|
1082
1040
|
page.goto("https://example.com")
|
1083
|
-
|
1084
1041
|
```
|
1085
1042
|
|
1086
1043
|
|
@@ -1145,10 +1102,9 @@ fine-grained keyboard events. To fill values in form fields, use [Page#fill](./p
|
|
1145
1102
|
|
1146
1103
|
To press a special key, like `Control` or `ArrowDown`, use [Keyboard#press](./keyboard#press).
|
1147
1104
|
|
1148
|
-
```
|
1105
|
+
```ruby
|
1149
1106
|
page.type("#mytextarea", "hello") # types instantly
|
1150
|
-
page.type("#mytextarea", "world", delay
|
1151
|
-
|
1107
|
+
page.type("#mytextarea", "world", delay: 100) # types slower, like a user
|
1152
1108
|
```
|
1153
1109
|
|
1154
1110
|
Shortcut for main frame's [Frame#type](./frame#type).
|
@@ -1221,7 +1177,7 @@ def expect_console_message(predicate: nil, timeout: nil, &block)
|
|
1221
1177
|
|
1222
1178
|
Performs action and waits for a [ConsoleMessage](./console_message) to be logged by in the page. If predicate is provided, it passes
|
1223
1179
|
[ConsoleMessage](./console_message) value into the `predicate` function and waits for `predicate(message)` to return a truthy value. Will
|
1224
|
-
throw an error if the page is closed before the console event is fired.
|
1180
|
+
throw an error if the page is closed before the [`event: Page.console`] event is fired.
|
1225
1181
|
|
1226
1182
|
## expect_download
|
1227
1183
|
|
@@ -1242,15 +1198,13 @@ def expect_event(event, predicate: nil, timeout: nil, &block)
|
|
1242
1198
|
Waits for event to fire and passes its value into the predicate function. Returns when the predicate returns truthy
|
1243
1199
|
value. Will throw an error if the page is closed before the event is fired. Returns the event data value.
|
1244
1200
|
|
1245
|
-
```
|
1246
|
-
|
1247
|
-
|
1248
|
-
|
1249
|
-
|
1201
|
+
```ruby
|
1202
|
+
frame = page.expect_event("framenavigated") do
|
1203
|
+
page.click("button")
|
1204
|
+
end
|
1250
1205
|
```
|
1251
1206
|
|
1252
1207
|
|
1253
|
-
|
1254
1208
|
## expect_file_chooser
|
1255
1209
|
|
1256
1210
|
```
|
@@ -1258,7 +1212,7 @@ def expect_file_chooser(predicate: nil, timeout: nil, &block)
|
|
1258
1212
|
```
|
1259
1213
|
|
1260
1214
|
Performs action and waits for a new [FileChooser](./file_chooser) to be created. If predicate is provided, it passes [FileChooser](./file_chooser) value
|
1261
|
-
into the `predicate` function and waits for `predicate(fileChooser)` to return a truthy value. Will throw an error if
|
1215
|
+
into the `predicate` function and waits for `predicate.call(fileChooser)` to return a truthy value. Will throw an error if
|
1262
1216
|
the page is closed before the file chooser is opened.
|
1263
1217
|
|
1264
1218
|
## wait_for_function
|
@@ -1271,28 +1225,16 @@ Returns when the `expression` returns a truthy value. It resolves to a JSHandle
|
|
1271
1225
|
|
1272
1226
|
The [Page#wait_for_function](./page#wait_for_function) can be used to observe viewport size change:
|
1273
1227
|
|
1274
|
-
```
|
1275
|
-
|
1276
|
-
|
1277
|
-
def run(playwright):
|
1278
|
-
webkit = playwright.webkit
|
1279
|
-
browser = webkit.launch()
|
1280
|
-
page = browser.new_page()
|
1281
|
-
page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
|
1282
|
-
page.wait_for_function("() => window.x > 0")
|
1283
|
-
browser.close()
|
1284
|
-
|
1285
|
-
with sync_playwright() as playwright:
|
1286
|
-
run(playwright)
|
1287
|
-
|
1228
|
+
```ruby
|
1229
|
+
page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
|
1230
|
+
page.wait_for_function("() => window.x > 0")
|
1288
1231
|
```
|
1289
1232
|
|
1290
1233
|
To pass an argument to the predicate of [Page#wait_for_function](./page#wait_for_function) function:
|
1291
1234
|
|
1292
|
-
```
|
1235
|
+
```ruby
|
1293
1236
|
selector = ".foo"
|
1294
|
-
page.wait_for_function("selector => !!document.querySelector(selector)", selector)
|
1295
|
-
|
1237
|
+
page.wait_for_function("selector => !!document.querySelector(selector)", arg: selector)
|
1296
1238
|
```
|
1297
1239
|
|
1298
1240
|
Shortcut for main frame's [Frame#wait_for_function](./frame#wait_for_function).
|
@@ -1308,20 +1250,19 @@ Returns when the required load state has been reached.
|
|
1308
1250
|
This resolves when the page reaches a required load state, `load` by default. The navigation must have been committed
|
1309
1251
|
when this method is called. If current document has already reached the required state, resolves immediately.
|
1310
1252
|
|
1311
|
-
```
|
1253
|
+
```ruby
|
1312
1254
|
page.click("button") # click triggers navigation.
|
1313
|
-
page.wait_for_load_state
|
1314
|
-
|
1255
|
+
page.wait_for_load_state # the promise resolves after "load" event.
|
1315
1256
|
```
|
1316
1257
|
|
1317
|
-
```
|
1318
|
-
|
1319
|
-
|
1320
|
-
|
1321
|
-
# Following resolves after "domcontentloaded" event.
|
1322
|
-
popup.wait_for_load_state("domcontentloaded")
|
1323
|
-
print(popup.title()) # popup is ready to use.
|
1258
|
+
```ruby
|
1259
|
+
popup = page.expect_popup do
|
1260
|
+
page.click("button") # click triggers a popup.
|
1261
|
+
end
|
1324
1262
|
|
1263
|
+
# Following resolves after "domcontentloaded" event.
|
1264
|
+
popup.wait_for_load_state("domcontentloaded")
|
1265
|
+
puts popup.title # popup is ready to use.
|
1325
1266
|
```
|
1326
1267
|
|
1327
1268
|
Shortcut for main frame's [Frame#wait_for_load_state](./frame#wait_for_load_state).
|
@@ -1340,17 +1281,16 @@ This resolves when the page navigates to a new URL or reloads. It is useful for
|
|
1340
1281
|
cause the page to navigate. e.g. The click target has an `onclick` handler that triggers navigation from a `setTimeout`.
|
1341
1282
|
Consider this example:
|
1342
1283
|
|
1343
|
-
```
|
1344
|
-
|
1345
|
-
|
1346
|
-
# Resolves after navigation has finished
|
1347
|
-
|
1284
|
+
```ruby
|
1285
|
+
page.expect_navigation do
|
1286
|
+
page.click("a.delayed-navigation") # clicking the link will indirectly cause a navigation
|
1287
|
+
end # Resolves after navigation has finished
|
1348
1288
|
```
|
1349
1289
|
|
1350
1290
|
> NOTE: Usage of the [History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API) to change the URL is
|
1351
1291
|
considered a navigation.
|
1352
1292
|
|
1353
|
-
Shortcut for main frame's [
|
1293
|
+
Shortcut for main frame's [Frame#expect_navigation](./frame#expect_navigation).
|
1354
1294
|
|
1355
1295
|
## expect_popup
|
1356
1296
|
|
@@ -1358,29 +1298,32 @@ Shortcut for main frame's [`method: Frame.waitForNavigation`].
|
|
1358
1298
|
def expect_popup(predicate: nil, timeout: nil, &block)
|
1359
1299
|
```
|
1360
1300
|
|
1361
|
-
Performs action and waits for a popup [Page](./page). If predicate is provided, it passes [
|
1362
|
-
function and waits for `predicate(page)` to return a truthy value. Will throw an error if the page is closed before the
|
1363
|
-
popup event is fired.
|
1301
|
+
Performs action and waits for a popup [Page](./page). If predicate is provided, it passes popup [Page](./page) value into the predicate function and waits for `predicate.call(page)` to return a truthy value. Will throw an error if the page is closed before the popup event is fired.
|
1364
1302
|
|
1365
1303
|
## expect_request
|
1366
1304
|
|
1367
1305
|
```
|
1368
|
-
def expect_request(urlOrPredicate, timeout: nil)
|
1306
|
+
def expect_request(urlOrPredicate, timeout: nil, &block)
|
1369
1307
|
```
|
1370
1308
|
|
1371
1309
|
Waits for the matching request and returns it. See [waiting for event](https://playwright.dev/python/docs/events) for more details
|
1372
1310
|
about events.
|
1373
1311
|
|
1374
|
-
```
|
1375
|
-
|
1376
|
-
|
1377
|
-
|
1312
|
+
```ruby
|
1313
|
+
page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
|
1314
|
+
request = page.expect_request(/example.com\/resource/) do
|
1315
|
+
page.click("input")
|
1316
|
+
end
|
1317
|
+
puts request.headers
|
1378
1318
|
|
1379
|
-
#
|
1380
|
-
with page.expect_request(lambda request: request.url == "http://example.com" and request.method == "get") as second:
|
1381
|
-
page.click('img')
|
1382
|
-
second_request = second.value
|
1319
|
+
page.wait_for_load_state # wait for request finished.
|
1383
1320
|
|
1321
|
+
# or with a predicate
|
1322
|
+
page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
|
1323
|
+
request = page.expect_request(->(req) { req.url.start_with? 'https://example.com/resource' }) do
|
1324
|
+
page.click("input")
|
1325
|
+
end
|
1326
|
+
puts request.headers
|
1384
1327
|
```
|
1385
1328
|
|
1386
1329
|
|
@@ -1388,23 +1331,24 @@ second_request = second.value
|
|
1388
1331
|
## expect_response
|
1389
1332
|
|
1390
1333
|
```
|
1391
|
-
def expect_response(urlOrPredicate, timeout: nil)
|
1334
|
+
def expect_response(urlOrPredicate, timeout: nil, &block)
|
1392
1335
|
```
|
1393
1336
|
|
1394
1337
|
Returns the matched response. See [waiting for event](https://playwright.dev/python/docs/events) for more details about events.
|
1395
1338
|
|
1396
|
-
```
|
1397
|
-
|
1398
|
-
|
1399
|
-
|
1400
|
-
|
1401
|
-
|
1402
|
-
# or with a lambda
|
1403
|
-
with page.expect_response(lambda response: response.url == "https://example.com" and response.status === 200) as response_info:
|
1404
|
-
page.click("input")
|
1405
|
-
response = response_info.value
|
1406
|
-
return response.ok
|
1339
|
+
```ruby
|
1340
|
+
page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
|
1341
|
+
response = page.expect_response(/example.com\/resource/) do
|
1342
|
+
page.click("input")
|
1343
|
+
end
|
1344
|
+
puts response.body
|
1407
1345
|
|
1346
|
+
# or with a predicate
|
1347
|
+
page.content = '<form action="https://example.com/resource"><input type="submit" /></form>'
|
1348
|
+
response = page.expect_response(->(res) { res.url.start_with? 'https://example.com/resource' }) do
|
1349
|
+
page.click("input")
|
1350
|
+
end
|
1351
|
+
puts response.body
|
1408
1352
|
```
|
1409
1353
|
|
1410
1354
|
|
@@ -1424,22 +1368,12 @@ selector doesn't satisfy the condition for the `timeout` milliseconds, the funct
|
|
1424
1368
|
|
1425
1369
|
This method works across navigations:
|
1426
1370
|
|
1427
|
-
```
|
1428
|
-
|
1429
|
-
|
1430
|
-
|
1431
|
-
|
1432
|
-
|
1433
|
-
page = browser.new_page()
|
1434
|
-
for current_url in ["https://google.com", "https://bbc.com"]:
|
1435
|
-
page.goto(current_url, wait_until="domcontentloaded")
|
1436
|
-
element = page.wait_for_selector("img")
|
1437
|
-
print("Loaded image: " + str(element.get_attribute("src")))
|
1438
|
-
browser.close()
|
1439
|
-
|
1440
|
-
with sync_playwright() as playwright:
|
1441
|
-
run(playwright)
|
1442
|
-
|
1371
|
+
```ruby
|
1372
|
+
%w[https://google.com https://bbc.com].each do |current_url|
|
1373
|
+
page.goto(current_url, waitUntil: "domcontentloaded")
|
1374
|
+
element = page.wait_for_selector("img")
|
1375
|
+
puts "Loaded image: #{element["src"]}"
|
1376
|
+
end
|
1443
1377
|
```
|
1444
1378
|
|
1445
1379
|
|
@@ -1452,10 +1386,9 @@ def wait_for_url(url, timeout: nil, waitUntil: nil)
|
|
1452
1386
|
|
1453
1387
|
Waits for the main frame to navigate to the given URL.
|
1454
1388
|
|
1455
|
-
```
|
1389
|
+
```ruby
|
1456
1390
|
page.click("a.delayed-navigation") # clicking the link will indirectly cause a navigation
|
1457
1391
|
page.wait_for_url("**/target.html")
|
1458
|
-
|
1459
1392
|
```
|
1460
1393
|
|
1461
1394
|
Shortcut for main frame's [Frame#wait_for_url](./frame#wait_for_url).
|