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 = @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, **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



87
88
89
# File 'lib/e2e/drivers/playwright.rb', line 87

def attach_file(selector, path)
  @page.set_input_files(selector, path)
end

#bodyObject



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


57
58
59
# File 'lib/e2e/drivers/playwright.rb', line 57

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

#current_urlObject



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, **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



108
109
110
# File 'lib/e2e/drivers/playwright.rb', line 108

def native
  @page
end

#pauseObject

Raises:



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

#quitObject



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

#textObject

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