=begin license --------------------------------------------------------------------------- Copyright (c) 2004-2005, Atomic Object LLC All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name "Atomic Object LLC" nor the names of contributors to this software may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------- (based on BSD Open Source License) =end require 'test/unit/assertions' require 'watir' module Watir # = Description # Watir::Simple is a simple wrapper around the Watir module. It provides a # similar set of operations while simplifying them and removing as much syntax # and test-framework context code as possible. # The goal is to allow toolsmiths to write domain-language frameworks on top of # Watir, using Watir::Simple as an easier, lightweight interface to the power # of Watir. # # = Note # Most action methods in Watir::Simple will automatically wait for the browser # not to be busy before and after they perform the specified action. # # revision: $Revision: 1505 $ module Simple # Open up a browser and point it at a certain URL. def new_browser_at(url) @@browser = IE.new @@browser.typingspeed = 0 @@browser.goto url end # Tell the browser to load a particular URL. def navigate_to(url) @@browser.goto url end # Much like click_link_with_url but navigates to link instead of clicking it, # thereby not invoking OnClick for links. def navigate_to_link_with_url(url) # FIXME: this should be moved into Watir! wait_before_and_after do doc = @@browser.getDocument links = doc.links link = nil links.each do |n| match = false case url when Regexp match = (n.invoke("href") =~ url) when String match = (n.invoke("href") == url) end if match link = n break end end raise "Couldn't find link with url #{url}" unless link @@browser.goto link end end # Much like click_link_with_id but navigates to link instead of clicking it, # thereby not invoking OnClick for links. def navigate_to_link_with_id(id) # FIXME: this should be moved into Watir! wait_before_and_after do doc = @@browser.getDocument links = doc.links link = nil links.each do |n| match = false case id when Regexp match = (n.invoke("id") =~ id) when String match = (n.invoke("id") == id) end if match link = n break end end raise "Couldn't find link with id #{id}" unless link @@browser.goto link end end # Tell the browser to click on the first link with the specified URL. # This takes the address of the link instead of the text displayed. # * url - can be a string to match exactly, or a regular expression. # # Example: # click_link_with_url "http://google.com" # or: # click_link_with_url /goo*/ def click_link_with_url(url) wait_before_and_after { @@browser.link(:url, url).click } end # Tell the browser to click on the first link with the specified id attribute # (the preferred method.) def click_link_with_id(id) wait_before_and_after { @@browser.link(:id, id).click } end # Tell the browser to click on the first link with the specified name attribute def click_link_with_name(name) wait_before_and_after { @@browser.link(:name, name).click } end # Tell the browser to click on the specified link as determined by the # sequential ordering of links on the document. def click_link_with_index(index) wait_before_and_after { @@browser.link(:index, index).click } end # Tell the browser to click on the first link with the specified text in the # link body. def click_link_with_text(text) wait_before_and_after { @@browser.link(:text, text).click } end # Set the text of the field with a given name (the preferred method.) # This only types characters into the field and does not submit the form. def enter_text_into_field_with_name(name, text) wait_before_and_after { @@browser.textField(:name, name).set(text) } end # Set the text of the field with a given id attribute (the preferred method.) # This only types characters into the field and does not submit the form. def enter_text_into_field_with_id(id, text) wait_before_and_after { @@browser.textField(:id, id).set(text) } end # Set the text of the indexed field. This only types characters # into the field and does not submit the form. def enter_text_into_field_with_index(index, text) wait_before_and_after { @@browser.textField(:index, index).set(text) } end # Select an item from a selectbox (a.k.a "combo box", or "pulldown") # The selectbox is chose by matching its name attribute. # The item is selected based on the text content of