How to read data in text file and put into the web page

Hi i want to read data in the text file like.

username=abcd
password=1234

i write code through the counter and i want write code without counter.
Plz help me…

require ‘watir’
include Watir

counter = 1

ie=Watir::IE.start(“http://www.google.co.in”)
file = File.new(“c:/test/test.txt”, “r”)

while (line = file.gets ‘=’)
line = file.readline
while(counter == 1)
ie.text_field(:name,“userId”).set(line)
break
end
while(counter == 2)
ie.text_field(:name,“password”).set(line)
break
end
counter = counter + 1
end
file.close
ie.button(:name,“Submit”).click()

******* It’s Realy works **************

Thanks a lotttttttttttttt…!!!1

On Tue, May 11, 2010 at 6:38 PM, Zubair A. [email protected]
wrote:

Hi i want to read data in the text file like.

username=abcd
password=1234

i write code through the counter and i want write code without counter.
Plz help me…

You have a file with just those two variables? Then you can do this:

contents = File.read(“c:/test/test.txt”)
data = contents.map do |line|
line.chomp.split(“=”)[1]
end

Then you have

data[0] #=> abcd
data[1] #=> 1234

and you can use those directly:

ie.text_field(:name,“userId”).set(data[0])
ie.text_field(:name,“password”).set(data[1])

Hope this helps,

Jesus.

Zubair A. wrote:

Hi i want to read data in the text file like.

username=abcd
password=1234

i write code through the counter and i want write code without counter.

Building a Hash of your input data is one way to make it convenient to
access:

opts = {}
File.open(“test.txt”) do |f|
f.each_line do |line|
opts[$1] = $2 if line =~ /^(.)=(.)$/
end
end
puts opts[“username”]
puts opts[“password”]

A lot of Rubyists use a YAML input file instead, which would have the
form

username: abcd
password: “1234”

(note the quotes needed to make it parse as a string rather than a
number). Then it just becomes:

require ‘yaml’
opts = YAML.load_file(“test.yaml”)