playwright-ruby-client 1.39.0 → 1.39.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,21 +12,12 @@ instance might have multiple [Page](./page) instances.
12
12
 
13
13
  This example creates a page, navigates it to a URL, and then saves a screenshot:
14
14
 
15
- ```python sync title=example_94e620cdbdfd41e2c9b14d561052ffa89535fc346038c4584ea4dd8520f5401c.py
16
- from playwright.sync_api import sync_playwright, Playwright
17
-
18
- def run(playwright: Playwright):
19
- webkit = playwright.webkit
20
- browser = webkit.launch()
21
- context = browser.new_context()
22
- page = context.new_page()
23
- page.goto("https://example.com")
24
- page.screenshot(path="screenshot.png")
25
- browser.close()
26
-
27
- with sync_playwright() as playwright:
28
- run(playwright)
29
-
15
+ ```ruby
16
+ playwright.webkit.launch do |browser|
17
+ page = browser.new_page
18
+ page.goto('https://example.com/')
19
+ page.screenshot(path: 'screenshot.png')
20
+ end
30
21
  ```
31
22
 
32
23
  The Page class emits various events (described below) which can be handled using any of Node's native
@@ -471,29 +462,18 @@ See [BrowserContext#expose_binding](./browser_context#expose_binding) for the co
471
462
 
472
463
  An example of exposing page URL to all frames in a page:
473
464
 
474
- ```python sync title=example_4f7d99a72aaea957cc5678ed8728965338d78598d7772f47fbf23c28f0eba52d.py
475
- from playwright.sync_api import sync_playwright, Playwright
476
-
477
- def run(playwright: Playwright):
478
- webkit = playwright.webkit
479
- browser = webkit.launch(headless=false)
480
- context = browser.new_context()
481
- page = context.new_page()
482
- page.expose_binding("pageURL", lambda source: source["page"].url)
483
- page.set_content("""
484
- <script>
485
- async function onClick() {
486
- document.querySelector('div').textContent = await window.pageURL();
487
- }
488
- </script>
489
- <button onclick="onClick()">Click me</button>
490
- <div></div>
491
- """)
492
- page.click("button")
493
-
494
- with sync_playwright() as playwright:
495
- run(playwright)
496
-
465
+ ```ruby
466
+ page.expose_binding("pageURL", ->(source) { source[:page].url })
467
+ page.content = <<~HTML
468
+ <script>
469
+ async function onClick() {
470
+ document.querySelector('div').textContent = await window.pageURL();
471
+ }
472
+ </script>
473
+ <button onclick="onClick()">Click me</button>
474
+ <div></div>
475
+ HTML
476
+ page.locator("button").click
497
477
  ```
498
478
 
499
479
  An example of passing an element handle:
@@ -537,35 +517,24 @@ See [BrowserContext#expose_function](./browser_context#expose_function) for cont
537
517
 
538
518
  An example of adding a `sha256` function to the page:
539
519
 
540
- ```python sync title=example_0f68a39bdff02a3df161c74e81cabb8a2ff1f09f0d09f6ef9b799a6f2f19a280.py
541
- import hashlib
542
- from playwright.sync_api import sync_playwright, Playwright
543
-
544
- def sha256(text):
545
- m = hashlib.sha256()
546
- m.update(bytes(text, "utf8"))
547
- return m.hexdigest()
548
-
549
-
550
- def run(playwright: Playwright):
551
- webkit = playwright.webkit
552
- browser = webkit.launch(headless=False)
553
- page = browser.new_page()
554
- page.expose_function("sha256", sha256)
555
- page.set_content("""
556
- <script>
557
- async function onClick() {
558
- document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
559
- }
560
- </script>
561
- <button onclick="onClick()">Click me</button>
562
- <div></div>
563
- """)
564
- page.click("button")
520
+ ```ruby
521
+ require 'digest'
565
522
 
566
- with sync_playwright() as playwright:
567
- run(playwright)
523
+ def sha256(text)
524
+ Digest::SHA256.hexdigest(text)
525
+ end
568
526
 
527
+ page.expose_function("sha256", method(:sha256))
528
+ page.content = <<~HTML
529
+ <script>
530
+ async function onClick() {
531
+ document.querySelector('div').textContent = await window.sha256('PLAYWRIGHT');
532
+ }
533
+ </script>
534
+ <button onclick="onClick()">Click me</button>
535
+ <div></div>
536
+ HTML
537
+ page.locator("button").click
569
538
  ```
570
539
 
571
540
  ## fill
@@ -1355,14 +1324,13 @@ Triggers a `change` and `input` event once all the provided options have been se
1355
1324
 
1356
1325
  **Usage**
1357
1326
 
1358
- ```python sync title=example_8260034c740933903e5a39d30a4f4e388bdffa9e82acd9a5fe1fb774752a505a.py
1359
- # Single selection matching the value or label
1360
- page.select_option("select#colors", "blue")
1327
+ ```ruby
1328
+ # single selection matching the value
1329
+ page.select_option("select#colors", value: "blue")
1361
1330
  # single selection matching both the label
1362
- page.select_option("select#colors", label="blue")
1331
+ page.select_option("select#colors", label: "blue")
1363
1332
  # multiple selection
1364
- page.select_option("select#colors", value=["red", "green", "blue"])
1365
-
1333
+ page.select_option("select#colors", value: ["red", "green", "blue"])
1366
1334
  ```
1367
1335
 
1368
1336
  ## set_checked
@@ -1676,20 +1644,9 @@ Returns when the `expression` returns a truthy value. It resolves to a JSHandle
1676
1644
 
1677
1645
  The [Page#wait_for_function](./page#wait_for_function) can be used to observe viewport size change:
1678
1646
 
1679
- ```python sync title=example_83eed1f1f00ad73f641bf4a49f672e81c4faf1ca098a4a5070afeeabb88312f5.py
1680
- from playwright.sync_api import sync_playwright, Playwright
1681
-
1682
- def run(playwright: Playwright):
1683
- webkit = playwright.webkit
1684
- browser = webkit.launch()
1685
- page = browser.new_page()
1686
- page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
1687
- page.wait_for_function("() => window.x > 0")
1688
- browser.close()
1689
-
1690
- with sync_playwright() as playwright:
1691
- run(playwright)
1692
-
1647
+ ```ruby
1648
+ page.evaluate("window.x = 0; setTimeout(() => { window.x = 100 }, 1000);")
1649
+ page.wait_for_function("() => window.x > 0")
1693
1650
  ```
1694
1651
 
1695
1652
  To pass an argument to the predicate of [Page#wait_for_function](./page#wait_for_function) function:
@@ -1857,22 +1814,12 @@ function will throw.
1857
1814
 
1858
1815
  This method works across navigations:
1859
1816
 
1860
- ```python sync title=example_903c7325fd65fcdf6f22c77fc159922a568841abce60ae1b7c54ab5837401862.py
1861
- from playwright.sync_api import sync_playwright, Playwright
1862
-
1863
- def run(playwright: Playwright):
1864
- chromium = playwright.chromium
1865
- browser = chromium.launch()
1866
- page = browser.new_page()
1867
- for current_url in ["https://google.com", "https://bbc.com"]:
1868
- page.goto(current_url, wait_until="domcontentloaded")
1869
- element = page.wait_for_selector("img")
1870
- print("Loaded image: " + str(element.get_attribute("src")))
1871
- browser.close()
1872
-
1873
- with sync_playwright() as playwright:
1874
- run(playwright)
1875
-
1817
+ ```ruby
1818
+ %w[https://google.com https://bbc.com].each do |current_url|
1819
+ page.goto(current_url, waitUntil: "domcontentloaded")
1820
+ element = page.wait_for_selector("img")
1821
+ puts "Loaded image: #{element["src"]}"
1822
+ end
1876
1823
  ```
1877
1824
 
1878
1825
  ## wait_for_timeout
@@ -8,20 +8,19 @@ sidebar_position: 10
8
8
  Playwright module provides a method to launch a browser instance. The following is a typical example of using Playwright
9
9
  to drive automation:
10
10
 
11
- ```python sync title=example_6647e5a44b0440884026a6142606dfddad75ba1e643919b015457df4ed2e198f.py
12
- from playwright.sync_api import sync_playwright, Playwright
11
+ ```ruby
12
+ require 'playwright'
13
13
 
14
- def run(playwright: Playwright):
15
- chromium = playwright.chromium # or "firefox" or "webkit".
16
- browser = chromium.launch()
17
- page = browser.new_page()
18
- page.goto("http://example.com")
19
- # other actions...
20
- browser.close()
14
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
15
+ chromium = playwright.chromium # or "firefox" or "webkit".
16
+ chromium.launch do |browser|
17
+ page = browser.new_page
18
+ page.goto('https://example.com/')
21
19
 
22
- with sync_playwright() as playwright:
23
- run(playwright)
20
+ # other actions
24
21
 
22
+ end
23
+ end
25
24
  ```
26
25
 
27
26
  ## chromium
@@ -34,22 +33,20 @@ This object can be used to launch or connect to Chromium, returning instances of
34
33
 
35
34
  Returns a dictionary of devices to be used with [Browser#new_context](./browser#new_context) or [Browser#new_page](./browser#new_page).
36
35
 
37
- ```python sync title=example_14d627977a4ad16a605ec5472d768a3324812fa8e7c57685561408fa6601e352.py
38
- from playwright.sync_api import sync_playwright, Playwright
36
+ ```ruby
37
+ require 'playwright'
39
38
 
40
- def run(playwright: Playwright):
41
- webkit = playwright.webkit
42
- iphone = playwright.devices["iPhone 6"]
43
- browser = webkit.launch()
39
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
40
+ iphone = playwright.devices["iPhone 6"]
41
+ playwright.webkit.launch do |browser|
44
42
  context = browser.new_context(**iphone)
45
- page = context.new_page()
46
- page.goto("http://example.com")
47
- # other actions...
48
- browser.close()
43
+ page = context.new_page
44
+ page.goto('https://example.com/')
49
45
 
50
- with sync_playwright() as playwright:
51
- run(playwright)
46
+ # other actions
52
47
 
48
+ end
49
+ end
53
50
  ```
54
51
 
55
52
  ## firefox
@@ -21,38 +21,33 @@ Selectors must be registered before creating the page.
21
21
 
22
22
  An example of registering selector engine that queries elements based on a tag name:
23
23
 
24
- ```python sync title=example_3e739d4f0e30e20a6a698e0e17605a841c35e65e75aa3c2642f8bfc368b33f9e.py
25
- from playwright.sync_api import sync_playwright, Playwright
26
-
27
- def run(playwright: Playwright):
28
- tag_selector = """
29
- {
30
- // Returns the first element matching given selector in the root's subtree.
31
- query(root, selector) {
32
- return root.querySelector(selector);
33
- },
34
- // Returns all elements matching given selector in the root's subtree.
35
- queryAll(root, selector) {
36
- return Array.from(root.querySelectorAll(selector));
37
- }
38
- }"""
39
-
40
- # Register the engine. Selectors will be prefixed with "tag=".
41
- playwright.selectors.register("tag", tag_selector)
42
- browser = playwright.chromium.launch()
43
- page = browser.new_page()
44
- page.set_content('<div><button>Click me</button></div>')
45
-
46
- # Use the selector prefixed with its name.
47
- button = page.locator('tag=button')
48
- # Combine it with built-in locators.
49
- page.locator('tag=div').get_by_text('Click me').click()
50
- # Can use it in any methods supporting selectors.
51
- button_count = page.locator('tag=button').count()
52
- print(button_count)
53
- browser.close()
54
-
55
- with sync_playwright() as playwright:
56
- run(playwright)
57
-
24
+ ```ruby
25
+ tag_selector = <<~JAVASCRIPT
26
+ {
27
+ // Returns the first element matching given selector in the root's subtree.
28
+ query(root, selector) {
29
+ return root.querySelector(selector);
30
+ },
31
+ // Returns all elements matching given selector in the root's subtree.
32
+ queryAll(root, selector) {
33
+ return Array.from(root.querySelectorAll(selector));
34
+ }
35
+ }
36
+ JAVASCRIPT
37
+
38
+ # Register the engine. Selectors will be prefixed with "tag=".
39
+ playwright.selectors.register("tag", script: tag_selector)
40
+ playwright.chromium.launch do |browser|
41
+ page = browser.new_page
42
+ page.content = '<div><button>Click me</button></div>'
43
+
44
+ # Use the selector prefixed with its name.
45
+ button = page.locator('tag=button')
46
+ # Combine it with other selector engines.
47
+ page.locator('tag=div').get_by_text('Click me').click
48
+
49
+ # Can use it in any methods supporting selectors.
50
+ button_count = page.locator('tag=button').count
51
+ button_count # => 1
52
+ end
58
53
  ```
@@ -527,6 +527,49 @@
527
527
 
528
528
  * ~~new_context~~
529
529
 
530
+ ## LocatorAssertions
531
+
532
+ * not_to_be_attached
533
+ * not_to_be_checked
534
+ * not_to_be_disabled
535
+ * not_to_be_editable
536
+ * not_to_be_empty
537
+ * not_to_be_enabled
538
+ * not_to_be_focused
539
+ * not_to_be_hidden
540
+ * not_to_be_in_viewport
541
+ * not_to_be_visible
542
+ * not_to_contain_text
543
+ * not_to_have_attribute
544
+ * not_to_have_class
545
+ * not_to_have_count
546
+ * not_to_have_css
547
+ * not_to_have_id
548
+ * not_to_have_js_property
549
+ * not_to_have_text
550
+ * not_to_have_value
551
+ * not_to_have_values
552
+ * to_be_attached
553
+ * to_be_checked
554
+ * to_be_disabled
555
+ * to_be_editable
556
+ * to_be_empty
557
+ * to_be_enabled
558
+ * to_be_focused
559
+ * to_be_hidden
560
+ * to_be_in_viewport
561
+ * to_be_visible
562
+ * to_contain_text
563
+ * to_have_attribute
564
+ * to_have_class
565
+ * to_have_count
566
+ * to_have_css
567
+ * to_have_id
568
+ * to_have_js_property
569
+ * to_have_text
570
+ * to_have_value
571
+ * to_have_values
572
+
530
573
  ## Android
531
574
 
532
575
  * ~~connect~~
@@ -48,7 +48,7 @@ module Playwright
48
48
 
49
49
  private def with_logging(&block)
50
50
  locations = caller_locations
51
- first_api_call_location_idx = locations.index { |loc| loc.absolute_path.include?('playwright_api') }
51
+ first_api_call_location_idx = locations.index { |loc| loc.absolute_path&.include?('playwright_api') }
52
52
  unless first_api_call_location_idx
53
53
  return block.call(nil)
54
54
  end
@@ -49,4 +49,6 @@ module Playwright
49
49
 
50
50
  attr_reader :error, :page
51
51
  end
52
+
53
+ class AssertionError < StandardError; end
52
54
  end
@@ -24,7 +24,7 @@ module Playwright
24
24
  @handles << value.channel
25
25
  { h: index }
26
26
  when nil
27
- { v: 'undefined' }
27
+ { v: 'null' }
28
28
  when Float::NAN
29
29
  { v: 'NaN'}
30
30
  when Float::INFINITY