Module: Cucumber::RbSupport::RbWorld

Defined in:
lib/cucumber/rb_support/rb_world.rb

Overview

Defines the basic DSL methods availlable in all Cucumber step definitions.

You can, and probably should, extend this DSL with your own methods that make sense in your domain. For more on that, see Cucumber::RbSupport::RbDsl#World

Instance Method Summary collapse

Instance Method Details

#announce(*messages) ⇒ Object

Deprecated.

Use #puts instead.



94
95
96
97
# File 'lib/cucumber/rb_support/rb_world.rb', line 94

def announce(*messages)
  STDERR.puts AnsiEscapes.failed + "WARNING: #announce is deprecated. Use #puts instead:" + caller[0] + AnsiEscapes.reset
  puts(*messages)
end

#ask(question, timeout_seconds = 60) ⇒ Object

Pause the tests and ask the operator for input



111
112
113
# File 'lib/cucumber/rb_support/rb_world.rb', line 111

def ask(question, timeout_seconds=60)
  @__cucumber_runtime.ask(question, timeout_seconds)
end

#doc_string(string_without_triple_quotes, content_type = '', line_offset = 0) ⇒ Object

Create an Ast::DocString object

Useful in conjunction with the #step method, when want to specify a content type.

Examples:

Create a multiline string

code = multiline_string(%{
  puts "this is ruby code"
%}, 'ruby')


88
89
90
91
# File 'lib/cucumber/rb_support/rb_world.rb', line 88

def doc_string(string_without_triple_quotes, content_type='', line_offset=0)
  # TODO: rename this method to multiline_string
  @__cucumber_runtime.doc_string(string_without_triple_quotes, content_type, line_offset)
end

#embed(file, mime_type, label = 'Screenshot') ⇒ Object

Embed an image in the output



116
117
118
# File 'lib/cucumber/rb_support/rb_world.rb', line 116

def embed(file, mime_type, label='Screenshot')
  @__cucumber_runtime.embed(file, mime_type, label)
end

#inspectObject

Prints the list of modules that are included in the World



135
136
137
138
139
140
141
# File 'lib/cucumber/rb_support/rb_world.rb', line 135

def inspect
  modules = [self.class]
  (class << self; self; end).instance_eval do
    modules += included_modules
  end
  sprintf("#<%s:0x%x>", modules.join('+'), self.object_id)
end

#pending(message = "TODO") ⇒ Object

Mark the matched step as pending.



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cucumber/rb_support/rb_world.rb', line 121

def pending(message = "TODO")
  if block_given?
    begin
      yield
    rescue Exception
      raise Pending.new(message)
    end
    raise Pending.new("Expected pending '#{message}' to fail. No Error was raised. No longer pending?")
  else
    raise Pending.new(message)
  end
end

#puts(*messages) ⇒ Object

Note:

Cucumber might surprise you with the behaviour of this method. Instead of sending the output directly to STDOUT, Cucumber will intercept and cache the message until the current step has finished, and then display it.

If you’d prefer to see the message immediately, call Kernel#puts instead.

Print a message to the output.



106
107
108
# File 'lib/cucumber/rb_support/rb_world.rb', line 106

def puts(*messages)
  @__cucumber_runtime.puts(*messages)
end

#step(name, multiline_argument = nil) ⇒ Object

Run a single Gherkin step

Examples:

Call another step

step "I am logged in"

Call a step with quotes in the name

step %{the user "Dave" is logged in}

Passing a table

step "the following users exist:", table(%{
  | name  | email           |
  | Matt  | [email protected]   |
  | Aslak | [email protected] |
})

Passing a multiline string

step "the email should contain:", "Dear sir,\nYou've won a prize!\n"

Parameters:



51
52
53
# File 'lib/cucumber/rb_support/rb_world.rb', line 51

def step(name, multiline_argument=nil)
  @__cucumber_runtime.invoke(name, multiline_argument)
end

#steps(steps_text) ⇒ Object

Run a snippet of Gherkin

Examples:

steps %{
  Given the user "Susan" exists
  And I am logged in as "Susan"
}

Parameters:

  • steps_text (String)

    The Gherkin snippet to run



62
63
64
# File 'lib/cucumber/rb_support/rb_world.rb', line 62

def steps(steps_text)
  @__cucumber_runtime.invoke_steps(steps_text, @__natural_language, caller[0])
end

#table(text_or_table, file = nil, line_offset = 0) ⇒ Object

Parse Gherkin into a Ast::Table object.

Useful in conjunction with the #step method.

Examples:

Create a table

users = table(%{
  | name  | email           |
  | Matt  | [email protected]   |
  | Aslak | [email protected] |
})

Parameters:

  • text_or_table (String)

    The Gherkin string that represents the table



76
77
78
# File 'lib/cucumber/rb_support/rb_world.rb', line 76

def table(text_or_table, file=nil, line_offset=0)
  @__cucumber_runtime.table(text_or_table, file, line_offset)
end

#to_sObject

see #inspect



144
145
146
# File 'lib/cucumber/rb_support/rb_world.rb', line 144

def to_s
  inspect
end

#Transform(arg) ⇒ Object

Call a Transform with a string from another Transform definition



22
23
24
25
# File 'lib/cucumber/rb_support/rb_world.rb', line 22

def Transform(arg)
  rb = @__cucumber_runtime.load_programming_language('rb')
  rb.execute_transforms([arg]).first
end