2
Hello, I am developing a test automation where I have a variable that receives dynamic values, according to the test run.
In the case of the contents of each element changes according to the websites accessed. For example:
element :formulario, '#resultado'
element :formulario_a, '#resultado-a'
element :formulario_b, '#resultado-b'
I created a marry, where I try to interpolate the variable name, to reduce the written cost of when's in the code.
case valor
when 'a' || 'b'
formulario_"#{valor}".click login
else
formulario.click login
end
When executing the code I get the following error:
Nomethoderror: Undefined method
click' for "formulario_a":String from (pry):3:in
form'
How do I dynamize the name of the variable?
Thanks, solved through the first block of code.
var_suffix = marca ? "_#{marca}" : ''
form = eval("formulario#{var_suffix}")
form.click
– Vini Gabriel
I don’t recommend using Eval. The best way to do this in Ruby would be with send, or something similar, anyway. = ) https://stackoverflow.com/questions/1902744/when-is-eval-in-ruby-justified
– vinibrsl
I agree that Eval has to be used carefully. In the case presented, there is no problem at all. I wouldn’t use Eval in most situations either, but local_variable_get. So I put it as an alternative.
– Fernando Vieira