如何将我的Selenium操作记录到example.metadata [:extra_failure_lines]?

时间:2016-03-30 20:57:50

标签: ruby logging selenium-webdriver rspec capybara

RSpec 3.4.0有example.metadata[:extra_failure_lines]的东西要在失败时打印。如何将Selenium / Capybara操作记录到其中?

RSpec.configure do |c|
  c.before do |example|
    example.metadata[:extra_failure_lines] = []
  end
end

Capybara::DSL.module_eval do
  alias :old_visit :visit
  def visit path
    example.metadata[:extra_failure_lines] << "visit #{path}"
    old_visit path
  end
end

这会失败,因为Capybara::DSL不了解example

1 个答案:

答案 0 :(得分:0)

我已将example公开为RSpec静态模块变量:

RSpec.module_eval do
  class << self
    attr_accessor :exposed_example
  end
end

RSpec.configure do |c|
  c.before do |example|
    RSpec.exposed_example = example
    example.metadata[:extra_failure_lines] = []
  end
end

# you may use another approaches of monkeypatching
Capybara::DSL.module_eval do
  old = instance_method :visit
  define_method :visit do |path|
    RSpec.exposed_example.metadata[:extra_failure_lines] << "visit #{path.inspect}"
    old.bind(self).(path)
  end
end