Class: Capybara::Playwright::Browser
- Inherits:
-
Object
- Object
- Capybara::Playwright::Browser
show all
- Extended by:
- Forwardable
- Includes:
- TmpdirOwner
- Defined in:
- lib/capybara/playwright/browser.rb
Overview
Responsibility of this class is:
Note that this class doesn’t manage Playwright::Browser. We should not use Playwright::Browser#close in this class.
Defined Under Namespace
Classes: NoSuchWindowError
Instance Method Summary
collapse
-
#accept_modal(dialog_type, **options, &block) ⇒ Object
-
#active_element ⇒ Object
-
#clear_browser_contexts ⇒ 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(driver:, playwright_browser:, page_options:, record_video: false) ⇒ Browser
constructor
A new instance of Browser.
-
#maximize_window(handle) ⇒ Object
-
#open_new_window(kind = :tab) ⇒ Object
-
#raw_screenshot(**options) ⇒ Object
Not used by Capybara::Session.
-
#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
-
#video_path ⇒ Object
Not used by Capybara::Session.
-
#visit(path) ⇒ Object
-
#window_handles ⇒ Object
-
#window_size(handle) ⇒ Object
-
#with_playwright_page(&block) ⇒ Object
#remove_tmpdir, #tmpdir
Constructor Details
#initialize(driver:, playwright_browser:, page_options:, record_video: false) ⇒ Browser
Returns a new instance of Browser.
17
18
19
20
21
22
23
24
25
|
# File 'lib/capybara/playwright/browser.rb', line 17
def initialize(driver:, playwright_browser:, page_options:, record_video: false)
@driver = driver
@playwright_browser = playwright_browser
@page_options = page_options
if record_video
@page_options[:record_video_dir] ||= tmpdir
end
@playwright_page = create_page(create_browser_context)
end
|
Instance Method Details
#accept_modal(dialog_type, **options, &block) ⇒ Object
328
329
330
331
332
|
# File 'lib/capybara/playwright/browser.rb', line 328
def accept_modal(dialog_type, **options, &block)
assert_page_alive {
@playwright_page.capybara_accept_modal(dialog_type, **options, &block)
}
end
|
#active_element ⇒ Object
168
169
170
171
172
173
174
175
|
# File 'lib/capybara/playwright/browser.rb', line 168
def active_element
el = @playwright_page.capybara_current_frame.evaluate_handle('() => document.activeElement')
if el
Node.new(@driver, @playwright_page, el)
else
nil
end
end
|
#clear_browser_contexts ⇒ Object
47
48
49
|
# File 'lib/capybara/playwright/browser.rb', line 47
def clear_browser_contexts
@playwright_browser.contexts.each(&:close)
end
|
#close_window(handle) ⇒ Object
287
288
289
290
291
292
293
294
295
|
# File 'lib/capybara/playwright/browser.rb', line 287
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
51
52
53
54
55
|
# File 'lib/capybara/playwright/browser.rb', line 51
def current_url
assert_page_alive {
@playwright_page.url
}
end
|
#current_window_handle ⇒ Object
255
256
257
|
# File 'lib/capybara/playwright/browser.rb', line 255
def current_window_handle
@playwright_page&.guid
end
|
#dismiss_modal(dialog_type, **options, &block) ⇒ Object
334
335
336
337
338
|
# File 'lib/capybara/playwright/browser.rb', line 334
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/capybara/playwright/browser.rb', line 152
def evaluate_async_script(script, *args)
assert_page_alive {
js = <<~JAVASCRIPT
function(_arguments){
let args = Array.prototype.slice.call(_arguments);
return new Promise((resolve, reject) => {
args.push(resolve);
(function(){ #{script} }).apply(this, args);
});
}
JAVASCRIPT
result = @playwright_page.capybara_current_frame.evaluate_handle(js, arg: unwrap_node(args))
wrap_node(result)
}
end
|
#evaluate_script(script, *args) ⇒ Object
145
146
147
148
149
150
|
# File 'lib/capybara/playwright/browser.rb', line 145
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
138
139
140
141
142
143
|
# File 'lib/capybara/playwright/browser.rb', line 138
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
86
87
88
89
90
91
92
|
# File 'lib/capybara/playwright/browser.rb', line 86
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
78
79
80
81
82
83
84
|
# File 'lib/capybara/playwright/browser.rb', line 78
def find_xpath(query, **options)
assert_page_alive {
@playwright_page.capybara_current_frame.query_selector_all("xpath=#{query}").map do |el|
Node.new(@driver, @playwright_page, el)
end
}
end
|
#fullscreen_window(handle) ⇒ Object
319
320
321
322
323
324
325
326
|
# File 'lib/capybara/playwright/browser.rb', line 319
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
126
127
128
129
130
|
# File 'lib/capybara/playwright/browser.rb', line 126
def go_back
assert_page_alive {
@playwright_page.go_back
}
end
|
#go_forward ⇒ Object
132
133
134
135
136
|
# File 'lib/capybara/playwright/browser.rb', line 132
def go_forward
assert_page_alive {
@playwright_page.go_forward
}
end
|
#html ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/capybara/playwright/browser.rb', line 106
def html
assert_page_alive {
js = <<~JAVASCRIPT
() => {
let html = '';
if (document.doctype) html += new XMLSerializer().serializeToString(document.doctype);
if (document.documentElement) html += document.documentElement.outerHTML;
return html;
}
JAVASCRIPT
@playwright_page.capybara_current_frame.evaluate(js)
}
end
|
#maximize_window(handle) ⇒ Object
309
310
311
312
313
314
315
316
317
|
# File 'lib/capybara/playwright/browser.rb', line 309
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
259
260
261
262
263
264
265
266
267
268
|
# File 'lib/capybara/playwright/browser.rb', line 259
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
|
#raw_screenshot(**options) ⇒ Object
Not used by Capybara::Session. Intended to be directly called by user.
187
188
189
190
191
|
# File 'lib/capybara/playwright/browser.rb', line 187
def raw_screenshot(**options)
return nil if !@playwright_page || @playwright_page.closed?
@playwright_page.screenshot(**options)
end
|
#refresh ⇒ Object
72
73
74
75
76
|
# File 'lib/capybara/playwright/browser.rb', line 72
def refresh
assert_page_alive {
@playwright_page.capybara_current_frame.evaluate('() => { location.reload(true) }')
}
end
|
#resize_window_to(handle, width, height) ⇒ Object
303
304
305
306
307
|
# File 'lib/capybara/playwright/browser.rb', line 303
def resize_window_to(handle, width, height)
on_window(handle) do |page|
page.viewport_size = { width: width, height: height }
end
end
|
94
95
96
97
98
|
# File 'lib/capybara/playwright/browser.rb', line 94
def
assert_page_alive {
@playwright_page.
}
end
|
#save_screenshot(path, **options) ⇒ Object
193
194
195
196
197
|
# File 'lib/capybara/playwright/browser.rb', line 193
def save_screenshot(path, **options)
assert_page_alive {
@playwright_page.screenshot(path: path)
}
end
|
#send_keys(*args) ⇒ Object
199
200
201
|
# File 'lib/capybara/playwright/browser.rb', line 199
def send_keys(*args)
Node::SendKeys.new(@playwright_page.keyboard, args).execute
end
|
#status_code ⇒ Object
100
101
102
103
104
|
# File 'lib/capybara/playwright/browser.rb', line 100
def status_code
assert_page_alive {
@playwright_page.capybara_status_code
}
end
|
#switch_to_frame(frame) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/capybara/playwright/browser.rb', line 203
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
279
280
281
282
283
284
285
|
# File 'lib/capybara/playwright/browser.rb', line 279
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
120
121
122
123
124
|
# File 'lib/capybara/playwright/browser.rb', line 120
def title
assert_page_alive {
@playwright_page.title
}
end
|
#video_path ⇒ Object
Not used by Capybara::Session. Intended to be directly called by user.
179
180
181
182
183
|
# File 'lib/capybara/playwright/browser.rb', line 179
def video_path
return nil if !@playwright_page || @playwright_page.closed?
@playwright_page.video&.path
end
|
#visit(path) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/capybara/playwright/browser.rb', line 57
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
251
252
253
|
# File 'lib/capybara/playwright/browser.rb', line 251
def window_handles
pages.map(&:guid)
end
|
#window_size(handle) ⇒ Object
297
298
299
300
301
|
# File 'lib/capybara/playwright/browser.rb', line 297
def window_size(handle)
on_window(handle) do |page|
page.evaluate('() => [window.innerWidth, window.innerHeight]')
end
end
|
#with_playwright_page(&block) ⇒ Object
386
387
388
389
390
|
# File 'lib/capybara/playwright/browser.rb', line 386
def with_playwright_page(&block)
assert_page_alive {
block.call(@playwright_page)
}
end
|