Home > watir > Append text to text_field at watir

Append text to text_field at watir

October 5th, 2008 Leave a comment Go to comments

At watir we can submit texts to forms by such a code like : ie.text_field.set(), we could also append texts by the code such as ie.text_field.append(), following is an example:

require ‘watir’
include Watir

# Initialize the text to be submitted to a bbs
conts = []
File.open(‘goodword.txt’) do |f|
f.each_line{|l| conts << l}
end
#Initialize the advertising text to be appended
advs = []
File.open(‘adv.txt’) do |f|
f.each_line{|l| advs << l}
end
———
ie22.text_field(:name, ‘co’).set(conts[rand(conts.size)])
ie22.text_field(:name, ‘co’).append(“\n”)
ie22.text_field(:name, ‘co’).append(“\n”)
ie22.text_field(:name, ‘co’).append(“\n”)
ie22.text_field(:name, ‘co’).append(“\n”)
ie22.text_field(:name, ‘co’).append(advs[rand(advs.size)])

—–

“\n”     means change to a new line.



Categories: watir Tags:
  1. October 30th, 2008 at 19:50 | #1

    Good example, but I thought it is better to use:

    tmp_conts = conts[rand(conts.size)] + “\n” + “\n” + “\n” + “\n” + advs[rand(advs.size)]
    ie22.text_field(:name, ‘co’).set(tmp_conts)

  1. No trackbacks yet.
      
                  
      

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word