I am using the PageObject pattern, which abstracts HTML details away from the top-level of the test. I am doing this using the SitePrism gem.
In my example, I have a home page (HomePage.rb):
class HomePage < SitePrism::Page
set_url ENV['BASE_URL']
section :header, HeaderSection, "div[class='headerSection']"
which refers to a common page section, HeaderSection (HeaderSection.rb):
class HeaderSection < SitePrism::Section
element :sign_in_button, "a[class='signIn']"
and a step definition for my tests (login.rb):
And(/^I am not logged in/) do
@home_page = HomePage.new # actually, this is specified in env.rb
expect(@home_page.header).to have_sign_in_button
end
Instead of exposing the web element to the step definition, I want to encapsulate this within a class method of HomePage. It seems that the best way to do that is to put the assertion into a class method within HomePage itself (HomePage.rb):
def amILoggedIn
expect(header).to have_sign_in_button
end
The above assumes that I am also using include RSpec::Matchers
.
My method would then be the only thing exposed to the step definition (login.rb):
And(/^I am not logged in/) do
@home_page.amILoggedIn
end
As part of SitePrism, the sign_in_button
element has its own built-in methods to check for its presence, which is:
header.has_sign_in_button?
Question
In terms of best practice, which is the most recommended way of writing this assertion within the amILoggedIn
method (even though it appears they both use the same instruction),
expect(header).to have_sign_in_button
or
header.has_sign_in_button?