[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how can I use variable as a attribute name

Darin Duphorn

2/5/2008 9:23:00 PM

I'm unable to use the attribute name (variable) to verify the button
with the attribute value exists.

I've tried the following:

#Set Variables
$attribute_nm = "type"
$attribute_val = "submit"

#Verify Button Exists
if $ie.button(:$attribute_nm,$attribute_val).exists?

#Verify Button Exists
if $ie.button(:+$attribute_nm,$attribute_val).exists?

#Verify Button Exists
if $ie.button($attribute_nm,$attribute_val).exists?

Thanks,

DD
--
Posted via http://www.ruby-....

3 Answers

wade

2/5/2008 11:44:00 PM

0

I assume from context that you're using Watir. I'm pretty sure Watir
is expecting a symbol there for the first argument, which is probably
why your attempts aren't working. Maybe try using the to_sym method
of the String class, like this:

if $ie.button($attribute_nm.to_sym,$attribute_val).exists?

Good luck,


wade

On Feb 5, 1:23 pm, Darin Duphorn <dduph...@redbrickhealth.com> wrote:
> I'm unable to use the attribute name (variable) to verify the button
> with the attribute value exists.
>
> I've tried the following:
>
> #Set Variables
> $attribute_nm = "type"
> $attribute_val = "submit"
>
> #Verify Button Exists
> if $ie.button(:$attribute_nm,$attribute_val).exists?
>
> #Verify Button Exists
> if $ie.button(:+$attribute_nm,$attribute_val).exists?
>
> #Verify Button Exists
> if $ie.button($attribute_nm,$attribute_val).exists?
>
> Thanks,
>
> DD
> --
> Posted viahttp://www.ruby-....

Phrogz

2/6/2008 12:10:00 AM

0

On Feb 5, 2:23 pm, Darin Duphorn <dduph...@redbrickhealth.com> wrote:
> I'm unable to use the attribute name (variable) to verify the button
> with the attribute value exists.
>
> I've tried the following:
>
> #Set Variables
> $attribute_nm = "type"
> $attribute_val = "submit"
>
> #Verify Button Exists
> if $ie.button(:$attribute_nm,$attribute_val).exists?
>
> #Verify Button Exists
> if $ie.button(:+$attribute_nm,$attribute_val).exists?
>
> #Verify Button Exists
> if $ie.button($attribute_nm,$attribute_val).exists?

Wow, that's a lot of global variables. If it's a symbol you want:
irb(main):001:0> f = "foo"
=> "foo"
irb(main):002:0> p f.to_sym, f.intern, :"#{f}"
:foo
:foo
:foo

If it's a method call you're trying to do based on the variable, see
the Object#send method:

C:\>ri Object#send
------------------------------------------------------------
Object#send
obj.send(symbol [, args...]) => obj
obj.__send__(symbol [, args...]) => obj
------------------------------------------------------------------------
Invokes the method identified by _symbol_, passing it any
arguments
specified. You can use +__send__+ if the name +send+ clashes with
an existing method in _obj_.

class Klass
def hello(*args)
"Hello " + args.join(' ')
end
end
k = Klass.new
k.send :hello, "gentle", "readers" #=> "Hello gentle
readers"

Darin Duphorn

2/6/2008 4:08:00 PM

0

wade wrote:
> I assume from context that you're using Watir. I'm pretty sure Watir
> is expecting a symbol there for the first argument, which is probably
> why your attempts aren't working. Maybe try using the to_sym method
> of the String class, like this:
>
> if $ie.button($attribute_nm.to_sym,$attribute_val).exists?
>
> Good luck,
>
>
> wade

Nice! That worked. This will save me tons of extra lines of Code.

Thanks Wade, Your the greatest.

DD

--
Posted via http://www.ruby-....