On Sat, Dec 1, 2012 at 7:14 AM, calin simona <lists@ruby-forum.com> wrote:
> 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
>
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-talk@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
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
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment