0

Creating tests in Ruby, Capybara using SitePrism. I have faced situation, where I have one site but content of the site depends on permissions the user has. For example element "admin" in menu is visible only for admins e.t.c. One major difference is that admins has their own subdomain like admin.example.com (site for normal user is example.com).

I have to test it both from admins and users point of view and I want to avoid creating two almost identical page objects.

Is there a right way to solve this?

Jakub Smolar
  • 81
  • 10
  • Create a single page object (per view) containing all possible elements and check for the existence of a specific element, e.g. `@page.menu.has_admin?` or `@page.menu.has_no_admin?` – Stefan Apr 20 '18 at 14:39
  • I was thinking about similar solution. When I declare element, I can override SitePrisms element method and check if every fetched element is present on the site like you suggested. If not, then return null or something. – Jakub Smolar Apr 20 '18 at 14:45
  • Besides accessing the element, you probably want to ensure that it is present / absent in the corresponding context. You therefore have to declare it. SitePrism already returns `nil` if an element is missing. – Stefan Apr 20 '18 at 14:49
  • Thank you, I'll consider that! – Jakub Smolar Apr 20 '18 at 15:08

2 Answers2

0

So there are a variety of tools at your disposal here.

#all_there? will check if all declared elements are on the page. Furthermore this can be restricted down via the Use of DSL statements such as .expected_elements

I would advise you going back to the README which has a lot of new info in the last 6-9months and checking on it.

In terms of scoping so there is the concept of a user and an admin, that's also easy to partition using variables in your ENV hash perhaps and setting the url accordingly. Again there is documentation on this on the SitePrism docs/Github.

If you feel as though this still isn't working or there is an issue, open an issue request here: https://github.com/natritmeyer/site_prism/issues

Luke Hill
  • 460
  • 2
  • 10
0
class ExamplePage < SitePrism::Page
  ...
  element :test_element, '#test_element'
  ...
end

...

let!(:example_page) { ExamplePage.new }

context 'situation 1' do    
  it 'displays test_element' do
    ...
    expect(example_page).to have_test_element
  end
end

context 'situation 2' do
  it 'displays test_element' do
    ...
    expect(example_page).to_not have_test_element
  end
end
General Grievance
  • 4,555
  • 31
  • 31
  • 45