Read text file and check multiple checkbox

Discussion in 'Ruby' started by KingMaker KingMaker, May 20, 2010.

  1. Hi Friends,

    I try to check the Multiple Checkboxes in the page.
    first i read the value in the text file and assing it to the variable.
    but the problem is that at the time only one check box are selected.

    textfile values:
    selectedLocationOids=0000025354000000630d
    selectedLocationOids=000002527100000062bc
    ______________________________________________________________________________
    HTML Code:

    <input type="checkbox" value="0000025354000000630d"
    name="selectedLocationOids" id="selectedLocationOids"
    class="vmCheckbox">

    <input type="checkbox" value="000002527100000062bc"
    name="selectedLocationOids" id="selectedLocationOids"
    class="vmCheckbox">
    ______________________________________________________________________________

    RUBY Script:
    require 'watir'
    include Watir
    require 'logger'

    opts = {}
    File.open("c:/test/Flow1.txt") do |f|
    f.each_line do |line|
    opts[$1] = $2 if line =~ /^(.*)=(.*)$/
    end
    end
    @check_location = opts["selectedLocationOids"]
    @check_location1 = opts["selectedLocationOids"]


    ie.checkbox:)value,@check_location).set()
    ie.checkbox:)value,@check_location1).set()
    --
    Posted via http://www.ruby-forum.com/.
    KingMaker KingMaker, May 20, 2010
    #1
    1. Advertising

  2. KingMaker KingMaker wrote:
    > Hi Friends,
    >
    > I try to check the Multiple Checkboxes in the page.
    > first i read the value in the text file and assing it to the variable.
    > but the problem is that at the time only one check box are selected.
    >
    > textfile values:
    > selectedLocationOids=0000025354000000630d
    > selectedLocationOids=000002527100000062bc
    > ______________________________________________________________________________



    In the example that you provided it seems that value of @check_location
    is equal to the value of @check_location1. Shouldn't they be different?
    --
    Posted via http://www.ruby-forum.com/.
    Marcin Wolski, May 20, 2010
    #2
    1. Advertising

  3. KingMaker KingMaker

    Josh Cheek Guest

    Re: Read text file and check multiple checkbox

    [Note: parts of this message were removed to make it a legal post.]

    On Thu, May 20, 2010 at 8:05 AM, KingMaker KingMaker
    <>wrote:

    > Hi Friends,
    >
    > I try to check the Multiple Checkboxes in the page.
    > first i read the value in the text file and assing it to the variable.
    > but the problem is that at the time only one check box are selected.
    >
    > textfile values:
    > selectedLocationOids=0000025354000000630d
    > selectedLocationOids=000002527100000062bc
    >
    > ______________________________________________________________________________
    > HTML Code:
    >
    > <input type="checkbox" value="0000025354000000630d"
    > name="selectedLocationOids" id="selectedLocationOids"
    > class="vmCheckbox">
    >
    > <input type="checkbox" value="000002527100000062bc"
    > name="selectedLocationOids" id="selectedLocationOids"
    > class="vmCheckbox">
    >
    > ______________________________________________________________________________
    >
    > RUBY Script:
    > require 'watir'
    > include Watir
    > require 'logger'
    >
    > opts = {}
    > File.open("c:/test/Flow1.txt") do |f|
    > f.each_line do |line|
    > opts[$1] = $2 if line =~ /^(.*)=(.*)$/
    > end
    > end
    > @check_location = opts["selectedLocationOids"]
    > @check_location1 = opts["selectedLocationOids"]
    >
    >
    > ie.checkbox:)value,@check_location).set()
    > ie.checkbox:)value,@check_location1).set()
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    In addition to what Marcin said:
    *Your html can't have two elements with the same ID (ID's must be unique,
    like a social security number)
    *Your inputs should end with /> instead of just > to close the tag.
    *Since they are checkboxes, they should also have different names. The name
    is what determines the name of the key that the server receives, so when you
    have two inputs whose names are selectedLocationOids, then clicking both
    causes one to be overwritten.

    To illustrate, when I submit your form, my server receives:
    Submitting with neither box checked
    Parameters: {"commit"=>"Create",
    "authenticity_token"=>"1vYLmUq7Yc52/Z8Gum2qxQz9i2YyXYL0+/4AEu434Wg="}

    Submitting with the first box checked
    Parameters: {"commit"=>"Create",
    "selectedLocationOids"=>"0000025354000000630d",
    "authenticity_token"=>"1vYLmUq7Yc52/Z8Gum2qxQz9i2YyXYL0+/4AEu434Wg="}

    Submitting with the second box checked
    Parameters: {"commit"=>"Create",
    "selectedLocationOids"=>"000002527100000062bc",
    "authenticity_token"=>"1vYLmUq7Yc52/Z8Gum2qxQz9i2YyXYL0+/4AEu434Wg="}

    Submitting with both boxes checked
    Parameters: {"commit"=>"Create",
    "selectedLocationOids"=>"000002527100000062bc",
    "authenticity_token"=>"1vYLmUq7Yc52/Z8Gum2qxQz9i2YyXYL0+/4AEu434Wg="}

    You can see that that even with both boxes checked, only receive one value,
    because they both have the same name. It might help you to read up on how
    hash tables work (http://en.wikipedia.org/wiki/Hash_table)

    * I'm on a Mac, so I can't check your example, but I'm not sure what "ie"
    is. It looks like a variable ,but could be a method. If it is a method, I
    don't see anything like it for Firefox, and their examples do not seem to
    behave the way you have used it (http://watir.com/examples/) If it is a
    variable, then where was it initialized? It looks like you need to say ie =
    Browser.start("http://your-url.com")
    Josh Cheek, May 20, 2010
    #3
    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. tshad
    Replies:
    0
    Views:
    487
    tshad
    Apr 15, 2005
  2. Replies:
    4
    Views:
    937
    M.E.Farmer
    Feb 13, 2005
  3. Replies:
    10
    Views:
    915
    Mark Rae
    Dec 5, 2006
  4. JB
    Replies:
    1
    Views:
    542
  5. Terje
    Replies:
    5
    Views:
    369
    Evertjan.
    Nov 18, 2004
Loading...

Share This Page