覆盖Capybara方法以添加日志记录

时间:2016-02-12 03:25:11

标签: ruby selenium selenium-webdriver capybara

我在Ruby中使用Capybara-Selenium运行一些自动化测试,我想将日志记录添加到Capybara操作中。

要做到这一点,我已经覆盖了click_onhas_text?等基本方法

这些方法现在看起来像这样:

def has_text? text, options = {}
  if page.has_text? text, options
    @step.log :pass, "Looking for text \"#{text}\""
    true
  else
    @step.log :fail, "Looking for text \"#{text}\""
    false
  end
end

def has_field? locator, options = {}
  if page.has_field? locator, options
    @step.log :pass, "Looking for field \"#{locator}\""
    true
  else
    @step.log :fail, "Looking for field \"#{locator}\""
    false
  end
end

这很好用。但是,这些方法仅在使用的Web元素具有ID时才有用。如果没有ID,则使用find方法找到该元素,然后直接操作,例如:

find(:css, 'some.locator').click

我也希望为这些事件添加日志记录。我可以覆盖find并添加通用日志记录,但如果我不知道该元素在被发现后用于什么,那将不会非常有用。我可以在我的find方法中添加另一个参数,但写入和读取会很笨拙。

  find(:css, 'some.locator', 'click').click

在我屈服于该解决方案之前,我想检查是否有更简单或更好的实现我可以使用?我正在思考某种类型的sudo类,它取代了find返回的对象,并记录了它的所有方法,如果我可以将某种事件附加到各种方法上,那就更好了告诉我的记录器什么时候需要添加东西...我不知道这些方法是否可行和/或可行。

0 个答案:

没有答案