Class: Capybara::Playwright::Browser
- Inherits:
-
Object
- Object
- Capybara::Playwright::Browser
show all
- Extended by:
- Forwardable
- Defined in:
- lib/capybara/playwright/browser.rb
Defined Under Namespace
Classes: NoSuchWindowError
Instance Method Summary
collapse
-
#accept_modal(dialog_type, **options, &block) ⇒ Object
-
#close_window(handle) ⇒ Object
-
#current_url ⇒ Object
-
#current_window_handle ⇒ Object
-
#dismiss_modal(dialog_type, **options, &block) ⇒ Object
-
#evaluate_async_script(script, *args) ⇒ Object
-
#evaluate_script(script, *args) ⇒ Object
-
#execute_script(script, *args) ⇒ Object
-
#find_css(query, **options) ⇒ Object
-
#find_xpath(query, **options) ⇒ Object
-
#fullscreen_window(handle) ⇒ Object
-
#go_back ⇒ Object
-
#go_forward ⇒ Object
-
#html ⇒ Object
-
#initialize(playwright:, driver:, browser_type:, browser_options:, page_options:) ⇒ Browser
constructor
A new instance of Browser.
-
#maximize_window(handle) ⇒ Object
-
#open_new_window(kind = :tab) ⇒ Object
-
#quit ⇒ Object
-
#refresh ⇒ Object
-
#resize_window_to(handle, width, height) ⇒ Object
-
#response_headers ⇒ Object
-
#save_screenshot(path, **options) ⇒ Object
-
#send_keys(*args) ⇒ Object
-
#status_code ⇒ Object
-
#switch_to_frame(frame) ⇒ Object
-
#switch_to_window(handle) ⇒ Object
-
#title ⇒ Object
-
#visit(path) ⇒ Object
-
#window_handles ⇒ Object
-
#window_size(handle) ⇒ Object
-
#with_playwright_page(&block) ⇒ Object
Constructor Details
#initialize(playwright:, driver:, browser_type:, browser_options:, page_options:) ⇒ Browser
8
9
10
11
12
13
14
15
|
# File 'lib/capybara/playwright/browser.rb', line 8
def initialize(playwright:, driver:, browser_type:, browser_options:, page_options:)
@driver = driver
browser_type = playwright.send(browser_type)
@playwright_browser = browser_type.launch(**browser_options)
@page_options = page_options
@playwright_page = create_page(create_browser_context)
end
|
Instance Method Details
#accept_modal(dialog_type, **options, &block) ⇒ Object
270
271
272
273
274
|
# File 'lib/capybara/playwright/browser.rb', line 270
def accept_modal(dialog_type, **options, &block)
assert_page_alive
@playwright_page.capybara_accept_modal(dialog_type, **options, &block)
end
|
#close_window(handle) ⇒ Object
229
230
231
232
233
234
235
236
237
|
# File 'lib/capybara/playwright/browser.rb', line 229
def close_window(handle)
on_window(handle) do |page|
page.close
if @playwright_page&.guid == handle
@playwright_page = nil
end
end
end
|
#current_url ⇒ Object
41
42
43
44
45
|
# File 'lib/capybara/playwright/browser.rb', line 41
def current_url
assert_page_alive
@playwright_page.url
end
|
#current_window_handle ⇒ Object
197
198
199
|
# File 'lib/capybara/playwright/browser.rb', line 197
def current_window_handle
@playwright_page&.guid
end
|
#dismiss_modal(dialog_type, **options, &block) ⇒ Object
276
277
278
279
280
|
# File 'lib/capybara/playwright/browser.rb', line 276
def dismiss_modal(dialog_type, **options, &block)
assert_page_alive
@playwright_page.capybara_dismiss_modal(dialog_type, **options, &block)
end
|
#evaluate_async_script(script, *args) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/capybara/playwright/browser.rb', line 142
def evaluate_async_script(script, *args)
assert_page_alive
js = " function(_arguments){\n let args = Array.prototype.slice.call(_arguments);\n return new Promise((resolve, reject) => {\n args.push(resolve);\n (function(){ \#{script} }).apply(this, args);\n });\n }\n JAVASCRIPT\n result = @playwright_page.capybara_current_frame.evaluate_handle(js, arg: unwrap_node(args))\n wrap_node(result)\nend\n"
|
#evaluate_script(script, *args) ⇒ Object
135
136
137
138
139
140
|
# File 'lib/capybara/playwright/browser.rb', line 135
def evaluate_script(script, *args)
assert_page_alive
result = @playwright_page.capybara_current_frame.evaluate_handle("function (arguments) { return #{script} }", arg: unwrap_node(args))
wrap_node(result)
end
|
#execute_script(script, *args) ⇒ Object
128
129
130
131
132
133
|
# File 'lib/capybara/playwright/browser.rb', line 128
def execute_script(script, *args)
assert_page_alive
@playwright_page.capybara_current_frame.evaluate("function (arguments) { #{script} }", arg: unwrap_node(args))
nil
end
|
#find_css(query, **options) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/capybara/playwright/browser.rb', line 76
def find_css(query, **options)
assert_page_alive
@playwright_page.capybara_current_frame.query_selector_all(query).map do |el|
Node.new(@driver, @playwright_page, el)
end
end
|
#find_xpath(query, **options) ⇒ Object
68
69
70
71
72
73
74
|
# File 'lib/capybara/playwright/browser.rb', line 68
def find_xpath(query, **options)
assert_page_alive
@playwright_page.capybara_current_frame.query_selector_all("xpath=#{query}").map do |el|
Node.new(@driver, @puppeteer_page, el)
end
end
|
#fullscreen_window(handle) ⇒ Object
261
262
263
264
265
266
267
268
|
# File 'lib/capybara/playwright/browser.rb', line 261
def fullscreen_window(handle)
puts "[WARNING] fullscreen_window is not supported in Playwright driver"
on_window(handle) do |page|
page.evaluate('() => document.body.requestFullscreen()')
end
end
|
#go_back ⇒ Object
116
117
118
119
120
|
# File 'lib/capybara/playwright/browser.rb', line 116
def go_back
assert_page_alive
@playwright_page.go_back
end
|
#go_forward ⇒ Object
122
123
124
125
126
|
# File 'lib/capybara/playwright/browser.rb', line 122
def go_forward
assert_page_alive
@playwright_page.go_forward
end
|
#html ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/capybara/playwright/browser.rb', line 96
def html
assert_page_alive
js = " () => {\n let html = '';\n if (document.doctype) html += new XMLSerializer().serializeToString(document.doctype);\n if (document.documentElement) html += document.documentElement.outerHTML;\n return html;\n }\n JAVASCRIPT\n @playwright_page.capybara_current_frame.evaluate(js)\nend\n"
|
#maximize_window(handle) ⇒ Object
251
252
253
254
255
256
257
258
259
|
# File 'lib/capybara/playwright/browser.rb', line 251
def maximize_window(handle)
puts "[WARNING] maximize_window is not supported in Playwright driver"
on_window(handle) do |page|
screen_size = page.evaluate('() => ({ width: window.screen.width, height: window.screen.height})')
page.viewport_size = screen_size
end
end
|
#open_new_window(kind = :tab) ⇒ Object
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/capybara/playwright/browser.rb', line 201
def open_new_window(kind = :tab)
browser_context =
if kind == :tab
@playwright_page&.context || create_browser_context
else
create_browser_context
end
create_page(browser_context)
end
|
#quit ⇒ Object
37
38
39
|
# File 'lib/capybara/playwright/browser.rb', line 37
def quit
@playwright_browser.close
end
|
#refresh ⇒ Object
62
63
64
65
66
|
# File 'lib/capybara/playwright/browser.rb', line 62
def refresh
assert_page_alive
@playwright_page.capybara_current_frame.evaluate('() => { location.reload(true) }')
end
|
#resize_window_to(handle, width, height) ⇒ Object
245
246
247
248
249
|
# File 'lib/capybara/playwright/browser.rb', line 245
def resize_window_to(handle, width, height)
on_window(handle) do |page|
page.viewport_size = { width: width, height: height }
end
end
|
84
85
86
87
88
|
# File 'lib/capybara/playwright/browser.rb', line 84
def
assert_page_alive
@playwright_page.
end
|
#save_screenshot(path, **options) ⇒ Object
158
159
160
161
162
|
# File 'lib/capybara/playwright/browser.rb', line 158
def save_screenshot(path, **options)
assert_page_alive
@playwright_page.screenshot(path: path)
end
|
#send_keys(*args) ⇒ Object
164
165
166
|
# File 'lib/capybara/playwright/browser.rb', line 164
def send_keys(*args)
Node::SendKeys.new(@playwright_page.keyboard, args).execute
end
|
#status_code ⇒ Object
90
91
92
93
94
|
# File 'lib/capybara/playwright/browser.rb', line 90
def status_code
assert_page_alive
@playwright_page.capybara_status_code
end
|
#switch_to_frame(frame) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
# File 'lib/capybara/playwright/browser.rb', line 168
def switch_to_frame(frame)
assert_page_alive
case frame
when :top
@playwright_page.capybara_reset_frames
when :parent
@playwright_page.capybara_pop_frame
else
playwright_frame = frame.native.content_frame
raise ArgumentError.new("Not a frame element: #{frame}") unless playwright_frame
@playwright_page.capybara_push_frame(playwright_frame)
end
end
|
#switch_to_window(handle) ⇒ Object
221
222
223
224
225
226
227
|
# File 'lib/capybara/playwright/browser.rb', line 221
def switch_to_window(handle)
if @playwright_page&.guid != handle
on_window(handle) do |page|
@playwright_page = page.tap(&:bring_to_front)
end
end
end
|
#title ⇒ Object
110
111
112
113
114
|
# File 'lib/capybara/playwright/browser.rb', line 110
def title
assert_page_alive
@playwright_page.title
end
|
#visit(path) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/capybara/playwright/browser.rb', line 47
def visit(path)
assert_page_alive
url =
if Capybara.app_host
URI(Capybara.app_host).merge(path)
elsif Capybara.default_host
URI(Capybara.default_host).merge(path)
else
path
end
@playwright_page.capybara_current_frame.goto(url)
end
|
#window_handles ⇒ Object
193
194
195
|
# File 'lib/capybara/playwright/browser.rb', line 193
def window_handles
pages.map(&:guid)
end
|
#window_size(handle) ⇒ Object
239
240
241
242
243
|
# File 'lib/capybara/playwright/browser.rb', line 239
def window_size(handle)
on_window(handle) do |page|
page.evaluate('() => [window.innerWidth, window.innerHeight]')
end
end
|
#with_playwright_page(&block) ⇒ Object
311
312
313
314
315
316
|
# File 'lib/capybara/playwright/browser.rb', line 311
def with_playwright_page(&block)
assert_page_alive
raise ArgumentError.new('block must be given') unless block
block.call(@playwright_page)
end
|