Module: Playwright

Defined in:
lib/playwright.rb,
lib/playwright.rb,
lib/playwright/utils.rb,
lib/playwright/video.rb,
lib/playwright/errors.rb,
lib/playwright/events.rb,
lib/playwright/channel.rb,
lib/playwright/version.rb,
lib/playwright_api/page.rb,
lib/playwright/transport.rb,
lib/playwright_api/frame.rb,
lib/playwright_api/mouse.rb,
lib/playwright_api/route.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/wait_helper.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/channel_owners/frame.rb,
lib/playwright/channel_owners/route.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/channel_owners/android.rb,
lib/playwright/channel_owners/browser.rb,
lib/playwright/channel_owners/request.rb,
lib/playwright/channel_owners/tracing.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_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/console_message.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, Utils Classes: APIRequest, APIRequestContext, APIResponse, Accessibility, Android, AndroidDevice, AndroidInput, AndroidSocket, AndroidWebView, Browser, BrowserContext, BrowserType, CDPSession, Channel, ChannelOwner, Connection, ConsoleMessage, Dialog, Download, DriverCrashedError, ElementHandle, Error, EventEmitterCallback, EventEmitterOnceCallback, EventEmitterProxy, Execution, FileChooser, Frame, FrameLocator, HarRouter, HttpHeaders, InputFiles, JSHandle, Keyboard, Locator, Mouse, Page, Playwright, PlaywrightApi, RawHeaders, Request, Response, RootChannelOwner, Route, RouteHandler, SelectOptionValues, Selectors, TimeoutError, TimeoutSettings, Touchscreen, Tracing, Transport, UrlMatcher, Video, WaitHelper, WebSocket, WebSocketClient, WebSocketTransport, Worker

Constant Summary collapse

VERSION =
'1.28.0'
COMPATIBLE_PLAYWRIGHT_VERSION =
'1.28.1'

Class Method Summary collapse

Class Method Details

.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



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/playwright.rb', line 133

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.should_close_connection_on_close!
      browser.send(:update_browser_type, browser.browser_type)
      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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/playwright.rb', line 97

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 Puppeteer::Execution is returned and we must call execution.stop on the end. The instance of playwright is available by calling execution.playwright



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/playwright.rb', line 64

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



144
145
146
147
148
# File 'lib/playwright/channel_owner.rb', line 144

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