与量角器的异步请求功能

时间:2017-06-20 16:18:02

标签: javascript async-await protractor

  • 节点版本: v6.10.3 ;
  • 量角器版本:版本5.1.2 ;
  • 浏览器: IE ;
  • 操作系统和版本: Windows 10 ;
  • 描述:我正在使用Protractor测试Angular页面。但是,在测试期间,我还需要使用POST(API REST)验证请求。但是,我需要获取tokenId以便稍后使用令牌身份验证发送GET

所以,我有以下代码:

protractor.conf.js

exports.config = {
  framework: 'mocha',
  frameworkPath: require.resolve('serenity-js'),
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['vmiRest.js'],
  capabilities: {
    browserName: 'internet explorer',
    chromeOptions : {
      args : ["incognito"],
      prefs: {"profile.managed_default_content_settings.images": 2}
    }
  },
  mochaOpts: {
    reporter: "spec",
    slow: 12000,
    timeout: 20000,
    ui: 'bdd'
  },
  serenity: {
    dialect: 'mocha'
  }
}

vmiRest.js

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var assert = chai.assert;
var expect = chai.expect;
chai.use(chaiAsPromised);
var SecurityApi = require(__dirname + '/common/restSecurityApi.js');
var Parts = require(__dirname + '/common/restParts.js');
describe('VMI - API', function() {
  var tokenId = 'none';
  var username = 'bla';
  var password = '*';
  var dealer = '112571';
  var part = '0420665R1';
beforeEach(function () {
    //
  });
it('Get Token from SecurityApi', function() {
    //
    var restSecurityApi= new SecurityApi(username, password);
    restSecurityApi.getToken(function (tokenId) {
      assert.notEqual(tokenId, null);
      console.log(tokenId);
    });
  });
  it('Test Interface getStockFullUsingGET', function() {
    console.log(tokenId);
  });
});

restSecurityApi.js

var request = require('request');
var SecurityApi = function (username, password) {
  this.getToken = function (callback) {
    var headers = {
      'X-OpenAM-Username': username,
      'X-OpenAM-Password': password,
      'Content-Type': 'application/json'
    }
    // Configure the request
    var options = {
        url: 'https://appsqa.host/SecurityApi/Token',
        method: 'POST',
        headers: headers
    }
    // Start the request
    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var json = JSON.parse(body);
            var tokenId = json["tokenId"];
            callback(tokenId);
        }  else {
            callback(null);
        }
    })
  };
};
module.exports = SecurityApi;

在执行期间,请求函数在测试结束时返回结果/响应 LATE (async)!那么,我如何执行请求同步或尊重执行顺序?

我使用了承诺,asyn / await,但没有任何作用!拜托,我需要帮助!非常感谢你!

:我还测试了以下内容:

vmiRest.js

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
var assert = chai.assert;
var expect = chai.expect;
chai.use(chaiAsPromised);
var SecurityApi = require(__dirname + '/common/restSecurityApi.js');
var Parts = require(__dirname + '/common/restParts.js');
describe('AGCOnline - VMI - API', function() {
  browser.ignoreSynchronization = true;
  browser.waitForAngularEnabled(false);
  var tokenId = 'none';
  var username = 'user';
  var password = '*';
  var dealer = '11571';
  var part = '040665R1';
  it('Get Token from SecurityApi', function() {
    tokenId = SecurityApi.getToken(username, password);
    console.log(tokenId);
  });
  it('Test Interface getStockFullUsingGET (BKW11)', function() {
    console.log(tokenId);
  });
});

restSecurityApi.js

var request = require('request');
exports.getToken = function (username, password) {
  var headers = {
    'X-OpenAM-Username': username,
    'X-OpenAM-Password': password,
    'Content-Type': 'application/json'
  }
  // Configure the request
  var options = {
    url: 'https://appsqa.agcoonline.com.br/SecurityApi/Token',
    method: 'POST',
    headers: headers
  }
  // Start the request
  request(options, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var json = JSON.parse(body);
      var tokenId = json["tokenId"];
        console.log(tokenId);
        return tokenId;
      }
    else {
      return null;
    }
  })
};

即便如此,同样的问题!

0 个答案:

没有答案