Search code examples
rubywatirwatir-webdriverpageobjectspage-object-gem

converting string to element in page object


Page object gem with ruby, and I have defined some number of page-objects for links like:

  link(:link_for_1week, text: /1 wk/)
  link(:link_for_2week, text: /2 wks/)
  link(:link_for_3week, text: /3 wks/)
  link(:link_for_4week, text: /4 wks/)
  link(:link_for_5week, text: /5 wks/)
  link(:link_for_6week, text: /6 wks/)
  link(:link_for_2months, text: /2 mo/)
  link(:link_for_3months, text: /3 mo/)
  link(:link_for_6months, text: /6 mo/)
  link(:link_for_1year, text: /1 yr/)

and I am having a method to call these links dynamically with user input:

If I give user input as '1week' it has to click on 'link_for_1week'
If I give user input as '2week' it has to click on 'link_for_2week' 
and so

I don't know how to do and I even used instance_eval, but it doesn't work:

def click_link(args:)
  self.browser.instance_eval('link_for_'+args+'_element.click')
end

But am not able to click the link.

I am not sure on using this instance_eval.. apart from instance_eval also most welcomed...


Solution

  • You can use send to convert a String to method call:

    def click_link(text)
      send("link_for_#{text}")
    end