Search code examples
rubyselenium-webdriverwatirpage-object-gem

How to navigate to a link using page object gem?


How to navigate to a page using page-object gem when am calling in a method?

class SidePage
 include PageObject

link(:create, text: /Create/)

def navigate_to(link)
  if link == 'Test'
    create
  else
    navigate_to "http://test.jprr.w3test/#{link}" # --> here i need to navigate on else condition.
  end
end

I need to navigate to the given link dynamically in else condition based on the #{link} text.


Solution

  • You cannot call #navigate_to within #navigate_to as it will go into an infinite loop. There are a couple ways to solve this.

    The easiest approach is to name your method differently. Part of the benefit is that it's clear that this page's #navigate_to is different than other pages.

    def navigate_or_click(link)
        if link == 'Test'
            create
        else
            navigate_to "http://test.jprr.w3test/#{link}"
        end
    end
    

    If you want to stick with the #navigate_to method name, you just need to call the appropriate browser method instead:

    def navigate_or_click(link)
        if link == 'Test'
            create
        else
            browser.goto "http://test.jprr.w3test/#{link}" # or platform.navigate_to
        end
    end