Module: Playwright

Defined in:
lib/playwright.rb,
lib/playwright.rb,
lib/playwright/test.rb,
lib/playwright/utils.rb,
lib/playwright/video.rb,
lib/playwright/errors.rb,
lib/playwright/events.rb,
lib/playwright/waiter.rb,
lib/playwright/channel.rb,
lib/playwright/version.rb,
lib/playwright_api/page.rb,
lib/playwright/transport.rb,
lib/playwright_api/clock.rb,
lib/playwright_api/frame.rb,
lib/playwright_api/mouse.rb,
lib/playwright_api/route.rb,
lib/playwright/clock_impl.rb,
lib/playwright/connection.rb,
lib/playwright/har_router.rb,
lib/playwright/mouse_impl.rb,
lib/playwright_api/dialog.rb,
lib/playwright_api/worker.rb,
lib/playwright/input_files.rb,
lib/playwright/raw_headers.rb,
lib/playwright/url_matcher.rb,
lib/playwright_api/android.rb,
lib/playwright_api/browser.rb,
lib/playwright_api/locator.rb,
lib/playwright_api/request.rb,
lib/playwright_api/tracing.rb,
lib/playwright/http_headers.rb,
lib/playwright/locator_impl.rb,
lib/playwright_api/download.rb,
lib/playwright_api/keyboard.rb,
lib/playwright_api/response.rb,
lib/playwright/channel_owner.rb,
lib/playwright/download_impl.rb,
lib/playwright/event_emitter.rb,
lib/playwright/keyboard_impl.rb,
lib/playwright/locator_utils.rb,
lib/playwright/route_handler.rb,
lib/playwright_api/js_handle.rb,
lib/playwright_api/selectors.rb,
lib/playwright/playwright_api.rb,
lib/playwright_api/playwright.rb,
lib/playwright_api/web_socket.rb,
lib/playwright_api/api_request.rb,
lib/playwright_api/cdp_session.rb,
lib/playwright_api/touchscreen.rb,
lib/playwright/javascript/regex.rb,
lib/playwright/timeout_settings.rb,
lib/playwright/touchscreen_impl.rb,
lib/playwright_api/api_response.rb,
lib/playwright_api/browser_type.rb,
lib/playwright_api/file_chooser.rb,
lib/playwright/api_response_impl.rb,
lib/playwright/file_chooser_impl.rb,
lib/playwright/web_socket_client.rb,
lib/playwright_api/accessibility.rb,
lib/playwright_api/android_input.rb,
lib/playwright_api/frame_locator.rb,
lib/playwright/accessibility_impl.rb,
lib/playwright/android_input_impl.rb,
lib/playwright/api_implementation.rb,
lib/playwright/frame_locator_impl.rb,
lib/playwright_api/android_device.rb,
lib/playwright_api/android_socket.rb,
lib/playwright_api/element_handle.rb,
lib/playwright/channel_owners/page.rb,
lib/playwright/event_emitter_proxy.rb,
lib/playwright_api/browser_context.rb,
lib/playwright_api/console_message.rb,
lib/playwright_api/page_assertions.rb,
lib/playwright/channel_owners/frame.rb,
lib/playwright/channel_owners/route.rb,
lib/playwright/console_message_impl.rb,
lib/playwright/page_assertions_impl.rb,
lib/playwright/select_option_values.rb,
lib/playwright/web_socket_transport.rb,
lib/playwright_api/android_web_view.rb,
lib/playwright/channel_owners/dialog.rb,
lib/playwright/channel_owners/stream.rb,
lib/playwright/channel_owners/worker.rb,
lib/playwright/javascript/expression.rb,
lib/playwright/javascript/source_url.rb,
lib/playwright/channel_owners/android.rb,
lib/playwright/channel_owners/browser.rb,
lib/playwright/channel_owners/request.rb,
lib/playwright/channel_owners/tracing.rb,
lib/playwright_api/locator_assertions.rb,
lib/playwright/channel_owners/artifact.rb,
lib/playwright/channel_owners/electron.rb,
lib/playwright/channel_owners/response.rb,
lib/playwright/javascript/value_parser.rb,
lib/playwright/javascript/visitor_info.rb,
lib/playwright/locator_assertions_impl.rb,
lib/playwright_api/api_request_context.rb,
lib/playwright/channel_owners/js_handle.rb,
lib/playwright/channel_owners/selectors.rb,
lib/playwright/channel_owners/playwright.rb,
lib/playwright/channel_owners/web_socket.rb,
lib/playwright/channel_owners/cdp_session.rb,
lib/playwright/channel_owners/local_utils.rb,
lib/playwright/channel_owners/binding_call.rb,
lib/playwright/channel_owners/browser_type.rb,
lib/playwright/javascript/value_serializer.rb,
lib/playwright/channel_owners/fetch_request.rb,
lib/playwright/channel_owners/android_device.rb,
lib/playwright/channel_owners/element_handle.rb,
lib/playwright/channel_owners/browser_context.rb,
lib/playwright/channel_owners/writable_stream.rb,
lib/playwright/channel_owners/api_request_context.rb

Overview

Defined Under Namespace

Modules: ApiImplementation, ChannelOwners, EventEmitter, EventListenerInterface, Events, JavaScript, LocatorUtils, Test, Utils Classes: APIRequest, APIRequestContext, APIResponse, Accessibility, Android, AndroidDevice, AndroidExecution, AndroidInput, AndroidSocket, AndroidWebView, AssertionError, Browser, BrowserContext, BrowserType, CDPSession, Channel, ChannelOwner, Clock, Connection, ConsoleMessage, Dialog, Download, DriverCrashedError, ElementHandle, Error, EventEmitterCallback, EventEmitterOnceCallback, EventEmitterProxy, Execution, FileChooser, Frame, FrameLocator, HarRouter, HttpHeaders, InputFiles, JSHandle, Keyboard, Locator, LocatorAssertions, Mouse, Page, PageAssertions, Playwright, PlaywrightApi, RawHeaders, Request, Response, RootChannelOwner, Route, RouteHandler, SelectOptionValues, Selectors, TargetClosedError, TimeoutError, TimeoutSettings, Touchscreen, Tracing, Transport, UrlMatcher, Video, Waiter, WebError, WebSocket, WebSocketClient, WebSocketTransport, Worker

Constant Summary collapse

VERSION =
'1.47.0'
COMPATIBLE_PLAYWRIGHT_VERSION =
'1.47.1'

Class Method Summary collapse

Class Method Details

.connect_to_android_server(ws_endpoint, &block) ⇒ Object

Playwright.connect_to_android_server(‘ws://.…’) do |browser|

page = browser.new_page
...

end



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/playwright.rb', line 190

module_function def connect_to_android_server(ws_endpoint, &block)
  require 'playwright/web_socket_client'
  require 'playwright/web_socket_transport'

  transport = WebSocketTransport.new(ws_endpoint: ws_endpoint)
  connection = Connection.new(transport)
  connection.mark_as_remote
  connection.async_run

  execution =
    begin
      playwright = connection.initialize_playwright
      android_device = playwright.send(:pre_connected_android_device)
      android_device.should_close_connection_on_close!
      AndroidExecution.new(connection, PlaywrightApi.wrap(playwright), PlaywrightApi.wrap(android_device))
    rescue
      connection.stop
      raise
    end

  if block
    begin
      block.call(execution.device)
    ensure
      execution.stop
    end
  else
    execution
  end
end

.connect_to_browser_server(ws_endpoint, &block) ⇒ Object

Connects to Playwright server, launched by ‘npx playwright launch-server chromium` or `playwright.chromium.launchServer()`

Playwright.connect_to_browser_server(‘ws://.…’) do |browser|

page = browser.new_page
...

end



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/playwright.rb', line 149

module_function def connect_to_browser_server(ws_endpoint, &block)
  require 'playwright/web_socket_client'
  require 'playwright/web_socket_transport'

  transport = WebSocketTransport.new(ws_endpoint: ws_endpoint)
  connection = Connection.new(transport)
  connection.mark_as_remote
  connection.async_run

  execution =
    begin
      playwright = connection.initialize_playwright
      browser = playwright.send(:pre_launched_browser)
      browser.send(:update_browser_type, playwright.chromium) # Just workaround for nil reference error.
      browser.browser_type.send(:did_launch_browser, browser)
      browser.send(:should_close_connection_on_close!)
      Execution.new(connection, PlaywrightApi.wrap(playwright), PlaywrightApi.wrap(browser))
    rescue
      connection.stop
      raise
    end

  if block
    begin
      block.call(execution.browser)
    ensure
      execution.stop
    end
  else
    execution
  end
end

.connect_to_playwright_server(ws_endpoint, &block) ⇒ Object

Connects to Playwright server, launched by ‘npx playwright run-server` via WebSocket transport.

Playwright.connect_to_playwright_server(…) do |playwright|

browser = playwright.chromium.launch
...

end



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/playwright.rb', line 113

module_function def connect_to_playwright_server(ws_endpoint, &block)
  require 'playwright/web_socket_client'
  require 'playwright/web_socket_transport'

  transport = WebSocketTransport.new(ws_endpoint: ws_endpoint)
  connection = Connection.new(transport)
  connection.async_run

  execution =
    begin
      playwright = connection.initialize_playwright
      Execution.new(connection, PlaywrightApi.wrap(playwright))
    rescue
      connection.stop
      raise
    end

  if block
    begin
      block.call(execution.playwright)
    ensure
      execution.stop
    end
  else
    execution
  end
end

.create(playwright_cli_executable_path:, &block) ⇒ Object

Recommended to call this method with block.

Playwright.create(…) do |playwright|

browser = playwright.chromium.launch
...

end

When we use this method without block, an instance of Playwright::Execution is returned and we must call execution.stop on the end. The instance of playwright is available by calling execution.playwright



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/playwright.rb', line 80

module_function def create(playwright_cli_executable_path:, &block)
  transport = Transport.new(playwright_cli_executable_path: playwright_cli_executable_path)
  connection = Connection.new(transport)
  connection.async_run

  execution =
    begin
      playwright = connection.initialize_playwright
      Execution.new(connection, PlaywrightApi.wrap(playwright))
    rescue
      connection.stop
      raise
    end

  if block
    begin
      block.call(execution.playwright)
    ensure
      execution.stop
    end
  else
    execution
  end
end

.define_api_implementation(class_name, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/playwright/api_implementation.rb', line 6

def self.define_api_implementation(class_name, &block)
  klass = Class.new
  klass.include(ApiImplementation)
  klass.class_eval(&block) if block
  if ::Playwright.const_defined?(class_name)
    raise ArgumentError.new("Playwright::#{class_name} already exist. Choose another class name.")
  end
  ::Playwright.const_set(class_name, klass)
end

.define_channel_owner(class_name, &block) ⇒ Object



149
150
151
152
153
# File 'lib/playwright/channel_owner.rb', line 149

def self.define_channel_owner(class_name, &block)
  klass = Class.new(ChannelOwner)
  klass.class_eval(&block) if block
  ChannelOwners.const_set(class_name, klass)
end