World对象

对于每个场景,都有一个独立的上下文叫World, 在hooks和步骤的运行函数中用this来访问.默认的 world 构造函数是:

  1. function World({attach, parameters}) {
  2. this.attach = attach
  3. this.parameters = parameters
  4. }
  • attach: 用于将附件添加到hooks/步骤的函数
  • parameters: 通过命令行传入的参数对象

默认的对象可以被setWorldConstructor覆盖.

  1. var {setWorldConstructor} = require('cucumber');
  2. var seleniumWebdriver = require('selenium-webdriver');
  3. function CustomWorld() {
  4. this.driver = new seleniumWebdriver.Builder()
  5. .forBrowser('firefox')
  6. .build();
  7. // Returns a promise that resolves to the element
  8. this.waitForElement = function(locator) {
  9. var condition = seleniumWebdriver.until.elementLocated(locator);
  10. return this.driver.wait(condition)
  11. }
  12. }
  13. setWorldConstructor(CustomWorld)

注意: World构造函数在v0.8.0后是严格同步的.