watir read from 2 files issue

Hello everybody,

I am somehow new in ruby&watir. All i want to do today its to fill a form, and 2 fields are taken from 2 different files.(urlimages.txt and title.txt) like you see in attACHMENT. After all fields are filled will be hit the "upload" button. After this, the 2 fields (url images and titles) will be again filled with second line from each file),press upload button,after this will be filled with 3th line from each file...and soo on till the end of file lenght,assuming that booth have the same numbers of lines.

My tactic:i tried to open each file with file.open, after this: file.readlines() then make a loop to read each line of files and fill the fields, then click submit. BUT...because are 2 files i find hard to fill the form (source_title and image_url_input) without losing the readlines counter.I mean every time will be read first line of each file,i dont want this. What should i do? to create a global variable that will be the counter of lines from files? please help!

here its my code:

require 'rubygems' require 'watir' Watir::Browser.default = "firefox" goto_url("http://www.imgfave.com/post"\) browser.text_field(:name, "source_title").set("_line_from_C\:\\title.txt") browser.text_field(:id, "image_url_input").set("_line_from_C\:\\urlimages.txt") browser.text_field(:name, "tags").set("funny images") browser.text_field(:name, "source_url").set("http://www.9gag.com") browser.button(:value,"Upload").click

Attachments: http://www.ruby-forum.com/attachment/7916/watir.PNG

Hi, Calin,

Let me see if I understand this.

You basically have two files which contain the lines of data you want to insert into two fields on this form. Assuming there is a one-to-one match between these (i.e., first line of titles.txt goes with first line of urlimages.txt, and so on), you could do the following:

titles = IO.readlines("path/to/titles.txt") imgurls = IO.readlines("path/to/urlimages.txt")

Now you can loop through the arrays using a range:

(1..titles.count).each do |i|    broswer.text_field(:name, "source_title").set(titles[i].strip)    browser.text_field(:name, "image_url_input").set(imgurls[i].strip)    # and rest of processing as you have it.. end

That scheme works if titles.txt and urlimages.txt aren't very large that you can suck them entirely into memory for the operation.

If that's not the case (it'd have to be a *huge* pair of files), you can also just read each line as you go through the loop:

titles = File.open("path/to/titles.txt") imgurls = File.open("path/to/urlimages.txt")

This time, since you don't know the number of lines ahead of time, simply loop until end of file:

until titles.eof? or imgurls.eof?    broswer.text_field(:name, "source_title").set(titles.gets.strip)    browser.text_field(:name, "image_url_input").set(imgurls.gets.strip)    # and rest of processing as you have it.. end

Hello everybody,

I am somehow new in ruby&watir. All i want to do today its to fill a form, and 2 fields are taken from 2 different files.(urlimages.txt and title.txt) like you see in attACHMENT. After all fields are filled will be hit the "upload" button. After this, the 2 fields (url images and titles) will be again filled with second line from each file),press upload button,after this will be filled with 3th line from each file...and soo on till the end of file lenght,assuming that booth have the same numbers of lines.

My tactic:i tried to open each file with file.open, after this: file.readlines() then make a loop to read each line of files and fill the fields, then click submit. BUT...because are 2 files i find hard to fill the form (source_title and image_url_input) without losing the readlines counter.I mean every time will be read first line of each file,i dont want this. What should i do? to create a global variable that will be the counter of lines from files? please help!

Can I clarify something? Which machine are the files on? The server machine running Rails or the client PC running the browser?

Colin

Hey guys thanks for youre answers. this really helped me. It s nice that you can find such great advices when you need them. thanks again. i will apply and will come back with news.

Colin, i put the topic in wrong section, i am sorry. I am not running the script on a server, was just a ruby issue.

Also, am thinking also to automate the writing into files step like this: connect to a database from where will be taken regularry last images and titles that will be writen to titles.txt and urlimages.txt. (thru A cronjob or ....) please advise if i can do something like this, and what should be the easiest and safer way to do it.

thanks