Class: E2E::Drivers::Playwright
- Inherits:
-
E2E::Driver
- Object
- E2E::Driver
- E2E::Drivers::Playwright
- Defined in:
- lib/e2e/drivers/playwright.rb
Instance Method Summary collapse
- #all(selector, **options) ⇒ Object
- #attach_file(selector, path) ⇒ Object
- #body ⇒ Object
- #check(selector) ⇒ Object
- #click(selector) ⇒ Object
- #click_button(value, **options) ⇒ Object
- #click_link(value, **options) ⇒ Object
- #current_url ⇒ Object
- #evaluate(script) ⇒ Object
- #fill_in(selector, with:) ⇒ Object
- #find(selector, **options) ⇒ Object
-
#initialize ⇒ Playwright
constructor
A new instance of Playwright.
- #native ⇒ Object
- #pause ⇒ Object
- #quit ⇒ Object
- #reset! ⇒ Object
- #save_screenshot(path, **options) ⇒ Object
-
#text ⇒ Object
Required for have_content matcher on page object.
- #uncheck(selector) ⇒ Object
- #visit(url) ⇒ Object
Constructor Details
#initialize ⇒ Playwright
Returns a new instance of Playwright.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/e2e/drivers/playwright.rb', line 8 def initialize @playwright_execution = ::Playwright.create(playwright_cli_executable_path: "npx playwright") @playwright = @playwright_execution.playwright browser_type_name = E2E.config.browser_type || :chromium @browser_type = @playwright.send(browser_type_name) @headless = E2E.config.headless @browser = @browser_type.launch(headless: @headless) @context = @browser.new_context # Enable debug console to allow page.pause (requires GUI, so only in headed mode) @context.enable_debug_console! unless @headless @page = build_page end |
Instance Method Details
#all(selector, **options) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/e2e/drivers/playwright.rb', line 47 def all(selector, **) if .key?(:text) text = .delete(:text) selector = "#{selector}:has-text('#{text}')" end @page.locator(selector, **).all.map { |l| E2E::Element.new(l) } end |
#attach_file(selector, path) ⇒ Object
93 94 95 |
# File 'lib/e2e/drivers/playwright.rb', line 93 def attach_file(selector, path) with_released_connection { @page.set_input_files(selector, path) } end |
#body ⇒ Object
97 98 99 |
# File 'lib/e2e/drivers/playwright.rb', line 97 def body @page.content end |
#check(selector) ⇒ Object
85 86 87 |
# File 'lib/e2e/drivers/playwright.rb', line 85 def check(selector) with_released_connection { @page.check(selector) } end |
#click(selector) ⇒ Object
55 56 57 |
# File 'lib/e2e/drivers/playwright.rb', line 55 def click(selector) with_released_connection { @page.click(selector) } end |
#click_button(value, **options) ⇒ Object
59 60 61 |
# File 'lib/e2e/drivers/playwright.rb', line 59 def (value, **) with_released_connection { @page.get_by_role("button", name: value, **).click } end |
#click_link(value, **options) ⇒ Object
63 64 65 |
# File 'lib/e2e/drivers/playwright.rb', line 63 def click_link(value, **) with_released_connection { @page.get_by_role("link", name: value, **).click } end |
#current_url ⇒ Object
35 36 37 |
# File 'lib/e2e/drivers/playwright.rb', line 35 def current_url @page.url end |
#evaluate(script) ⇒ Object
106 107 108 |
# File 'lib/e2e/drivers/playwright.rb', line 106 def evaluate(script) @page.evaluate(script) end |
#fill_in(selector, with:) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/e2e/drivers/playwright.rb', line 67 def fill_in(selector, with:) # Try to match Capybara's behavior: Label, Placeholder, ID, Name chain = @page.get_by_label(selector).or(@page.get_by_placeholder(selector)) # Only add ID/Name matching if the selector doesn't contain spaces (valid CSS ID/Name assumption-ish) if selector.match?(/^[a-zA-Z0-9_-]+$/) chain = chain.or(@page.locator("##{selector}")) .or(@page.locator("[name='#{selector}']")) end with_released_connection do chain.first.fill(with) rescue # Fallback to treating it as a direct CSS selector if the above failed @page.fill(selector, with) end end |
#find(selector, **options) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/e2e/drivers/playwright.rb', line 39 def find(selector, **) if .key?(:text) text = .delete(:text) selector = "#{selector}:has-text('#{text}')" end E2E::Element.new(@page.locator(selector, **).first) end |
#native ⇒ Object
114 115 116 |
# File 'lib/e2e/drivers/playwright.rb', line 114 def native @page end |
#pause ⇒ Object
118 119 120 121 122 |
# File 'lib/e2e/drivers/playwright.rb', line 118 def pause raise E2E::Error, "pause is not available in headless mode" if @headless @page.pause end |
#quit ⇒ Object
131 132 133 134 |
# File 'lib/e2e/drivers/playwright.rb', line 131 def quit @browser.close @playwright_execution.stop end |
#reset! ⇒ Object
124 125 126 127 128 129 |
# File 'lib/e2e/drivers/playwright.rb', line 124 def reset! @context.close @context = @browser.new_context @context.enable_debug_console! unless @headless @page = build_page end |
#save_screenshot(path, **options) ⇒ Object
110 111 112 |
# File 'lib/e2e/drivers/playwright.rb', line 110 def save_screenshot(path, **) @page.screenshot(path: path, **) end |
#text ⇒ Object
Required for have_content matcher on page object
102 103 104 |
# File 'lib/e2e/drivers/playwright.rb', line 102 def text @page.inner_text("body") end |
#uncheck(selector) ⇒ Object
89 90 91 |
# File 'lib/e2e/drivers/playwright.rb', line 89 def uncheck(selector) with_released_connection { @page.uncheck(selector) } end |