Ruby-水豚录制测试执行视频

时间:2019-03-08 02:01:08

标签: ruby automated-tests capybara

我正在尝试使用Capybara录制并以视频格式保存我的执行测试。我的测试通过了,但是我的Mac终端出现了几个错误。另外,视频也不会被记录/保存。

SPEC_HELPER.RB

require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'selenium-webdriver'
require 'headless'

headless = Headless.new
headless.start

RSpec.configure do |config|

  config.expect_with :rspec do |expectations|
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  config.mock_with :rspec do |mocks|
    mocks.verify_partial_doubles = true
  end

  config.shared_context_metadata_behavior = :apply_to_host_groups
  config.include Capybara::DSL

  config.before(:example) do
    headless.video.start_capture
    page.current_window.resize_to(1200, 800)
  end

  config.after(:example) do |e, scenario|
      name = e.description.gsub(/[^A-Za-z0-9 ]/, '').tr(' ', '_')
      d = Time.now.strftime("%F-%T")
      page.save_screenshot('log/' + name + '-' + d.to_s + '.png')

      headless.video.stop_and_save('log/' + name + '-' + d.to_s + '.mov')
  end

end

Capybara.configure do |config|
  config.default_driver = :selenium_chrome_headless
  config.run_server = false
end

Capybara.default_max_wait_time = 10

MAC终端错误

在19.68秒内完成(文件花费0.82221秒来加载) 4个例子,0个失败

Traceback (most recent call last):
    8: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/capybara-3.14.0/lib/capybara/selenium/driver.rb:374:in `block in setup_exit_handler'
    7: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/capybara-3.14.0/lib/capybara/selenium/driver.rb:208:in `quit'
    6: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/chrome/driver.rb:62:in `quit'
    5: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:81:in `stop'
    4: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:122:in `stop_process'
    3: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:12:in `stop'
    2: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:61:in `send_term'
    1: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:72:in `send_signal'
/Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:72:in `kill': Operation not permitted (Errno::EPERM)
Traceback (most recent call last):
    8: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/capybara-3.14.0/lib/capybara/selenium/driver.rb:374:in `block in setup_exit_handler'
    7: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/capybara-3.14.0/lib/capybara/selenium/driver.rb:208:in `quit'
    6: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/chrome/driver.rb:62:in `quit'
    5: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:81:in `stop'
    4: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:122:in `stop_process'
    3: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:12:in `stop'
    2: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:61:in `send_term'
    1: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:72:in `send_signal'
/Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:72:in `kill': Operation not permitted (Errno::EPERM)
    12: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/platform.rb:141:in `block in exit_hook'
    11: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:67:in `block in start'
    10: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:77:in `stop'
     9: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:128:in `stop_server'
     8: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:104:in `connect_to_server'
     7: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/net/http.rb:605:in `start'
     6: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/net/http.rb:919:in `start'
     5: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/net/http.rb:930:in `do_start'
     4: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/net/http.rb:945:in `connect'
     3: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/timeout.rb:103:in `timeout'
     2: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/timeout.rb:93:in `block in timeout'
     1: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/net/http.rb:946:in `block in connect'
/Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/2.6.0/net/http.rb:949:in `rescue in block in connect': Failed to open TCP connection to 127.0.0.1:9515 (Connection refused - connect(2) for "127.0.0.1" port 9515) (Errno::ECONNREFUSED)
    8: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/platform.rb:141:in `block in exit_hook'
    7: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:67:in `block in start'
    6: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:81:in `stop'
    5: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:81:in `ensure in stop'
    4: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.141.0/lib/selenium/webdriver/common/service.rb:122:in `stop_process'
    3: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:12:in `stop'
    2: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:61:in `send_term'
    1: from /Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:72:in `send_signal'
/Users/dsanders/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/childprocess-0.9.0/lib/childprocess/unix/process.rb:72:in `kill': Operation not permitted (Errno::EPERM)

2 个答案:

答案 0 :(得分:0)

尝试替换这些行

page.save_screenshot('log/' + name + '-' + d.to_s + '.png')
headless.video.stop_and_save('log/' + name + '-' + d.to_s + '.mov')

使用

#please note for this ../ from this file to root just reach to root of the project. in my case root 

root_path = File.join(File.dirname(__FILE__), "../")

page.save_screenshot(File.join(root_path,'log/' + name + '-' + d.to_s + '.png'))
headless.video.stop_and_save(File.join(root_path,'log/' + name + '-' + d.to_s + '.mov'))

答案 1 :(得分:0)

我的搜索显示Mac与Xvfb不兼容,这是Headless所使用的。即使您通过了rails错误,也可能不会通过此错误。