Stn
Stn

Reputation: 447

Watir & Ruby; A couple beginner "how can I do this" questions inside

Programming newbie here, about a month in with Ruby. I'm currently playing around with Watir and have a few questions to streamline the automation process of a simple form fill.

Current Code:

# Start Watir
require 'watir-webdriver'
# Open Firefox w/ Watir
browser = Watir::Browser.new :ff
# Visit URL w/ browser
browser.goto("http://website.com")
# Fill in page name field
browser.text_field(:name => "page_name").set "page_name1"
# Fill in password field
browser.text_field(:name => "password").set "static_password"
# Click publish button
browser.button(:value => "Publish").click
# Puts URL
puts browser.url

What I'd like to add on to this is:

Any guidance on this is much appreciated, I'm not getting too far guessing, I just need a push in the right direction.

Upvotes: 0

Views: 1374

Answers (3)

marc
marc

Reputation: 509

What I would do is create an array for all of the page names and iterate through it.

pages = ["page_name1", "page_name2", "page_name3"]

pages.each {|page| browser.text_field(:name => "page_name").set page}

Upvotes: 0

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

Ruby has a wonderful concept called iterables which can help with this.

First bullet. looping a given number of times, lets say 100 for example. There about 10 different ways to skin this cat, but my favorite is just this

100.times do
  #code lines to be repeated
end

Second bullet, which likely changes the approach presuming what you want is to do something once for every line in the array of site names.

First create your array, then make use of the wonderful .each method to do something with each row of the array. If your array was named page_names then

page_names.each do |name|
  #code to be repeated, use `name` variable at point you need the value
  browser.text_field(:name => "page_name").set name
end

This .each method can be used with just about any 'collection' of items, which means you can end up doing stuff that looks like this

pages.each do |page|
  code that does something with page.name
  code that does something with page.password
end

Third bullet, yep Michael (who I saw just posted an answer) has what I'd have suggested which is a nested hash, although since you are starting with page names, and that's the part you know, and the URL is something the script is gathering, I'd use the page_name as the key to the outer hash that would let you look up the url and password values for each page name. (I'm presuming in that case you would be doing something like generating the passwwords randomly and thus the need to store them)

Combining the above with .each it's important to know that in that case of using .each with hashes, the first thing you get back from .each is the key, and then the value. So if you had an the nested hash discussed above prepopulated with passwords, and keyed on page names, you might do something like this

pages.each do |page_name, pageinfo|
  browser.goto("http://website.com")
  browser.text_field(:name => "page_name").set page_name
  browser.text_field(:name => "password").set pageinfo[:password]
  browser.button(:value => "Publish").click
  pages[page_name][:url] = browser.url
end

(given murphy loves me I expect I've made some mistake above, which another rubiest will no doubt spot.. fortunately you can edit these answers to fix that stuff later)

Upvotes: 2

Michael Kohl
Michael Kohl

Reputation: 66857

  • Loop: xxx.times do ... end
  • Page name array: page_names.each do |page_name| ... end
  • How about a nested hash? Use the URL as key for the outer hash and the name and password as keys in the inner hash.
{ "http://foo.com" => { :name => "Foo", :password => "super_s3kr3t" }
  "http://bar.com" => { :name => "Bar", :password => "also_s3kr3t" } }

Upvotes: 2

Related Questions