无法访问Hook中的场景对象

时间:2017-08-18 08:51:06

标签: javascript selenium cucumber

尝试通过访问方案对象并查看.isSuccessful()是真还是假来截断我的任何步骤失败的时间。在我的hooks.js文件中执行此操作:

After(function (scenario) {
        var status = scenario.isSuccessful();
        if (!status)
          driver.sleep(3000);
        TakeScreenshot('./test_artifacts/img/failures/', scenario.getName(), driver);
        //prints browser console logs
        driver.manage().logs().get('browser').then(function (logs) {
          fs.writeFile('./test_artifacts/logs/' + scenario.getName() + '.txt', stringify(logs), function (err) {
            if (err)
              return console.log(err);
          });
        })
        return driver.quit();
      });

但是,我目前收到以下错误:

TypeError: Cannot read property 'isSuccessful' of undefined

知道发生了什么事吗?我的step_definition文件工作正常,我的.feature文件也是如此。

编辑:这是我的hooks.js文件:

var selenium = require('selenium-webdriver');
var phantomjs = require('phantomjs');
var sleep = require('system-sleep');
var By = selenium.By;
var fs = require('fs');
var seleniumTimeOut = 20 * 1000;
var childProcess = require('child_process');
var stringify = require('node-stringify');
var Key = require('../../node_modules/selenium-webdriver/lib/input');
var { defineSupportCode } = require('cucumber');
require('chromedriver');

function TakeScreenshot(path, imgName, driver) {
  driver.takeScreenshot().then(
    function (image, err) {
      fs.writeFileSync(path + imgName + (new Date).getTime().toString() + '.png', image, 'base64');
    });
};

defineSupportCode(function ({ After, Before, setDefaultTimeout }) {

  setDefaultTimeout(60 * 1000);

  After(function (scenario) {
    var status = scenario.isSuccessful();
    if (!status)
      driver.sleep(3000);
    TakeScreenshot('./test_artifacts/img/failures/', scenario.getName(), driver);
    //prints browser console logs
    driver.manage().logs().get('browser').then(function (logs) {
      fs.writeFile('./test_artifacts/logs/' + scenario.getName() + '.txt', stringify(logs), function (err) {
        if (err)
          return console.log(err);
      });
    })
    return driver.quit();
  });

  Before(function () {

    //set up driver instance
    driver = {};
    driver = new selenium.Builder()
      .withCapabilities({ 'phantomjs.cli.args': ['--ignore-ssl-errors=true'] })
      .forBrowser('phantomjs')
      .build();
    driver.manage().timeouts().implicitlyWait(10000);
    driver.manage().window().setSize(1674, 1407);

    //clear local storage before tests
    var localStoragePath = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'] + '\\AppData\\Local\\Ofi Labs\\PhantomJS\\';
    fs.readdir(localStoragePath, function (err, items) {

      if (items) {
        for (var i = 0; i < items.length; i++) {

          var file = localStoragePath + '/' + items[i];

          try {
            fs.writeFileSync(file, '');
            console.log('Truncated file: ' + file);
          } catch (err) {
            console.log('Failed to truncate file: ' + file);
          }
        }
      }
    });

  //log in to site to retrieve access token before feature

    driver.get("https://localhost:8000/portal/login");

    sleep(4000);

    driver.findElement(By.css('#app > div > div > div.col-xs-6.textColumn > button')).click();

    sleep(4000);

    TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

    driver.findElement(By.css('#auth0-lock-container-1 > div > div.auth0-lock-center > form > div > div > div:nth-child(3) > span > div > div > div > div > div > div > div > div > div > div.auth0-lock-social-buttons-container > button > div.auth0-lock-social-button-text')).click();

    TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

    sleep(3000);

    var input1 = driver.findElement(By.css('#userId'));
    var input2 = driver.findElement(By.css('#password'));

    input1.sendKeys('utenza_reseller');
    input2.sendKeys('reseller2015');

    TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

    driver.findElement(By.css('#doLoginOAuth')).click();

    TakeScreenshot('./test_artifacts/img/', 'Load Success', driver);

  });

});

1 个答案:

答案 0 :(得分:0)

感谢用户Chuan Qin对cuc-js Gitter的回答如下:

从V 3.0.0恢复到Cucumber的V 2.0.0。 isSuccessful仍然无法正常工作,但isFailed确实有效。我现在似乎可以访问方案对象,而它在3.0.0中未定义