rubyrspeccucumberpage-object-gemrspec-expectations

how can i use Rspec Expectations in page object classes


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"

Solution

  • 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).