Module: Waw::WSpec::HTMLAnalysis

Included in:
Browser, DSL
Defined in:
lib/waw/wspec/html_analysis.rb,
lib/waw/wspec/html_analysis/tag.rb

Overview

Provides utility methods for analysis of XML/HTML contents.

This module expects an accessor to a browser instance. Otherwise, the browser_contents method may be overrided.

Defined Under Namespace

Classes: Tag

Instance Method Summary collapse

Instance Method Details

Same as all_links, but retains external links only (i.e. URI::Generic.absolute?)



80
81
82
83
84
85
86
87
88
# File 'lib/waw/wspec/html_analysis.rb', line 80

def all_external_links(opts = nil, contents = browser_contents, &block)
  if block
    all_links(opts, contents).each do |link|
      yield(link) if URI.parse(link[:href]).absolute?
    end
  else
    all_links(opts, contents).select{|link| URI.parse(link[:href]).absolute?}
  end
end

Same as all_links, but retains internal links only (i.e. not(URI::Generic.absolute?))



68
69
70
71
72
73
74
75
76
# File 'lib/waw/wspec/html_analysis.rb', line 68

def all_internal_links(opts = nil, contents = browser_contents, &block)
  if block
    all_links(opts, contents).each do |link|
      yield(link) unless URI.parse(link[:href]).absolute?
    end
  else
    all_links(opts, contents).reject{|link| URI.parse(link[:href]).absolute?}
  end
end

Shortcut for tags('a', {:href => /^(.*?)$/}.merge(opts), contents, &block)



62
63
64
# File 'lib/waw/wspec/html_analysis.rb', line 62

def all_links(opts = nil, contents = browser_contents, &block)
  tags('a', {:href => /^(.*?)$/}.merge(opts || {}), contents, &block)
end

#all_tags(name, opts = nil, contents = browser_contents) ⇒ Object Also known as: tags

Find tags inside the browser contents. If a block is given, yield it with each tag information. Otherwise, returns an array of found tags, that can be empty.



31
32
33
34
35
36
37
38
39
# File 'lib/waw/wspec/html_analysis.rb', line 31

def all_tags(name, opts = nil, contents = browser_contents)
  found = [] unless block_given?
  contents.scan(/(<\s*#{name}\s*(.*?)\/?>)/) do |match|
    tag = Tag.new(match[0], name, decode_attributes_string(match[1]))
    next unless tag.matches?(opts)
    block_given? ? yield(tag) : (found << tag)
  end
  found
end

#browser_contentsObject

Yields the block passing browser contents as first argument



13
14
15
# File 'lib/waw/wspec/html_analysis.rb', line 13

def browser_contents
  browser.contents
end

#decode_attributes_string(str) ⇒ Object

Decodes a string of HTML attributes as a hash with symbols as keys



18
19
20
21
22
23
24
# File 'lib/waw/wspec/html_analysis.rb', line 18

def decode_attributes_string(str)
  attrs = {}
  str.scan(/([a-z]+)=["'](.*?)["']/) do |match|
    attrs[match[0].to_sym] = match[1]
  end
  attrs
end

#each_tag(name, opts = nil, contents = browser_contents, &block) ⇒ Object

Iterates over a tag specification



43
44
45
# File 'lib/waw/wspec/html_analysis.rb', line 43

def each_tag(name, opts = nil, contents = browser_contents, &block)
  tags(name, opts, contents, &block)
end

Shortcut for links(name, opts, contents)[0]. Returns nil if no such link can be found



93
94
95
# File 'lib/waw/wspec/html_analysis.rb', line 93

def first_link(opts = nil, contents = browser_contents)
  links(opts, contents)[0]
end

#first_tag(name, opts = nil, contents = browser_contents) ⇒ Object Also known as: tag

Shortcut for tags(name, opts, contents)[0]. Returns nil if no such tag can be found



49
50
51
# File 'lib/waw/wspec/html_analysis.rb', line 49

def first_tag(name, opts = nil, contents = browser_contents)
  tags(name, opts, contents)[0]
end

#form(opts = nil, contents = browser_contents) ⇒ Object

Shortcut for tag('form', opts, contents)



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/waw/wspec/html_analysis.rb', line 106

def form(opts = nil, contents = browser_contents)
  if opts[:action] and ::Waw::ActionController::Action===opts[:action]
    action = opts[:action]
    opts = {:id => action.id}.merge(opts || {}).forget(:action)
    form = tag('form', opts, contents)
    form[:action] = action if form
    form
  else
    tag('form', opts, contents)
  end
end

#has_link?(opts = nil, contents = browser_contents) ⇒ Boolean

Checks if some link can be found

Returns:



99
100
101
# File 'lib/waw/wspec/html_analysis.rb', line 99

def has_link?(opts = nil, contents = browser_contents)
  return links(opts, contents).size != 0
end

#has_tag?(name, opts = nil, contents = browser_contents) ⇒ Boolean

Look for some html tag

Returns:



55
56
57
# File 'lib/waw/wspec/html_analysis.rb', line 55

def has_tag?(name, opts = nil, contents = browser_contents)
  return tags(name, opts, contents).size != 0
end

#i_see?(what, contents = browser_contents) ⇒ Boolean

Assert that the user sees something in the browser contents

Returns:



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/waw/wspec/html_analysis.rb', line 121

def i_see?(what, contents = browser_contents)
  case what
    when NilClass
      false
    when String, Regexp
      not(contents.index(what).nil?)
    when Tag
      true
    else
      raise ArgumentError, "Unable to see #{what}, not understood"
  end
end