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 = @context.new_page end |
Instance Method Details
#all(selector, **options) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/e2e/drivers/playwright.rb', line 41 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
87 88 89 |
# File 'lib/e2e/drivers/playwright.rb', line 87 def attach_file(selector, path) @page.set_input_files(selector, path) end |
#body ⇒ Object
91 92 93 |
# File 'lib/e2e/drivers/playwright.rb', line 91 def body @page.content end |
#check(selector) ⇒ Object
79 80 81 |
# File 'lib/e2e/drivers/playwright.rb', line 79 def check(selector) @page.check(selector) end |
#click(selector) ⇒ Object
49 50 51 |
# File 'lib/e2e/drivers/playwright.rb', line 49 def click(selector) @page.click(selector) end |
#click_button(value, **options) ⇒ Object
53 54 55 |
# File 'lib/e2e/drivers/playwright.rb', line 53 def (value, **) @page.get_by_role("button", name: value, **).click end |
#click_link(value, **options) ⇒ Object
57 58 59 |
# File 'lib/e2e/drivers/playwright.rb', line 57 def click_link(value, **) @page.get_by_role("link", name: value, **).click end |
#current_url ⇒ Object
29 30 31 |
# File 'lib/e2e/drivers/playwright.rb', line 29 def current_url @page.url end |
#evaluate(script) ⇒ Object
100 101 102 |
# File 'lib/e2e/drivers/playwright.rb', line 100 def evaluate(script) @page.evaluate(script) end |
#fill_in(selector, with:) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/e2e/drivers/playwright.rb', line 61 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 begin 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
33 34 35 36 37 38 39 |
# File 'lib/e2e/drivers/playwright.rb', line 33 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
108 109 110 |
# File 'lib/e2e/drivers/playwright.rb', line 108 def native @page end |
#pause ⇒ Object
112 113 114 115 116 |
# File 'lib/e2e/drivers/playwright.rb', line 112 def pause raise E2E::Error, "pause is not available in headless mode" if @headless @page.pause end |
#quit ⇒ Object
124 125 126 127 |
# File 'lib/e2e/drivers/playwright.rb', line 124 def quit @browser.close @playwright_execution.stop end |
#reset! ⇒ Object
118 119 120 121 122 |
# File 'lib/e2e/drivers/playwright.rb', line 118 def reset! @context.close @context = @browser.new_context @page = @context.new_page end |
#save_screenshot(path, **options) ⇒ Object
104 105 106 |
# File 'lib/e2e/drivers/playwright.rb', line 104 def save_screenshot(path, **) @page.screenshot(path: path, **) end |
#text ⇒ Object
Required for have_content matcher on page object
96 97 98 |
# File 'lib/e2e/drivers/playwright.rb', line 96 def text @page.inner_text("body") end |
#uncheck(selector) ⇒ Object
83 84 85 |
# File 'lib/e2e/drivers/playwright.rb', line 83 def uncheck(selector) @page.uncheck(selector) end |
#visit(url) ⇒ Object
25 26 27 |
# File 'lib/e2e/drivers/playwright.rb', line 25 def visit(url) @page.goto(url) end |