Class: E2E::Drivers::Playwright

Inherits:
E2E::Driver show all
Defined in:
lib/e2e/drivers/playwright.rb

Instance Method Summary collapse

Constructor Details

#initializePlaywright

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, **options)
  if options.key?(:text)
    text = options.delete(:text)
    selector = "#{selector}:has-text('#{text}')"
  end
  @page.locator(selector, **options).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

#bodyObject



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 click_button(value, **options)
  with_released_connection { @page.get_by_role("button", name: value, **options).click }
end


63
64
65
# File 'lib/e2e/drivers/playwright.rb', line 63

def click_link(value, **options)
  with_released_connection { @page.get_by_role("link", name: value, **options).click }
end

#current_urlObject



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, **options)
  if options.key?(:text)
    text = options.delete(:text)
    selector = "#{selector}:has-text('#{text}')"
  end
  E2E::Element.new(@page.locator(selector, **options).first)
end

#nativeObject



114
115
116
# File 'lib/e2e/drivers/playwright.rb', line 114

def native
  @page
end

#pauseObject

Raises:



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

#quitObject



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, **options)
  @page.screenshot(path: path, **options)
end

#textObject

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

#visit(url) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/e2e/drivers/playwright.rb', line 25

def visit(url)
  with_released_connection do
    @page.goto(
      url,
      waitUntil: E2E.config.navigation_wait_until,
      timeout: navigation_timeout_ms
    )
  end
end