playwright-ruby-client icon indicating copy to clipboard operation
playwright-ruby-client copied to clipboard

Capybara always wants Selenium

Open chmich opened this issue 4 months ago • 0 comments

Usecase / What to achieve

Because this seems to me that it is more related to Capybara, but related to your docs, i post it as question here:

I want to use Playwright without Capybara DSL like described in docs within a rails project.

gems: capybara (3.40.0), rspec-rails (6.0.1), playwright-ruby-client (1.47.0)

configs / playwright_helper.rb:

require 'rails_helper' 

RSpec.configure do |config|

  ...

  # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  # configs from https://playwright-ruby-client.vercel.app/docs/article/guides/rails_integration_with_null_driver
  # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  require 'capybara'

  class CapybaraNullDriver < Capybara::Driver::Base
    def needs_server?
      true
    end
  end

  Capybara.register_driver(:null) { CapybaraNullDriver.new }

  config.around(driver: :null) do |example|
    Capybara.current_driver = :null
    Capybara.default_max_wait_time = 1

    # Rails server is launched here, at the first time of accessing Capybara.current_session.server
    base_url = Capybara.current_session.server.base_url

    Playwright.create(playwright_cli_executable_path: './node_modules/.bin/playwright') do |playwright|
      # pass any option for Playwright#launch and Browser#new_page as you prefer.
      playwright.chromium.launch(headless: false) do |browser|

        @playwright_page = p = browser.new_page(baseURL: base_url)
        example.run

      end
    end
  end
end

And the test is this

require 'playwright_helper'

RSpec.describe "pw", type: :system do
  describe 'example', driver: :null do

    let(:page) { @playwright_page }

    it 'can browse' do
      page.goto("/")
    end
  end
end

What i have done

I made this setup first in a newly created test-project: There a little dummy-test succeeded, but when i add a error, for provoking a failing, in the logs appears the line

undefined method `save_screenshot' for #<Playwright::Page:0x000000010688b808 impl=#<page@a18b54bc230bc516814e1fd2346046af>>

So far, so good, not a big problem, with that i can live.

Problem

But, then, i integrated the same configuration in a existing app, set up a little test that should sourely pass, and got this output:

/bin/zsh -c "bash -c '/Users/christian/.rvm/bin/rvm ruby-3.2.2@ad-playwright do /Users/christian/.rvm/rubies/ruby-3.2.2/bin/ruby -x /Users/christian/projects/Finitia/arc_digital/bin/bundle exec /Users/christian/.rvm/rubies/ruby-3.2.2/bin/ruby /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/bin/rspec /Users/christian/projects/Finitia/arc_digital/spec/system/playwright_projects_spec.rb --require teamcity/spec/runner/formatter/teamcity/formatter --format Spec::Runner::Formatter::TeamcityFormatter --example '\"'\"'pw example can browse'\"'\"''"
Testing started at 11:52 ...
Run options: include {:full_description=>/pw\ example\ can\ browse/}

Randomized with seed 45554

RSpec::Core::MultipleExceptionError: cannot load such file -- selenium/webdriver

undefined method `save_screenshot' for #<Playwright::Page:0x000000011c3fb278 @impl=#<page@d131ed63e4da7c8c551f38cabb9e4b45>, @capybara_all_responses={}, @capybara_last_response=nil, @capybara_frames=[], @event_emitter_proxy=#<Playwright::EventEmitterProxy:0x000000011c3fafd0 @api=#<Playwright::Page:0x000000011c3fb278 ...>, @impl=#<page@d131ed63e4da7c8c551f38cabb9e4b45>, @listeners={"dialog"=>#<Proc:0x000000011c3fad50 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "download"=>#<Proc:0x000000011c3f8cf8 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "response"=>#<Proc:0x000000011c3f87a8 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "framenavigated"=>#<Proc:0x000000011c3f6098 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "load"=>#<Proc:0x000000011c3f5c38 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>}, @__event_emitter={"dialog"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f8ff0 @proc=#<Proc:0x000000011c3fb098 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:20 (lambda)>>}>, "download"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f8960 @proc=#<Proc:0x000000011c3f8f28 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:23 (lambda)>>}>, "response"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f63e0 @proc=#<Proc:0x000000011c3f88c0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:29 (lambda)>>}>, "framenavigated"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f5e90 @proc=#<Proc:0x000000011c3f6250 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:32 (lambda)>>}>, "load"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f5a80 @proc=#<Proc:0x000000011c3f5da0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:35 (lambda)>>}>}, @__event_emitter_callback={"dialog/74300"=>#<Playwright::EventEmitterCallback:0x000000011c3f8ff0 @proc=#<Proc:0x000000011c3fb098 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:20 (lambda)>>, "download/74380"=>#<Playwright::EventEmitterCallback:0x000000011c3f8960 @proc=#<Proc:0x000000011c3f8f28 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:23 (lambda)>>, "response/74460"=>#<Playwright::EventEmitterCallback:0x000000011c3f63e0 @proc=#<Proc:0x000000011c3f88c0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:29 (lambda)>>, "framenavigated/74540"=>#<Playwright::EventEmitterCallback:0x000000011c3f5e90 @proc=#<Proc:0x000000011c3f6250 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:32 (lambda)>>, "load/74620"=>#<Playwright::EventEmitterCallback:0x000000011c3f5a80 @proc=#<Proc:0x000000011c3f5da0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:35 (lambda)>>}>>

  0) pw example can browse
     Got 0 failures and 2 other errors:

     0.1) Failure/Error: example.run

          LoadError:
            cannot load such file -- selenium/webdriver
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/bootsnap-1.16.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/zeitwerk-2.6.8/lib/zeitwerk/kernel.rb:38:in `require'
          # ./spec/playwright_helper.rb:39:in `block (4 levels) in <top (required)>'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/playwright_api.rb:121:in `block in wrap_block_call'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/channel_owners/browser_type.rb:20:in `launch'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright_api/browser_type.rb:109:in `launch'
          # ./spec/playwright_helper.rb:36:in `block (3 levels) in <top (required)>'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright.rb:96:in `create'
          # ./spec/playwright_helper.rb:34:in `block (2 levels) in <top (required)>'

     0.2) Failure/Error: example.run

          NoMethodError:
            undefined method `save_screenshot' for #<Playwright::Page:0x000000011c3fb278 @impl=#<page@d131ed63e4da7c8c551f38cabb9e4b45>, @capybara_all_responses={}, @capybara_last_response=nil, @capybara_frames=[], @event_emitter_proxy=#<Playwright::EventEmitterProxy:0x000000011c3fafd0 @api=#<Playwright::Page:0x000000011c3fb278 ...>, @impl=#<page@d131ed63e4da7c8c551f38cabb9e4b45>, @listeners={"dialog"=>#<Proc:0x000000011c3fad50 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "download"=>#<Proc:0x000000011c3f8cf8 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "response"=>#<Proc:0x000000011c3f87a8 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "framenavigated"=>#<Proc:0x000000011c3f6098 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>, "load"=>#<Proc:0x000000011c3f5c38 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/event_emitter_proxy.rb:35 (lambda)>}, @__event_emitter={"dialog"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f8ff0 @proc=#<Proc:0x000000011c3fb098 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:20 (lambda)>>}>, "download"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f8960 @proc=#<Proc:0x000000011c3f8f28 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:23 (lambda)>>}>, "response"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f63e0 @proc=#<Proc:0x000000011c3f88c0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:29 (lambda)>>}>, "framenavigated"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f5e90 @proc=#<Proc:0x000000011c3f6250 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:32 (lambda)>>}>, "load"=>#<Set: {#<Playwright::EventEmitterCallback:0x000000011c3f5a80 @proc=#<Proc:0x000000011c3f5da0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:35 (lambda)>>}>}, @__event_emitter_callback={"dialog/74300"=>#<Playwright::EventEmitterCallback:0x000000011c3f8ff0 @proc=#<Proc:0x000000011c3fb098 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:20 (lambda)>>, "download/74380"=>#<Playwright::EventEmitterCallback:0x000000011c3f8960 @proc=#<Proc:0x000000011c3f8f28 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:23 (lambda)>>, "response/74460"=>#<Playwright::EventEmitterCallback:0x000000011c3f63e0 @proc=#<Proc:0x000000011c3f88c0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:29 (lambda)>>, "framenavigated/74540"=>#<Playwright::EventEmitterCallback:0x000000011c3f5e90 @proc=#<Proc:0x000000011c3f6250 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:32 (lambda)>>, "load/74620"=>#<Playwright::EventEmitterCallback:0x000000011c3f5a80 @proc=#<Proc:0x000000011c3f5da0 /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/capybara-playwright-driver-0.5.2/lib/capybara/playwright/page.rb:35 (lambda)>>}>>
          # ./spec/playwright_helper.rb:39:in `block (4 levels) in <top (required)>'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/playwright_api.rb:121:in `block in wrap_block_call'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright/channel_owners/browser_type.rb:20:in `launch'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright_api/browser_type.rb:109:in `launch'
          # ./spec/playwright_helper.rb:36:in `block (3 levels) in <top (required)>'
          # /Users/christian/.rvm/gems/ruby-3.2.2@ad-playwright/gems/playwright-ruby-client-1.47.0/lib/playwright.rb:96:in `create'
          # ./spec/playwright_helper.rb:34:in `block (2 levels) in <top (required)>'
1 example, 1 failure, 0 passed
Finished in 1.611605 seconds

Randomized with seed 45554

Process finished with exit code 1

What i have done/2

I searched and searched through the whole application, especially the rails_helper, and disabled all lines that might refer to Capybara and/or require Selenium.

I deleted and re-installed my entire gemset and the node_modules folder to be sure that there are no old remnants were left behind.

Beside the Capybara.current_driver, i tried to set the .default_driver and/or the .javascript_driver to :null.

Regarding to the Capybara docs switching away from Selenium is looking easy and should be done with the code above.

All this has no effect, Capybara always wants to load Selenium and my test is always marked as failed, even though the test steps pass.

When i, then, add the gem 'selenium-webdriver', the test is passing.

Any suggestions?

Best Regards, Chris

chmich avatar Oct 26 '24 10:10 chmich