Alternative to switch statements

Discussion in 'Ruby' started by aidy, Mar 26, 2007.

  1. aidy

    aidy Guest

    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', ''
    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
     
    aidy, Mar 26, 2007
    #1
    1. Advertising

  2. On 26.03.2007 18:04, aidy wrote:
    > 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', ''
    > 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
     
    Robert Klemme, Mar 26, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    8
    Views:
    1,219
    mfmehdi
    Oct 20, 2006
  2. Neil Zanella

    C99: variable declarations inside switch statements

    Neil Zanella, Oct 27, 2003, in forum: C Programming
    Replies:
    5
    Views:
    516
    Christian Bau
    Oct 28, 2003
  3. Harry George
    Replies:
    6
    Views:
    431
    Bart Nessux
    Feb 23, 2004
  4. Vince
    Replies:
    12
    Views:
    768
    Martin Gregorie
    Jan 21, 2008
  5. John Crichton
    Replies:
    6
    Views:
    282
    John Crichton
    Jul 12, 2010
Loading...

Share This Page