How to create arrays from show_links, show_tables, show_images, show_divs that can be used to create re-usable tests
Im 开发者_开发问答new to programming/Ruby/Watir and I would like to know how to create arrays from content scraped from web sites.
Basically I'm prompting the user for the site to test, the URL is entered, and (eventually) I would like to be able to give the user choices as to what elements of the page they would like to target.
######################
# User prompts for site to be tested #
#######################
puts 'Automation test tool: Enter the website that you want to test' url=gets.chomp puts 'Testing the following website:' ' ' + url + ' ' ' '
#####################
# Watir method for invoking IE #
#####################
require 'watir/ie'
ie = Watir::IE.new
ie.goto url
**ie.show_links.each {|l| puts l}**
The line in bold I obtained from another Watir/Ruby site while it did successfully print to screen all of the links, I would rather store the links/images/tables/divs and then give the user the option as to how/what will be tested. Obviously, I need to gain a stronger grasp of how Arrays work in Ruby. Thanks in advance.
They are already in an array: ie.links http://wtr.rubyforge.org/rdoc/1.6.5/classes/Watir/Container.html#M000318
You will find those for many of the types you want to give a choice on. Good luck
The content is not exactly the same as the elements. The page elements are stored in containers, and reference elements by their content (amongst other things). So if you wanted to store a bunch of text fields' IDs you could create an array thusly...
text_field_id_array = Array.new
ie.text_fields.each { |field| text_field_id_array << field.id }
But storing the text fields themselves only stores a "pointer". So if you had a link on a page and you stored the link element using ie.link(:index, 1), then did something that created a link on the page BEFORE your stored link, then clicked your stored link it wouldn't click the same link you stored. It would click "ie.link(:index, 1)". The first link on the page...
mylink = ie.link(:index, 1) # Store the link you want
ie.button(:name, 'createsanearlierlink').click
mylink.click # Click the wrong link
I really hope that makes sense.
精彩评论