Selenium Tutorial: Appendix

14. Appendix

14.1. Selenium RC API Reference (Ruby)

  • alert

    Retrieves the message of a JavaScript alert, or fail if there were no alerts

  • alert

    Whether an alert occurred

  • all_window_ids

    Returns the IDs of all windows that the browser knows about

  • all_window_names

    Returns the names of all windows that the browser knows about

  • all_window_titles

    Returns the titles of all windows that the browser knows about

  • body_text

    Gets the entire text of the page

  • browser_network_traffic(format = :plain)

    Network traffic seen by the browser

  • checked?(locator)

    Returns whether a toggle-button (checkbox/radio) is checked

  • click(locator, options={})

    Clicks on a link, button, checkbox or radio button. Using options you can automatically wait for an event to happen after the click. e.g

    • click "a_locator", :wait_for => :page

      Will wait for a new page to load

    • click "a_locator", :wait_for => :popup, :window => "a window id"

      Will wait for a new popup window to appear. Also selects the popup window for you provide :select => true

    • click "a_locator", :wait_for => :ajax

      Will wait for all ajax requests to be completed using semantics of default javascript framework

    • click "a_locator", :wait_for => :ajax, :javascript_framework => :jquery

      Will wait for all ajax requests to be completed overriding default javascript framework

    • click "a_locator", :wait_for => :effects

      Will wait for all javascript effects to be rendered using semantics of default javascript framework

    • click "a_locator", :wait_for => :effects, :javascript_framework => :prototype

      Will wait for all javascript effects to be rendered overriding default javascript framework

    • click "a_locator", :wait_for => :element, :element => "new_element_id"

      Will wait for an element to be present/appear

    • click "a_locator", :wait_for => :no_element, :element => "new_element_id"

      Will wait for an element to be not be present/disappear

    • click "a_locator", :wait_for => :text, :text => "some text"

      Will wait for some text to be present/appear

    • click "a_locator", :wait_for => :text, :text => /A Regexp/

      Will wait for some text to be present/appear using a regular expression

    • click "a_locator", :wait_for => :text, :element => "a_locator", :text => "some text"

      Will wait for the content of a_locator to be some text

    • click "a_locator", :wait_for => :no_text, :text => "some text"

      Will wait for the text to be not be present/disappear

    • click "a_locator", :wait_for => :no_text, :text => /A Regexp/

      Will wait for the text to be not be present/disappear

    • click "a_locator", :wait_for => :no_text, :element => "a_locator", :text => "some text"

      Will wait for the content of a_locator to not be some text

    • click "a_locator", :wait_for => :value, :element => "a_locator", :value => "some value"

      Will wait for the field value of a_locator to be some value

    • click "a_locator", :wait_for => :no_value, :element => "a_locator", :value => "some value"

      will wait for the field value of a_locator to not be some value

    • click "a_locator", :wait_for => :visible, :element => "a_locator"

      will wait for the element to be visible on the page

    • click "a_locator", :wait_for => :not_visible, :element => "a_locator"

      will wait for the element to be not visible anymore on the page

    • click "a_locator", :wait_for => :condition, :javascript => "some expression"

      Will wait for the javascript expression to be true Using options you can also define an explicit timeout (:timeout_in_seconds key). Otherwise the default driver timeout is used

  • confirmation

    Message of a JavaScript confirmation dialog generated during the previous action

  • confirmation?

    Whether a confirmation has been auto-acknoledged (i.e. confirm() been called)

  • cookie(name)

    Value of the cookie with the specified name, or error if the cookie is not present

  • cookie?(name)

    Returns true if a cookie with the specified name is present, or false otherwise

  • cookies

    Return all cookies for the current page under test

  • create_cookie(name_value_pair, options="")

    Create a new cookie, see docs for details

  • delete_cookie(name, options="")

    Delete a named cookie, see docs for detail

  • element?(locator)

    Verifies that the specified element is somewhere on the page

  • execution_delay

    Get delay following each selenium operation, in milliseconds

  • execution_delay=(delay)

    Pause delay following each selenium operation

  • field(locator)

    Gets the (whitespace-trimmed) value of an input field; "on" or "off" for checkboxes

  • go_back(options={})

    Simulates the user clicking the "back" button on their browser

  • js_eval(script)

    Returns the result of evaluating the specified JavaScript snippet whithin the browser

  • location

    Returns the absolute URL of the current page

  • prompt

    Message of a JavaScript question prompt dialog generated during the previous action

  • prompt?

    Whether a prompt occurred

  • remote_control_timeout_in_seconds=(timeout)

    Set the Remote Control timeout

  • table_cell_text(tableCellAddress)

    Retrieves the text from a cell of a table. The tableCellAddress syntax is tableLocator.row.column. Row and column start at 0

  • text(locator)

    Text content of an HTML element

  • text?(pattern)

    Verifies that the specified text pattern appears somewhere on the rendered page

  • text_content(locator)

    Alias for text

  • title

    Return the title of the current HTML pages

  • value(locator)

    Alias for field

  • visible?(locator)

    Determines if the specified element is visible. Fails if the element is not present

  • wait_for(options)

    Flexible wait semantics. Wait is happening browser side. Useful for testing AJAX applications

  • wait :wait_for => :page

    Will wait for a new page to load

  • wait :wait_for => :popup, :window => "a window id"

    Will wait for a new popup window to appear. Also selects the popup window for you provide :select => true

  • wait :wait_for => :ajax

    Will wait for all ajax requests to be completed using semantics of default javascript framework

  • wait :wait_for => :ajax, :javascript_framework => :jquery

    Will wait for all ajax requests to be completed overriding default javascript framework

  • wait :wait_for => :effects

    Will wait for all javascript effects to be rendered using semantics of default javascript framework

  • wait :wait_for => :effects, :javascript_framework => :prototype

    Will wait for all javascript effects to be rendered overriding default javascript framework

  • wait :wait_for => :element, :element => "new_element_id"

    Will wait for an element to be present/appear

  • wait :wait_for => :no_element, :element => "new_element_id"

    Will wait for an element to be not be present/disappear

  • wait :wait_for => :text, :text => "some text"

    Will wait for some text to be present/appear

  • wait :wait_for => :text, :text => /A Regexp/

    Will wait for some text to be present/appear

  • wait :wait_for => :text, :element => "a_locator", :text => "some text"

    Will wait for the content of "a_locator" to be "some text"

  • wait :wait_for => :no_text, :text => "some text"

    Will wait for the text to be not be present/disappear

  • wait :wait_for => :no_text, :text => /A Regexp/

    Will wait for the text to be not be present/disappear

  • wait :wait_for => :no_text, :element => "a_locator", :text => "some text"

    Will wait for the content of "a_locator" to not be "some text"

  • wait :wait_for => :value, :element => "a_locator", :value => "some value"

    Will wait for the field value of "a_locator" to be "some value"

  • wait :wait_for => :no_value, :element => "a_locator", :value => "some value"

    Will wait for the field value of "a_locator" to not be "some value"

  • wait :wait_for => :condition, :javascript => "some expression"

    Will wait for the javascript expression to be true. Using options you can override the default framework timeout

  • wait_for_condition(script, timeout_in_seconds = nil)

    Runs the specified JavaScript snippet repeatedly until it evaluates to "true"

  • wait_for_page(timeout_in_seconds=nil)

    Waits for a new page to load

  • wait_for_page_to_load(timeout_in_seconds=nil)

    Alias for wait_for_page

  • wait_for_popup(window_id, timeout_in_seconds=nil)

    Waits for a popup window to appear and load

14.2. Quiz answers

  • What do these locators do?

    • ol[id="list"]//li

      all <li> elements one or more levels deep in the <ol id="list"> element

    • ol[id="list"]/*/li

      all <li> element that are grandchildren of <ol>

    • document.forms[0]

      get the first form on the page

    • p.this_class

      The first <p> element that is of the that_class css class

    • div span#thisid

      The <span> element with the id thisid that is inside a div

    • div.sidenav li > a [name=this_name]

      Matches the <a> element that has a name attribute set to this_name when inside a list item grandchildren of the <div> that has the class sidenav

    • /html/body/span[@id="thatspan"]

      the span with the id thatspan

    • //span[@id="thatspan"]

      the same thing!

  • Why use an XPath locator?

    Each time you refresh the page, the id of the Delete link control change. We need to use a locator based on the structure of the page.

14.2.3. Installation

14.2.3.1. Ruby
  • GNU\Linux: use your package manager

    • Ubuntu: aptitude install ruby
  • Windows: install Ruby 1.8 with RubyInstaller

    • available at http://rubyinstaller.org/downloads/
  • MacOS: install ruby with a port manager like MacPorts or Fink
14.2.3.2. Rubygems
  • GNU\Linux/Windows: follow the instructions at http://rubygems.org/pages/download
14.2.3.3. Rspec
  • Use rubygems: gem install rspec
14.2.3.4. Selenium
  • Selenium RC API (Ruby): gem install warnold-selenium-client