-1

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...

orde
  • 5,233
  • 6
  • 31
  • 33
Ram Kumar
  • 45
  • 6

3 Answers3

3

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

def click_link(text)
  send("link_for_#{text}")
end
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
0

I haven't used that gem but the argument in the method is wrong. Instead of

def click_link(args:) 

use

def click_link(args) 
s1mpl3
  • 1,456
  • 1
  • 10
  • 14
0

Below mentioned code will solve your problem:

("link_for_#{args}_element")

When you need to write/get dynamic value from any ruby variable in double quotes use "#{variable}" syntax.