1.World:
World can be seen as an instance of the object that Cucumber creates before running each scenario. It not only enables every Step Definition to call the method of this instance, in addition, the Ruby class defined for this project can also call the method of this instance. World is defined as a method in Cucumber. It accepts the variable parameter mudules and a proc:
1 def World(*world_modules, &proc)2
2 RbDsl.build_rb_world_factory(world_modules, proc)
3 end
The following is a small example written by myself:
The directory structure is as follows:
Env. rb:This file is usually used to prepare environment variables. Here I load three files.
1 require 'rubygems'
2 require 'watir-webdriver'
3 require 'rspec'
World. rb
1 module MyModule
2
3 def self.foo(name)
4 puts "MyModule_FOO: #{name} "
5 end
6
7 def boo(key)
8 puts "MyModule_BOO: #{key}!"
9 end
10
11 end
12
13 World(MyModule)
This file defines a module, which defines two methods: an instance method boo and a class method foo. Finally, pass the defined module to World. in this way, when running feature, you can call the methods in this module elsewhere (step_definitions or custom ruby class files.
Test. feature
1 Feature:Test
2 In order to test how to use world
3 as a learner
4 I want to practice to use world
5
6 Scenario: practice to use hooks
7 Given try to use the cucumber keyword
In this feature file, I have defined only one scenario, and this scenario contains only one step Given. Because this small example is used to illustrate the usage of World, we will not discuss feature too much here.
Test. rb
1 Given /^try to use the cucumber keyword$/ do
2 @page = Home.new
3 @page.visit!
4 MyModule.foo("*step_definitions*")
5 end
In this step_definitions file, it is the specific operation implementation in the feature file. Because we all know that the feature file only has some descriptions, and the specific execution is put in the file under step _ definitions. In this file, I added a Home file (see the definition of the Home file below) and called the visit defined in the Home file! Method. In the last line, I directly called the module method foo defined in the world. rb file (because foo is a class method, it can be called directly. The boo method is an instance method, and an object must be instantiated before calling ).
Home. rb
1 class Home
2
3 def visit!
4 MyModule.foo("*Home_page_module*")
5 end
6
7 end
This file is a ruby class file that I have defined for this project. It defines a visit! This method also calls the class method foo in the world. rb file.
As mentioned at the beginning of this article, World not only enables Step Definition to call the method of this instance, but also enables the Ruby class defined for this project to call the method of this instance. In this case, the program will normally output these two sentences respectively.
Result:
1 C: \ Ruby192 \ bin \ ruby.exe-e $ stdout. sync = true; $ stderr. sync = true; load ($0 = ARGV. shift) C: \ Ruby192 \ bin/cucumber D:/cucumber/mycucumber/features/test. feature -- format Teamcity: Cucumber: Formatter -- expand -- color-r features
2 Testing started at a.m...
3 MyModule_FOO: * Home_page_module *
4 MyModule_FOO: * step_definitions *
5 1 scenario (1 passed)
6 1 step (1 passed)
7 0m0. 000 s
Lines 3 and 4 in the result are consistent with our expectation.