playwright-ruby-client 0.6.0 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/documentation/README.md +33 -0
  3. data/documentation/babel.config.js +3 -0
  4. data/documentation/docs/api/accessibility.md +7 -0
  5. data/documentation/docs/api/browser.md +185 -0
  6. data/documentation/docs/api/browser_context.md +398 -0
  7. data/documentation/docs/api/browser_type.md +105 -0
  8. data/documentation/docs/api/cdp_session.md +7 -0
  9. data/documentation/docs/api/console_message.md +41 -0
  10. data/documentation/docs/api/dialog.md +74 -0
  11. data/documentation/docs/api/element_handle.md +640 -0
  12. data/documentation/docs/api/experimental/_category_.yml +3 -0
  13. data/documentation/docs/api/experimental/android.md +25 -0
  14. data/documentation/docs/api/experimental/android_device.md +91 -0
  15. data/documentation/docs/api/experimental/android_input.md +38 -0
  16. data/documentation/docs/api/experimental/android_socket.md +7 -0
  17. data/documentation/docs/api/experimental/android_web_view.md +7 -0
  18. data/documentation/docs/api/file_chooser.md +51 -0
  19. data/documentation/docs/api/frame.md +867 -0
  20. data/documentation/docs/api/js_handle.md +116 -0
  21. data/documentation/docs/api/keyboard.md +157 -0
  22. data/documentation/docs/api/mouse.md +69 -0
  23. data/documentation/docs/api/page.md +1469 -0
  24. data/documentation/docs/api/playwright.md +63 -0
  25. data/documentation/docs/api/request.md +188 -0
  26. data/documentation/docs/api/response.md +97 -0
  27. data/documentation/docs/api/route.md +80 -0
  28. data/documentation/docs/api/selectors.md +23 -0
  29. data/documentation/docs/api/touchscreen.md +8 -0
  30. data/documentation/docs/api/tracing.md +54 -0
  31. data/documentation/docs/api/web_socket.md +7 -0
  32. data/documentation/docs/api/worker.md +7 -0
  33. data/documentation/docs/article/api_coverage.mdx +11 -0
  34. data/documentation/docs/article/getting_started.md +152 -0
  35. data/documentation/docs/article/guides/_category_.yml +3 -0
  36. data/documentation/docs/article/guides/download_playwright_driver.md +49 -0
  37. data/documentation/docs/article/guides/launch_browser.md +119 -0
  38. data/documentation/docs/article/guides/rails_integration.md +51 -0
  39. data/{docs → documentation/docs/include}/api_coverage.md +0 -0
  40. data/documentation/docusaurus.config.js +107 -0
  41. data/documentation/package.json +39 -0
  42. data/documentation/sidebars.js +15 -0
  43. data/documentation/src/components/HomepageFeatures.js +61 -0
  44. data/documentation/src/components/HomepageFeatures.module.css +13 -0
  45. data/documentation/src/css/custom.css +44 -0
  46. data/documentation/src/pages/index.js +50 -0
  47. data/documentation/src/pages/index.module.css +41 -0
  48. data/documentation/src/pages/markdown-page.md +7 -0
  49. data/documentation/static/.nojekyll +0 -0
  50. data/documentation/static/img/playwright-logo.svg +9 -0
  51. data/documentation/static/img/undraw_dropdown_menu.svg +1 -0
  52. data/documentation/static/img/undraw_web_development.svg +1 -0
  53. data/documentation/static/img/undraw_windows.svg +1 -0
  54. data/documentation/yarn.lock +8805 -0
  55. data/lib/playwright/channel_owners/binding_call.rb +33 -0
  56. data/lib/playwright/channel_owners/browser_context.rb +2 -2
  57. data/lib/playwright/channel_owners/element_handle.rb +2 -10
  58. data/lib/playwright/channel_owners/frame.rb +6 -28
  59. data/lib/playwright/channel_owners/js_handle.rb +2 -10
  60. data/lib/playwright/channel_owners/page.rb +10 -1
  61. data/lib/playwright/input_files.rb +0 -8
  62. data/lib/playwright/javascript.rb +0 -10
  63. data/lib/playwright/javascript/expression.rb +2 -7
  64. data/lib/playwright/version.rb +1 -1
  65. data/lib/playwright_api/accessibility.rb +7 -89
  66. data/lib/playwright_api/android.rb +7 -64
  67. data/lib/playwright_api/android_device.rb +8 -8
  68. data/lib/playwright_api/browser.rb +15 -169
  69. data/lib/playwright_api/browser_context.rb +47 -609
  70. data/lib/playwright_api/browser_type.rb +13 -103
  71. data/lib/playwright_api/cdp_session.rb +2 -25
  72. data/lib/playwright_api/console_message.rb +6 -6
  73. data/lib/playwright_api/dialog.rb +11 -92
  74. data/lib/playwright_api/element_handle.rb +60 -362
  75. data/lib/playwright_api/file_chooser.rb +0 -28
  76. data/lib/playwright_api/frame.rb +74 -713
  77. data/lib/playwright_api/js_handle.rb +16 -90
  78. data/lib/playwright_api/keyboard.rb +21 -213
  79. data/lib/playwright_api/mouse.rb +1 -45
  80. data/lib/playwright_api/page.rb +155 -1635
  81. data/lib/playwright_api/playwright.rb +14 -117
  82. data/lib/playwright_api/request.rb +15 -121
  83. data/lib/playwright_api/response.rb +9 -9
  84. data/lib/playwright_api/route.rb +8 -105
  85. data/lib/playwright_api/selectors.rb +6 -97
  86. data/lib/playwright_api/tracing.rb +1 -61
  87. data/lib/playwright_api/web_socket.rb +1 -1
  88. data/lib/playwright_api/worker.rb +6 -42
  89. metadata +55 -4
  90. data/lib/playwright/javascript/function.rb +0 -67
@@ -0,0 +1,8 @@
1
+ ---
2
+ sidebar_position: 10
3
+ ---
4
+
5
+ # Touchscreen
6
+
7
+ The Touchscreen class operates in main-frame CSS pixels relative to the top-left corner of the viewport. Methods on the
8
+ touchscreen can only be used in browser contexts that have been initialized with `hasTouch` set to true.
@@ -0,0 +1,54 @@
1
+ ---
2
+ sidebar_position: 10
3
+ ---
4
+
5
+ # Tracing
6
+
7
+ API for collecting and saving Playwright traces. Playwright traces can be opened using the Playwright CLI after
8
+ Playwright script runs.
9
+
10
+ Start with specifying the folder traces will be stored in:
11
+
12
+ ```python sync title=example_b375e389cd6685ec49d1ef57f3186da60ef682785c646fe8db351b6f39b1a34c.py
13
+ browser = chromium.launch(traceDir='traces')
14
+ context = browser.new_context()
15
+ context.tracing.start(name="trace", screenshots=True, snapshots=True)
16
+ page.goto("https://playwright.dev")
17
+ context.tracing.stop()
18
+ context.tracing.export("trace.zip")
19
+
20
+ ```
21
+
22
+
23
+ ## export
24
+
25
+ ```
26
+ def export(path)
27
+ ```
28
+
29
+ Export trace into the file with the given name. Should be called after the tracing has stopped.
30
+
31
+ ## start
32
+
33
+ ```
34
+ def start(name: nil, screenshots: nil, snapshots: nil)
35
+ ```
36
+
37
+ Start tracing.
38
+
39
+ ```python sync title=example_4c72a858b35ec7bd7aaba231cb93acecb7ee4b7ea8048a534f28f7e16af966b8.py
40
+ context.tracing.start(name="trace", screenshots=True, snapshots=True)
41
+ page.goto("https://playwright.dev")
42
+ context.tracing.stop()
43
+ context.tracing.export("trace.zip")
44
+
45
+ ```
46
+
47
+
48
+ ## stop
49
+
50
+ ```
51
+ def stop
52
+ ```
53
+
54
+ Stop tracing.
@@ -0,0 +1,7 @@
1
+ ---
2
+ sidebar_position: 10
3
+ ---
4
+
5
+ # WebSocket
6
+
7
+ Not Implemented
@@ -0,0 +1,7 @@
1
+ ---
2
+ sidebar_position: 10
3
+ ---
4
+
5
+ # Worker
6
+
7
+ Not Implemented
@@ -0,0 +1,11 @@
1
+ ---
2
+ position: 20
3
+ ---
4
+
5
+ # API Coverages
6
+
7
+ `playwright-ruby-client` doesn't cover all of the Playwright APIs. The APIs listed below without strikethrough are available in `playwright-ruby-client`.
8
+
9
+ import ApiCoverage from '../include/api_coverage.md'
10
+
11
+ <ApiCoverage />
@@ -0,0 +1,152 @@
1
+ ---
2
+ sidebar_position: 0
3
+ ---
4
+
5
+ # Getting started
6
+
7
+ `playwright-ruby-client` doesn't include Playwright driver nor its downloader. **We have to install Playwright in advance**
8
+
9
+ ```shell
10
+ $ npx playwright install
11
+ ```
12
+
13
+ and then set `playwright_cli_executable_path: "npx playwright"` into `Playwright.create`.
14
+
15
+ ## Enjoy with examples
16
+
17
+ ### Capture a site
18
+
19
+ Navigate pages with `page.goto(url)` and save the screenshot with `page.screenshot(path: './image.png')`.
20
+
21
+ ```rb {6-7}
22
+ require 'playwright'
23
+
24
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
25
+ playwright.chromium.launch(headless: false) do |browser|
26
+ page = browser.new_page
27
+ page.goto('https://github.com/YusukeIwaki')
28
+ page.screenshot(path: './YusukeIwaki.png')
29
+ end
30
+ end
31
+ ```
32
+
33
+ ![image](https://user-images.githubusercontent.com/11763113/104339718-412f9180-553b-11eb-9116-908e1e4b5186.gif)
34
+
35
+ ### Simple scraping
36
+
37
+ Extract data from a site.
38
+
39
+ * Grab DOM elements with `page.query_selector(loc)` and `page.query_selector_all(loc)`
40
+ * Extract data with `elem.evaluate(js)` or `page.eval_on_selector(js)`
41
+ * Wait for navigation with `page.expect_navigation do ... end`
42
+
43
+ ```rb {12-14,17-21}
44
+ require 'playwright'
45
+
46
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
47
+ playwright.chromium.launch(headless: false) do |browser|
48
+ page = browser.new_page
49
+ page.goto('https://github.com/')
50
+
51
+ form = page.query_selector("form.js-site-search-form")
52
+ search_input = form.query_selector("input.header-search-input")
53
+ search_input.click
54
+ page.keyboard.type("playwright")
55
+ page.expect_navigation {
56
+ page.keyboard.press("Enter")
57
+ }
58
+
59
+ list = page.query_selector("ul.repo-list")
60
+ items = list.query_selector_all("div.f4")
61
+ items.each do |item|
62
+ title = item.eval_on_selector("a", "a => a.innerText")
63
+ puts("==> #{title}")
64
+ end
65
+ end
66
+ end
67
+ ```
68
+
69
+ ```
70
+ $ bundle exec ruby main.rb
71
+ ==> microsoft/playwright
72
+ ==> microsoft/playwright-python
73
+ ==> microsoft/playwright-cli
74
+ ==> checkly/headless-recorder
75
+ ==> microsoft/playwright-sharp
76
+ ==> playwright-community/jest-playwright
77
+ ==> microsoft/playwright-test
78
+ ==> mxschmitt/playwright-go
79
+ ==> microsoft/playwright-java
80
+ ==> MarketSquare/robotframework-browser
81
+ ```
82
+
83
+ ### Android browser automation
84
+
85
+ As an experimental feature, we can automate Chrome for Android.
86
+
87
+ ```rb
88
+ require 'playwright'
89
+
90
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
91
+ devices = playwright.android.devices
92
+ unless devices.empty?
93
+ device = devices.last
94
+ begin
95
+ puts "Model: #{device.model}"
96
+ puts "Serial: #{device.serial}"
97
+ puts device.shell('ls /system')
98
+
99
+ device.launch_browser do |context|
100
+ page = context.pages.first
101
+ page.goto('https://github.com/YusukeIwaki')
102
+ page.click('header button')
103
+ page.click('input[name="q"]')
104
+ page.keyboard.type('puppeteer')
105
+ page.expect_navigation {
106
+ page.keyboard.press('Enter')
107
+ }
108
+ page.screenshot(path: 'YusukeIwaki.android.png')
109
+ end
110
+ ensure
111
+ device.close
112
+ end
113
+ end
114
+ end
115
+ ```
116
+
117
+ ![image](https://user-images.githubusercontent.com/11763113/106615177-8467a800-65af-11eb-94d9-c56e71487e78.gif)
118
+
119
+ ### Android native automation
120
+
121
+ We have to download android-driver for Playwright in advance.
122
+
123
+ ```shell
124
+ $ wget https://github.com/microsoft/playwright/raw/master/bin/android-driver-target.apk -O /path/to/playwright-driver/package/bin/android-driver-target.apk
125
+ $ wget https://github.com/microsoft/playwright/raw/master/bin/android-driver.apk -O /path/to/playwright-driver/package/bin/android-driver.apk
126
+ ```
127
+
128
+ (If you downloaded Playwright via npm, replace /path/to/playwright-driver/package/ with ./node_modules/playwright/ above.)
129
+
130
+ ```rb
131
+ require 'playwright'
132
+
133
+ Playwright.create(playwright_cli_executable_path: ENV['PLAYWRIGHT_CLI_EXECUTABLE_PATH']) do |playwright|
134
+ devices = playwright.android.devices
135
+ unless devices.empty?
136
+ device = devices.last
137
+ begin
138
+ device.shell('input keyevent POWER')
139
+ device.shell('input keyevent POWER')
140
+ device.shell('input keyevent 82')
141
+ sleep 1
142
+ device.shell('cmd statusbar expand-notifications')
143
+
144
+ # pp device.tree
145
+ # pp device.info(res: 'com.android.systemui:id/clock')
146
+ device.tap_on(res: 'com.android.systemui:id/clock')
147
+ ensure
148
+ device.close
149
+ end
150
+ end
151
+ end
152
+ ```
@@ -0,0 +1,3 @@
1
+ label: Guides
2
+ position: 10
3
+ collapsed: false
@@ -0,0 +1,49 @@
1
+ ---
2
+ sidebar_position: 1
3
+ ---
4
+
5
+ # Download Playwright driver
6
+
7
+ `playwright-ruby-client` doesn't include Playwright driver nor its downloader. **We have to install Playwright in advance**, and set the proper CLI path like `Playwright.create(playwright_cli_executable_path: /path/to/playwright)`
8
+
9
+ Choose any of the three ways as you prefer to download the driver:
10
+
11
+ * `npx`: suitable for playground use, and not suitable for continuous usage.
12
+ * `npm install`: the best choice for most use cases, with existing Node.js environment.
13
+ * Direct download: maybe a good choice for Docker :whale: integration.
14
+
15
+ ## Using `npx`
16
+
17
+ ```shell
18
+ $ npx playwright install
19
+ ```
20
+
21
+ and then set `playwright_cli_executable_path: "npx playwright"` at `Playwright.create`.
22
+
23
+
24
+ ## Using `npm install`
25
+
26
+ Actually `npx playwright` is a bit slow. We can also use `npm install` to setup.
27
+
28
+ Instead of `npx playwright install`:
29
+
30
+ ```shell
31
+ $ export PLAYWRIGHT_CLI_VERSION=$(bundle exec ruby -e 'puts Playwright::COMPATIBLE_PLAYWRIGHT_VERSION.strip')
32
+ $ npm install playwright@$PLAYWRIGHT_CLI_VERSION || npm install playwright@next
33
+ $ ./node_modules/.bin/playwright install
34
+ ```
35
+
36
+ and then set `playwright_cli_executable_path: './node_modules/.bin/playwright'`
37
+
38
+
39
+ ## Directly download driver without Node.js installation.
40
+
41
+ Instead of npm, you can also directly download playwright driver from playwright.azureedge.net. (The URL can be easily detected from [here](https://github.com/microsoft/playwright-python/blob/cfc1030a69d1e934cac579687a680eac53d4b9ee/setup.py#L75))
42
+
43
+
44
+ ```shell
45
+ $ export PLAYWRIGHT_CLI_VERSION=$(bundle exec ruby -e 'puts Playwright::COMPATIBLE_PLAYWRIGHT_VERSION.strip')
46
+ $ wget https://playwright.azureedge.net/builds/driver/playwright-$PLAYWRIGHT_CLI_VERSION-linux.zip
47
+ ```
48
+
49
+ and then extract it, and set `playwright_cli_executable_path: '/path/to/playwright-1.11.0-linux/playwright.sh'`
@@ -0,0 +1,119 @@
1
+ ---
2
+ sidebar_position: 2
3
+ ---
4
+
5
+ # Launch Browser
6
+
7
+ ![image](https://user-images.githubusercontent.com/11763113/118982444-68e5a900-b9b6-11eb-92a8-3e8fcbe36186.png)
8
+
9
+ ## Create Playwright session
10
+
11
+ In oder to launch browser, it is required to create Playwright session.
12
+
13
+ In previous examples,
14
+
15
+ ```rb
16
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
17
+ # Play with `playwright` here
18
+ end
19
+ ```
20
+
21
+ this is the exact procedure for creating Playwright session. Choose either of method for creating the session.
22
+
23
+ ### Define scoped Playwright session with block
24
+
25
+ ```rb
26
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
27
+ # Play with `playwright` here
28
+ end
29
+ ```
30
+
31
+ As is described repetedly, this is the recommended way for creating Playwright session. Even when any exception happens, Playwright session is safely ended on leaving the block.
32
+
33
+ Internally `playwright run-driver` session is alive during the block.
34
+
35
+ ### Define start/end of the Playwright session separately without block.
36
+
37
+ Sometimes we have to define separated start/end definition. `playwright-ruby-client` also allows it.
38
+
39
+ ```rb
40
+ class SomeClass
41
+
42
+ def start_playwright
43
+ # Start Playwright driver (runs `playwright run-driver` internally)
44
+ @playwright_exec = Playwright.create(playwright_cli_executable_path: 'npx playwright')
45
+ end
46
+
47
+ def stop_playwright!
48
+ # Stop Playwright driver
49
+ @playwright_exec.stop
50
+ end
51
+
52
+ def play_with_playwright(&blk)
53
+ # Acquire Playwright instance with PlaywrightExecution#playwright
54
+ playwright = @playwright_exec.playwright
55
+
56
+ browser = playwright.chromium.launch
57
+
58
+ begin
59
+ blk.call(browser)
60
+ ensure
61
+ browser.close
62
+ end
63
+ end
64
+ end
65
+ ```
66
+
67
+ Note that we have to make sure to call `PlaywrightExecution#stop` (`stop_playwright!` in this case).
68
+
69
+ ## Create browser instance
70
+
71
+ Playwright allows launching several browsers.
72
+
73
+ * `playwright.chromium.launch` for launching Chrome, Microsoft Edge, Chromium
74
+ * `playwright.firefox.launch` for launching (modified version of) Firefox
75
+ * `playwright.webkit.launch` for launching (modified version of) WebKit/Safari
76
+
77
+ It is recommended in most cases to launch browser with block, which automatically closes the launched browser on end.
78
+
79
+ ```rb
80
+ # scoped browser block
81
+ playwright.chromium.launch do |browser|
82
+ # Play with `browser`
83
+ end
84
+ ```
85
+
86
+ Of course, you can manually call `Browser#close` when the browser is launched without block.
87
+
88
+ ```rb
89
+ # separated start/end
90
+ browser = playwright.chromium.launch
91
+
92
+ begin
93
+ # Play with `@browser`
94
+ ensure
95
+ browser.close
96
+ end
97
+ ```
98
+
99
+ ## Open new window and new tab
100
+
101
+ Use `Browser#new_context` to prepare a new browser window and use `BrowserContext#new_page` to create a new tab.
102
+ Also we can use `Browser#new_page` to create a new window and new tab at once.
103
+
104
+ ```rb
105
+ Playwright.create(playwright_cli_executable_path: 'npx playwright') do |playwright|
106
+ playwright.chromium.launch(headless: false) do |browser| # Chromium task icon appers in.
107
+ context = browser.new_context # Prepare new window.
108
+ page = context.new_page # Open new window and new tab here. (about:blank)
109
+ page.goto('https://example.com') # Navigate to a site.
110
+
111
+ page2 = context.new_page # Open another tab here.
112
+ page2.goto('https://example.com') # Navigate to a site.
113
+
114
+ another_context = browser.new_context # Prepare another window here.
115
+ another_page = another_context.new_page # Open a new window and new tab.
116
+ another_page.goto('https://example.com/') # Navigate to a site.
117
+ end
118
+ end
119
+ ```
@@ -0,0 +1,51 @@
1
+ ---
2
+ sidebar_position: 3
3
+ ---
4
+
5
+ # Integration into Ruby on Rails application
6
+
7
+ `playwright-ruby-client` is a client library just for browser automation. `capybara-playwright-driver` provides the [Capybara](https://github.com/teamcapybara/capybara) driver based on playwright-ruby-client and makes it easy to integrate into Ruby on Rails applications.
8
+
9
+ ## Installation
10
+
11
+ Add the line below into Gemfile:
12
+
13
+ ```rb
14
+ gem 'capybara-playwright-driver'
15
+ ```
16
+
17
+ and then `bundle install`.
18
+
19
+ Note that capybara-playwright-driver does not depend on Selenium. But `selenium-webdriver` is also required [on Rails 5.x, 6.0](https://github.com/rails/rails/pull/39179)
20
+
21
+ ## Register and configure Capybara driver
22
+
23
+ ```rb
24
+ Capybara.register_driver(:playwright) do |app|
25
+ Capybara::Playwright::Driver.new(app,
26
+ browser_type: :chromium, # :chromium (default) or :firefox, :webkit
27
+ headless: false, # true for headless mode (default), false for headful mode.
28
+ )
29
+ end
30
+ ```
31
+
32
+ ### Update timeout
33
+
34
+ Capybara sets the default value of timeout to *2 seconds*. Generally it is too short to wait for HTTP responses.
35
+
36
+ It is recommended to set the timeout to 15-30 seconds for Playwright driver.
37
+
38
+ ```rb
39
+ Capybara.default_max_wait_time = 15
40
+ ```
41
+
42
+ ### (Optional) Update default driver
43
+
44
+ By default, Capybara driver is set to `:rack_test`, which works only with non-JS contents. If your Rails application has many JavaScript contents, it is recommended to change the default driver to `:playwrite`.
45
+
46
+ ```rb
47
+ Capybara.default_driver = :playwright
48
+ Capybara.javascript_driver = :playwright
49
+ ```
50
+
51
+ It is not mandatry. Without changing the default driver, you can still use Playwright driver by specifying `Capybara.current_driver = :playwrite` (or `driven_by :playwright` in system spec) explicitly.