如何正常使用水豚和硒 class

how to use capybara and selenium in normal class

我们的应用程序在某笔交易后需要登录第三方系统并输入一些数据。 用 Capybara 和 selenium 进行了系统测试,效果很好,但是那是 运行 在测试中使用它的时候。 我想创建一个 class/service ,它将 运行 在 ActiveJob 中做同样的事情,所以在保存交易后我想调用 ActiveJob.perform_later(params) 和 params应该传递给 'external_system_integration' class 和 运行 headless 以登录第三方站点并输入参数中收到的数据。

如何操作?

我的Class

require 'capybara/dsl'
require 'capybara/rspec'
require "selenium/webdriver"

class OrderDeskService
    Capybara.default_driver = :webkit
    include Capybara::DSL


    def self.login
        Capybara.register_driver :chrome do |app|
            profile = Selenium::WebDriver::Chrome::Profile.new
            profile["download.default_directory"] = DOWNLOAD_PATH
            Capybara::Selenium::Driver.new(app, :browser => :chrome, :profile => profile)
        end

        Capybara.configure do |config|
            config.run_server = false
            config.app_host = 'https://app.orderdesk.me'
        end
        visit '/login'
        sleep 10
    end
end

反馈后编辑:


Capybara.configure do |c|
    c.run_server = false
    c.app_host = 'https://app.orderdesk.me'
end

#Configure
Capybara.register_driver :selenium do |app|
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile["download.default_directory"] = DOWNLOAD_PATH
    Capybara::Selenium::Driver.new(app, :browser => :chrome, :profile => profile)
end
#headless
Capybara.register_driver :headless_chrome do |app|
    capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
        chromeOptions: {
            args: %w[headless disable-gpu enable-features=NetworkService,NetworkServiceInProcess]
        }
    )
    Capybara::Selenium::Driver.new app,
                                                                 browser: :chrome,
                                                                 desired_capabilities: capabilities
end



#make it thread safe
Capybara.threadsafe = true

Capybara.default_driver = Capybara.javascript_driver = :headless_chrome


class OrderDeskService
    include Capybara::DSL

    def self.login
        session = Capybara::Session.new(:selenium)
        session.visit '/login'
        sleep 3
        session.quit
    end
end

正在尝试 运行 无头模式,但它仍会打开 Chrome 浏览器。

绝对可以在 class 中使用 Capybara,但是按照您尝试的方式(调用 Capybaras class 方法)会与您的系统测试发生冲突,并给您带来各种问题头痛。相反,您想在 class 中使用手动会话管理,并在该会话中调用水豚方法。

Capybara.register_driver :my_driver do |app|
  # Only needs to be done once
  ...
end

class OrderDeskService
  def self.login
    session = Capybara::Session.new(:my_driver)
    session.visit('https://app.orderdesk.me/login')
    ,,,
    session.quit
  end
end

如果您希望会话的持续时间超过一次方法调用,请将其保存在 class 变量中并在需要的时间调用退出。如果您希望多个会话能够同时处于活动状态,请改用实例 variables/methods。更高级的是启用 Capybaras "threadsafe" 模式,这使得大多数配置选项 session/thread 特定

Capybara.register_driver :my_driver do |app|
  # Only needs to be done once
  ...
end

Capybara.threadsafe = true

class OrderDeskService
  def self.login
    session = Capybara::Session.new(:my_driver) do |c|
      c.app_host = 'https://app.orderdesk.me'
      c.... # any other Capybara session configs that need to apply to this session
    end
    session.visit('/login')
    ,,,
    session.quit
  end
end