无头模式在 cucumber 水豚Ruby自动化

kx7yvsdv  于 12个月前  发布在  Ruby
关注(0)|答案(2)|浏览(122)

我看到很多类似的问题,但没有一个能帮助我理解。如何使用chromedrive、selenium、capybara、ruby和cucumber在无头模式下运行我的.feature文件。这是我的env.rb:require_relative 'helper.rb'

BROWSER = ENV['BROWSER']

World(Helper)

Capybara.register_driver :selenium do |app|

  if BROWSER.eql?('chrome_headless')

    Capybara::Selenium::Driver.new(app,
        :browser => :chrome,
        :desired_capabilities => Selenium::WebDriver::Remote::Capabilities.chrome(
          'chromeOptions' => {
            'args' => [ "headless", "window-size=1440x768", "disable-gpu"]
          }
        )
      )
    elsif BROWSER.eql?('chrome')
        Capybara::Selenium::Driver.new(app,browser: :chrome)
        
    end
  end

Capybara.configure do |config|
    config.default_driver =:selenium
   Capybara.page.driver.browser.manage.window.resize_to(1440,768)
end

Capybara.default_max_wait_time = 60

这是我的助手。RB:

module Helper
 def take picture(file_name, res)
     file_path = "reports/screenshot/"
     dateTime = DateTime.now.to_s
     dateTime.split(':')
     date = dataHora[0..12].to_s+dataHora[14..15].to_s+dataHora[17..21].to_s
     picture = "#{file_path}#{date}#{nome_arquivo}-#{res}.png"
     temp_shot = page.save_screenshot(picture)
     shot = Base64.encode64(File.open(temp_shot, "rb").read)
     attach(shot, 'image/png')
 end
end

这是我的step_definition文件.rb只是一个普通的rb文件包含水豚元素

adm = LoginAdmin.new
 admInic = TelaInicialAdmin.new

   varGlobal = YAML.load(File.read('./configuracoesGlobaisTeste.yaml'))
    @nomeOferta = varGlobal["nomeOferta"]
     @nomeProduto = varGlobal["nomeProduto"]

       Dado('que acessei a página da Vivo') do
            acesso.load
             end

   Quando('clicar em Entrar posso digitar minhas credenciais ') do 
        page.driver.browser.navigate.refresh
        sleep 3
        inicio.btnEntrar.click
        inicio.usuario.set @usuario
        inicio.senha.set @senha
        inicio.btnAcessarConta.click
     end

是不是少了什么?这是我如何使用我的功能文件中的标记在终端上运行自动化:

cucumber -t@criarClienteCompraLojaClone

此命令使用GUI打开浏览器。我不想打开GUI。我想做一个无头测试。我刚刚尝试了stackoverflow和谷歌的每一个教程。请任何建议将帮助!

pgpifvop

pgpifvop1#

我的无头工程更新的env.rb文件为:

require_relative 'helper.rb'

BROWSER = ENV['BROWSER']

#HEADLESS
World(Helper)

Capybara.register_driver :headless_chrome  do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome,
    options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless no-sandbox disable-gpu]))
end

Capybara.configure do |config|
config.default_driver =:headless_chrome
#Capybara.page.driver.browser.manage.window.resize_to(1440,768)
end

Capybara.default_max_wait_time = 60
kyxcudwk

kyxcudwk2#

我的env.rb文件只包含以下几行Capybara配置调整:

Capybara.default_driver = :selenium_chrome_headless
Capybara.javascript_driver = :selenium_chrome_headless

相关问题