Class: Waw::WSpec::DSL

Inherits:
Object show all
Includes:
Test::Unit::Assertions, ScopeUtils, HTMLAnalysis
Defined in:
lib/waw/wspec/dsl.rb

Overview

Provides the DSL of the .wspec scenario language.

Instance Method Summary collapse

Methods included from HTMLAnalysis

#all_external_links, #all_internal_links, #all_links, #all_tags, #decode_attributes_string, #each_tag, #first_link, #first_tag, #form, #has_link?, #has_tag?, #i_see?

Methods included from ScopeUtils

#config, #find_kernel_context, #logger, #params, #rack_env, #real_session, #request, #resources, #response, #root_folder, #session

Constructor Details

#initialize(waw_kernel, scenario) ⇒ DSL

Creates a DSL instance for a given scenario



13
14
15
16
17
# File 'lib/waw/wspec/dsl.rb', line 13

def initialize(waw_kernel, scenario)
  @waw_kernel = waw_kernel
  @scenario = scenario
  @browser = Browser.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Delegates to waw ressources



50
51
52
53
54
55
56
# File 'lib/waw/wspec/dsl.rb', line 50

def method_missing(name, *args, &block)
  if args.empty? and resources and resources.has_resource?(name)
    resources[name]
  else
    super(name, *args, &block)
  end
end

Instance Method Details

#__execute(&block) ⇒ Object Also known as: by_the_way

Executes some code



27
28
29
# File 'lib/waw/wspec/dsl.rb', line 27

def __execute(&block)
  self.instance_eval(&block)
end

#__last_becauseObject

Returns the last because clause



38
39
40
# File 'lib/waw/wspec/dsl.rb', line 38

def __last_because
  __stack.last || @scenario.name
end

#__stackObject

A stack of execution context



33
34
35
# File 'lib/waw/wspec/dsl.rb', line 33

def __stack
  @stack ||= []
end

#add_assertionObject

Adds an assertion



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

def add_assertion
  @scenario.add_assertion
end

#because(msg = "Unknown cause") {|browser| ... } ⇒ Object Also known as: therefore

Because some condition holds…

Yields:

Raises:

  • (ArgumentError)


91
92
93
94
95
96
# File 'lib/waw/wspec/dsl.rb', line 91

def because(msg="Unknown cause", &block)
  raise ArgumentError, "WSpec because/therefore expects a block" unless block
  __stack.push(msg)
  yield(browser)
  __stack.pop
end

#browserObject

Returns the current browser instance



61
62
63
# File 'lib/waw/wspec/dsl.rb', line 61

def browser
  @browser
end

#browser_contentsObject Also known as: contents

Returns current browser contents (used by HTMLAnalysis)



66
67
68
# File 'lib/waw/wspec/dsl.rb', line 66

def browser_contents
  browser.contents
end

#go(location) ⇒ Object

Goes to a given location and returns the HTTPResponse object



78
79
80
81
# File 'lib/waw/wspec/dsl.rb', line 78

def go(location)
  browser.location = location
  browser.response
end

#i_could_reach(which_page) ⇒ Object

Asserts that a page could be reached (requesting headers leads to a Net::HTTPSucess result)



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/waw/wspec/dsl.rb', line 108

def i_could_reach(which_page)
  return if which_page.nil? or '#'==which_page 
  case which_page
    when Tag
      i_could_reach(which_page[:href])
    when Array
      which_page.each{|link| i_could_reach(link)}
    when String
      result = browser.headers_fetch(which_page)
      assert Net::HTTPSuccess===result, __last_because + " (could not reach #{which_page}: #{result})"
    else
      raise ArgumentError, "Unable to use #{which_page} for i_could_reach"
  end
end

#i_dont_reach(which_page) ⇒ Object

Asserts that a page cannot be reached, leading to a Net::HTTPNotFound or a Net::HTTPForbidden (403) result



125
126
127
128
# File 'lib/waw/wspec/dsl.rb', line 125

def i_dont_reach(which_page)
  result = go(which_page)
  assert (Net::HTTPNotFound===result or Net::HTTPForbidden===result), __last_because + " (can reach #{which_page}: #{result})"
end

#i_dont_see(what) ⇒ Object

Asserts that something is not present on the current page



144
145
146
# File 'lib/waw/wspec/dsl.rb', line 144

def i_dont_see(what)
  assert !i_see?(what), __last_because + " (actually see #{what})"
end

#i_follow(link) ⇒ Object

Following a link



161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/waw/wspec/dsl.rb', line 161

def i_follow(link)
  link = case link
    when String
      link
    when Tag
      link[:href]
    when ::Waw::ActionController::Action
      link.href
    else
      raise ArgumentError, "Unable to apply i_follow to #{link.inspect}"
  end
  i_reach(link)
end

#i_invoke(action, args = {}) ⇒ Object

Directly invoke an action or service server side, bypassing form lookup and so on.



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/waw/wspec/dsl.rb', line 186

def i_invoke(action, args = {})
  result = case action
    when String
      browser.invoke_service(action, args)
    when ::Waw::ActionController::Action
      browser.invoke_action(action, args)
    else
      raise ArgumentError, "Unable to apply i_invoke on #{action.inspect}, unable to catch the associated action"
  end
  assert Net::HTTPSuccess===result, __last_because + " (invoking #{action.inspect} led to: #{result})"
  result
end

#i_may_not_reach(which_page) ⇒ Object

Asserts that a page may not be reached, leading to a Net::HTTPForbidden (403) result.



131
132
133
134
# File 'lib/waw/wspec/dsl.rb', line 131

def i_may_not_reach(which_page)
  result = go(which_page)
  assert (Net::HTTPForbidden===result or Net::HTTPBadRequest===result), __last_because + " (may actually reach #{which_page}: #{result})"
end

#i_reach(which_page) ⇒ Object

Asserts that a page can be reached, leading to a Net::HTTPSuccess result



102
103
104
105
# File 'lib/waw/wspec/dsl.rb', line 102

def i_reach(which_page)
  result = go(which_page)
  assert Net::HTTPSuccess===result, __last_because + " (cannot actually reach #{which_page}: #{result})"
end

#i_see(what) ⇒ Object

Asserts that something can be seen on the current page



139
140
141
# File 'lib/waw/wspec/dsl.rb', line 139

def i_see(what)
  assert i_see?(what), __last_because + " (don't see #{what})"
end

Asserts that I see a particular link (see HTMLAnalysis.link)



154
155
156
# File 'lib/waw/wspec/dsl.rb', line 154

def i_see_link(opts = nil)
  assert has_link?(opts), __last_because + " (dont see link <a #{opts.inspect})"
end

#i_see_tag(name, opts = nil) ⇒ Object

Asserts that I see a particular tag (see HTMLAnalysis.tag)



149
150
151
# File 'lib/waw/wspec/dsl.rb', line 149

def i_see_tag(name, opts = nil)
  assert has_tag?(name, opts), __last_because + " (don't see tag <#{name} #{opts.inspect})"
end

#i_submit(form, args = {}) ⇒ Object

Submits some form with optional arguments

Raises:

  • (ArgumentError)


178
179
180
181
182
# File 'lib/waw/wspec/dsl.rb', line 178

def i_submit(form, args = {})
  assert_not_nil form, __last_because + "(form has not been found)"
  raise ArgumentError, "i_submit requires a Tag instance, #{form.inspect} received" unless Tag===form
  i_invoke(form[:action], args)
end

#index_pageObject

Returns the URL of the index page (the web_base actually) This method returns nil unless the Waw application has been loaded



73
74
75
# File 'lib/waw/wspec/dsl.rb', line 73

def index_page
  config && config.web_base
end

#waw_kernelObject

Returns waw kernel



20
21
22
# File 'lib/waw/wspec/dsl.rb', line 20

def waw_kernel
  @waw_kernel
end

#with(args = {}) ⇒ Object

Simply returns args



84
85
86
# File 'lib/waw/wspec/dsl.rb', line 84

def with(args = {})
  args
end