Module: WatirRobot::Page

Included in:
KeywordLibrary
Defined in:
lib/watir_robot/keywords/page.rb

Overview

Functionality scoped against the entire HTML page

Instance Method Summary collapse

Instance Method Details

#execute_javascript(code) ⇒ Object

Execute arbitrary JavaScript



70
71
72
# File 'lib/watir_robot/keywords/page.rb', line 70

def execute_javascript(code)
  @browser.execute_script(code)
end

#get_all_elements_by_xpath(xpath, sep = ';;') ⇒ String

Get the text of all elements that match a given XPath query

Parameters:

  • xpath (String)

    the xpath query to use for searching

  • sep (String) (defaults to: ';;')

    the separator that will be used in printing out the results

Returns:

  • (String)

    a string of the text of all the matching elements, separated by sep



59
60
61
62
63
64
65
# File 'lib/watir_robot/keywords/page.rb', line 59

def get_all_elements_by_xpath(xpath, sep = ';;')
  matches = []
  @browser.elements_by_xpath(xpath).each do |element|
    matches << element.text
  end
  return matches.join(sep)
end

#get_page_sourceString

Get source of page

Returns:

  • (String)

    the HTML content of the entire page



22
23
24
# File 'lib/watir_robot/keywords/page.rb', line 22

def get_page_source
  @browser.html
end

#get_page_statusString

Get status of page (located at bottom of browser window)

Returns:

  • (String)

    the status displayed at the bottom of the browser window



40
41
42
# File 'lib/watir_robot/keywords/page.rb', line 40

def get_page_status
  @browser.status
end

#get_page_textString

Get text of a page

Returns:

  • (String)

    teh text content of the entire page, without HTML markup



31
32
33
# File 'lib/watir_robot/keywords/page.rb', line 31

def get_page_text
  @browser.text
end

#get_titleString

Get page title

Returns:

  • (String)

    the title of the page as defined by the <title> tag



48
49
50
# File 'lib/watir_robot/keywords/page.rb', line 48

def get_title
  @browser.title
end

#log_page_sourceObject

Log page source in a readable format



13
14
15
# File 'lib/watir_robot/keywords/page.rb', line 13

def log_page_source
  print @browser.html
end

#page_should_contain(text) ⇒ Object

Verify page text contains a certain value

Parameters:

  • text (String)

    the text to compare against

Raises:



82
83
84
85
# File 'lib/watir_robot/keywords/page.rb', line 82

def page_should_contain(text)
  raise(Exception::PageMatchError, "The expected text #{text} was not contained in the page: #{@browser.text}") unless
    @browser.text.include? text
end

#page_should_contain_area(loc) ⇒ Object

Verify area tag exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



102
103
104
105
# File 'lib/watir_robot/keywords/page.rb', line 102

def page_should_contain_area(loc)
  raise(Exception::ElementDoesNotExist, "The area described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.area(parse_location(loc)).exists?
end

#page_should_contain_button(loc) ⇒ Object

Verify button exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



122
123
124
125
# File 'lib/watir_robot/keywords/page.rb', line 122

def page_should_contain_button(loc)
  raise(Exception::ElementDoesNotExist, "The button described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.button(parse_location(loc)).exists?
end

#page_should_contain_checkbox(loc) ⇒ Object

Verify checkbox exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



142
143
144
145
# File 'lib/watir_robot/keywords/page.rb', line 142

def page_should_contain_checkbox(loc)
  raise(Exception::ElementDoesNotExist, "The checkbox described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.checkbox(parse_location(loc)).exists?
end

#page_should_contain_element(loc) ⇒ Object

Verify element exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



162
163
164
165
# File 'lib/watir_robot/keywords/page.rb', line 162

def page_should_contain_element(loc)
  raise(Exception::ElementDoesNotExist, "The element described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.element(parse_location(loc)).exists?
end

#page_should_contain_form(loc) ⇒ Object

Verify form exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



182
183
184
185
# File 'lib/watir_robot/keywords/page.rb', line 182

def page_should_contain_form(loc)
  raise(Exception::ElementDoesNotExist, "The form described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.form(parse_location(loc)).exists?
end

#page_should_contain_image(loc) ⇒ Object

Verify image exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



202
203
204
205
# File 'lib/watir_robot/keywords/page.rb', line 202

def page_should_contain_image(loc)
  raise(Exception::ElementDoesNotExist, "The image described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.image(parse_location(loc)).exists?
end

Verify link exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



222
223
224
225
# File 'lib/watir_robot/keywords/page.rb', line 222

def page_should_contain_link(loc)
  raise(Exception::ElementDoesNotExist, "The link described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.link(parse_location(loc)).exists?
end

#page_should_contain_list(loc, ordered = nil) ⇒ Object

Verify list exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

  • ordered (String, nil) (defaults to: nil)

    nil means check both ul and ol, true is ol, false is ul; strings for the booleans because the arguments come from Robot Framework



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/watir_robot/keywords/page.rb', line 243

def page_should_contain_list(loc, ordered = nil)
  ordered = ordered.downcase unless ordered.nil?
  if ordered.nil?
    # Not specified; match either
    raise(Exception::ElementDoesNotExist, "The list described by #{loc} is not contained within the page:\n#{@browser.html}") unless
      (@browser.ol(parse_location(loc)).exists? || @browser.ul(parse_location(loc)).exists?)
  elsif ordered == 'true'
    raise(Exception::ElementDoesNotExist, "The ordered list described by #{loc} is not contained within the page:\n#{@browser.html}") unless
      @browser.ol(parse_location(loc)).exists?
  elsif ordered == 'false'
    raise(Exception::ElementDoesNotExist, "The unordered list described by #{loc} is not contained within the page:\n#{@browser.html}") unless
      @browser.ul(parse_location(loc)).exists?
  else
    raise(ArgumentError, "If you specify ordered vs. unordered lists, the only valid values are 'true' or 'false' (case-insensitive)")
  end
end

#page_should_contain_radio_button(loc) ⇒ Object

Verify radio button exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



288
289
290
291
# File 'lib/watir_robot/keywords/page.rb', line 288

def page_should_contain_radio_button(loc)
  raise(Exception::ElementDoesNotExist, "The radio button described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.radio(parse_location(loc)).exists?
end

#page_should_contain_select_list(loc) ⇒ Object

Verify select list does exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



308
309
310
311
# File 'lib/watir_robot/keywords/page.rb', line 308

def page_should_contain_select_list(loc)
  raise(Exception::ElementDoesNotExist, "The select list described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.select(parse_location(loc)).exists?
end

#page_should_contain_textfield(loc) ⇒ Object

Verify link exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



328
329
330
331
# File 'lib/watir_robot/keywords/page.rb', line 328

def page_should_contain_textfield(loc)
  raise(Exception::ElementDoesNotExist, "The text field described by #{loc} is not contained within the page:\n#{@browser.html}") unless
    @browser.text_field(parse_location(loc)).exists?
end

#page_should_not_contain(text) ⇒ Object

Verify page text does not contain a certain value

Parameters:

  • text (String)

    the text to compare against

Raises:



92
93
94
95
# File 'lib/watir_robot/keywords/page.rb', line 92

def page_should_not_contain(text)
  raise(Exception::PageMatchError, "The expected text #{text} was contained in the page erroneously: #{@browser.text}") if
    @browser.text.include? text
end

#page_should_not_contain_area(loc) ⇒ Object

Verify area tag exists on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



112
113
114
115
# File 'lib/watir_robot/keywords/page.rb', line 112

def page_should_not_contain_area(loc)
  raise(Exception::ElementExists, "The area described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.area(parse_location(loc)).exists?
end

#page_should_not_contain_button(loc) ⇒ Object

Verify button does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



132
133
134
135
# File 'lib/watir_robot/keywords/page.rb', line 132

def page_should_not_contain_button(loc)
  raise(Exception::ElementExists, "The button described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.button(parse_location(loc)).exists?
end

#page_should_not_contain_checkbox(loc) ⇒ Object

Verify checkbox does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



152
153
154
155
# File 'lib/watir_robot/keywords/page.rb', line 152

def page_should_not_contain_checkbox(loc)
  raise(Exception::ElementExists, "The checkbox described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.checkbox(parse_location(loc)).exists?
end

#page_should_not_contain_element(loc) ⇒ Object

Verify element does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



172
173
174
175
# File 'lib/watir_robot/keywords/page.rb', line 172

def page_should_not_contain_element(loc)
  raise(Exception::ElementExists, "The element described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.element(parse_location(loc)).exists?
end

#page_should_not_contain_form(loc) ⇒ Object

Verify form does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



192
193
194
195
# File 'lib/watir_robot/keywords/page.rb', line 192

def page_should_not_contain_form(loc)
  raise(Exception::ElementExists, "The form described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.form(parse_location(loc)).exists?
end

#page_should_not_contain_image(loc) ⇒ Object

Verify image does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



212
213
214
215
# File 'lib/watir_robot/keywords/page.rb', line 212

def page_should_not_contain_image(loc)
  raise(Exception::ElementExists, "The image described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.image(parse_location(loc)).exists?
end

Verify link does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



232
233
234
235
# File 'lib/watir_robot/keywords/page.rb', line 232

def page_should_not_contain_link(loc)
  raise(Exception::ElementExists, "The link described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.link(parse_location(loc)).exists?
end

#page_should_not_contain_list(loc, ordered = nil) ⇒ Object

Verify list does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

  • ordered (String, nil) (defaults to: nil)

    nil means check both ul and ol, true is ol, false is ul; strings for the booleans because the arguments come from Robot Framework



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/watir_robot/keywords/page.rb', line 266

def page_should_not_contain_list(loc, ordered = nil)
  ordered = ordered.downcase unless ordered == nil
  if ordered.nil?
    # Not specified; match either
    raise(Exception::ElementExists, "The list described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
      (@browser.ol(parse_location(loc)).exists? || @browser.ul(parse_location(loc)).exists?)
  elsif ordered == 'true'
    raise(Exception::ElementExists, "The ordered list described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
      @browser.ol(parse_location(loc)).exists?
  elsif ordered == 'false'
    raise(Exception::ElementExists, "The unordered list described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
      @browser.ul(parse_location(loc)).exists?
  else
    raise(ArgumentError, "If you specify ordered vs. unordered lists, the only valid values are 'true' or 'false' (case-insensitive)")
  end
end

#page_should_not_contain_radio_button(loc) ⇒ Object

Verify radio button does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



298
299
300
301
# File 'lib/watir_robot/keywords/page.rb', line 298

def page_should_not_contain_radio_button(loc)
  raise(Exception::ElementExists, "The radio button described by #{loc} is erroneously contained within the page:\n#{@browser.html}") if
    @browser.radio(parse_location(loc)).exists?
end

#page_should_not_contain_select_list(loc) ⇒ Object

Verify select list does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



318
319
320
321
# File 'lib/watir_robot/keywords/page.rb', line 318

def page_should_not_contain_select_list(loc)
  raise(Exception::ElementExists, "The select list described by #{loc} is not contained within the page erroneously:\n#{@browser.html}") if
    @browser.select(parse_location(loc)).exists?
end

#page_should_not_contain_textfield(loc) ⇒ Object

Verify link does not exist on page

Parameters:

  • loc (String)

    attribute/value pairs that match an HTML element

Raises:



338
339
340
341
# File 'lib/watir_robot/keywords/page.rb', line 338

def page_should_not_contain_textfield(loc)
  raise(Exception::ElementExists, "The text field described by #{loc} is not contained within the page erroneously:\n#{@browser.html}") if
    @browser.text_field(parse_location(loc)).exists?
end

#title_should_be(title) ⇒ Object

Verify page title, i.e. the <title> in <head>

Parameters:

  • title (String)

    the title text to compare against

Raises:



348
349
350
# File 'lib/watir_robot/keywords/page.rb', line 348

def title_should_be(title)
  raise(Exception::TitleMatchError, "The page title #{@browser.title} is not correct; it should be #{title}") unless @browser.title == title
end

#title_should_contain(text) ⇒ Object

Verify that the page title contains a certain value

Parameters:

  • text (String)

    the text to compare against

Raises:



357
358
359
# File 'lib/watir_robot/keywords/page.rb', line 357

def title_should_contain(text)
  raise(Exception::TitleMatchError, "The page title #{@browser.url} is not correct; it should contain #{text}") unless @browser.title.include?(text)
end