Alternative to switch statements

A

aidy

Hi,

I am trying to write a 'keyword-driven test automation framework' in
Ruby.

Using a case statement

<snip>
def field(name, args)
case name

when 'username'
name='login'

when 'password'
name='password'
end

return $ie.text_field:)name, name).set(args)

end

def button(name)
case name

when 'login'
name = 'Login'

end

return $ie.button:)value, name).click

end

<snip>

Calling code

<snip>
field 'username', '(e-mail address removed)'
field 'password', 'password123'
button 'login'
<snip>

I don't like it because

1. It is procedural
2. Lengthy switch statements may affect performance

Could anyone suggest a way using, collections or symbols or something
else?

Thanks

aidy
 
R

Robert Klemme

Hi,

I am trying to write a 'keyword-driven test automation framework' in
Ruby.

Using a case statement

<snip>
def field(name, args)
case name

when 'username'
name='login'

when 'password'
name='password'
end

return $ie.text_field:)name, name).set(args)

end

def button(name)
case name

when 'login'
name = 'Login'

end

return $ie.button:)value, name).click

end

<snip>

Calling code

<snip>
field 'username', '(e-mail address removed)'
field 'password', 'password123'
button 'login'
<snip>

I don't like it because

1. It is procedural
2. Lengthy switch statements may affect performance

Could anyone suggest a way using, collections or symbols or something
else?

This is an obvious case for Hash with a custom default value handler:

MAP_FIELD = Hash.new {|h,k| k}.update(
'username' => 'login'
)

def field(name,args)
$ie.text_field:)name, MAP_FIELD[name]).set(args)
end

....

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top