我如何在页面对象类中使用Rspec Expectations

时间:2014-07-18 03:56:39

标签: ruby rspec cucumber page-object-gem rspec-expectations

如何在页面对象类中使用Rspec Expectations。我需要断言元素。目前我正在使用xpath,其他定位器来检查元素是否存在。我知道使用它是步骤定义。但我在课堂上需要它。

代码:

class AssertTest
include PageObject

span(:success, text: "Message added successfully")

def assert_element
    success_element.when_present (or) success?
    # I need to use Rspec expectations instead

    # continue...
end
end

在步骤定义中,我可以使用它:

@current_page.text.should include "message"

1 个答案:

答案 0 :(得分:6)

假设您已经需要'rspec',您只需要在您的班级中包含RSpec :: Matchers模块。

class AssertTest
  include PageObject
  include RSpec::Matchers

  span(:success, text: "Message added successfully")

  def assert_element()
    # Example assertions for checking element is present
    success_element.should be_visible
    expect(success_element).to be_visible
  end

end

请注意,有些人会建议只在测试中进行断言(而不是在页面对象中)。