2

How can i use Rspec Expectations in Page-object classes. I need to assert elements. Currently i am using xpath, other locators to check element existence. I know using it is step definitions. But i need it in classes.

Code:

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

In step definitions i am able to use it like:

@current_page.text.should include "message"
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
spectator
  • 329
  • 2
  • 15

1 Answers1

6

Assuming you have already required 'rspec', you just need to include the RSpec::Matchers module in your class.

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

Note that some would recommend only doing assertions in tests (not in page objects).

Justin Ko
  • 46,526
  • 5
  • 91
  • 101