rubyautomated-testspage-object-gem

How to find element in page-object gem with custom parameter?


I use page-object gem with RSpec and I want to create an element with a custom parameter like

link(:derect_link, text: "#{custom_text}"

Because I want to get the link to text and this text was changed every time when I starting the test.

How can I use it in spec scenario?


Solution

  • The accessor methods do not support custom parameters at run time. You will have to manually create the methods for the link. The equivalent of the methods created by the link accessor would be:

    class MyPage
      include PageObject
    
      def derect_link_element(text)
        link_element(text: text)
      end
    
      def derect_link(text)
        derect_link_element(text).click
      end
    
      def derect_link?(text)
        derect_link_element(text).exists?
      end
    end
    

    This would be used like the standard methods, except that you would specify the text of the link:

    # Click the link
    page.derect_link('custom_text')
    
    # Check if the link exists
    page.derect_link?('custom_text')
    
    # Get the link element to perform other actions (eg inspect attribute values)
    link = page.derect_link_element('custom_text')
    link.attribute('href')