Class: Capybara::Simulated::Driver::FakePlaywrightPage

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/simulated/driver.rb

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ FakePlaywrightPage

Returns a new instance of FakePlaywrightPage.



152
153
154
155
156
157
158
159
160
# File 'lib/capybara/simulated/driver.rb', line 152

def initialize(browser) = (@browser = browser)
# Playwright's `page.evaluate` takes either a string expression
# or a function literal — when given a function it calls it
# and returns the result. The simulated driver's underlying
# `evaluate_script` just runs the source as an expression, so
# a function-literal payload would return the function object
# instead of its return value. Wrap arrow-function-shaped
# bodies in `(...)()` so the function is invoked and the test
# sees its result.

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing {|_self| ... } ⇒ Object

Yield to the block when one is given so Playwright methods whose semantics live entirely in their block (the canonical case is pw_page.expect_download { click_link "…" }expect_download arms a download watcher, then runs the block, then awaits the watcher). Returning self from a block-taking method-missing would skip the block entirely and the download never triggers. Pass the receiver in as the block argument so chained |d| d.suggested_filename readers see a no-op object.

Yields:

  • (_self)

Yield Parameters:



183
184
185
186
# File 'lib/capybara/simulated/driver.rb', line 183

def method_missing(*)
  yield self if block_given?
  self
end

Instance Method Details

#evaluate(js) ⇒ Object

Playwright's page.evaluate takes either a string expression or a function literal — when given a function it calls it and returns the result. The simulated driver's underlying evaluate_script just runs the source as an expression, so a function-literal payload would return the function object instead of its return value. Wrap arrow-function-shaped bodies in (...)() so the function is invoked and the test sees its result.



161
162
163
164
165
# File 'lib/capybara/simulated/driver.rb', line 161

def evaluate(js, *)
  src = js.to_s.strip
  src = "(#{src})()" if src.match?(/\A(\(?\s*(async\s+)?(\(.*?\)|\w+)\s*=>|\(?\s*(async\s+)?function\s*\*?\s*\()/m)
  @browser.evaluate_script(src)
end

#locator(selector) ⇒ Object

pw_page.locator(selector) returns a Locator that proxies click / fill / count / etc. through Capybara's current session. Discourse's SelectKit / system_helpers locator method drives the suspend / silence / penalize / dropdown chains via pw_page.locator(...).click — without a real locator the click is a no-op and the modal never advances.



172
# File 'lib/capybara/simulated/driver.rb', line 172

def locator(selector) = FakePlaywrightLocator.new(selector)

#respond_to_missing?Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
# File 'lib/capybara/simulated/driver.rb', line 173

def respond_to_missing?(*) = true
# Yield to the block when one is given so Playwright methods
# whose semantics live entirely in their block (the canonical
# case is `pw_page.expect_download { click_link "…" }` —
# `expect_download` arms a download watcher, *then* runs the
# block, *then* awaits the watcher). Returning `self` from a
# block-taking method-missing would skip the block entirely
# and the download never triggers. Pass the receiver in as
# the block argument so chained `|d| d.suggested_filename`
# readers see a no-op object.