在JS中的API测试之间传递变量

时间:2018-08-30 14:19:58

标签: javascript api testing mocha

我有以下问题。 我开始创建e2e api测试 第一次测试的目的是获得未授权的用户令牌,方法标头中第一次测试的第二次测试使用值以及为授权用户返回的令牌-毕竟,第二次令牌将用于进一步的测试。 如何将令牌的值保存在变量中并通过测试?

我是JS新手,现在收到“ ReferenceError:未定义auth_token”

const chai = require('chai');
const request = require('request-promise-native');
const mocha = require('mocha');
const config = require('../config');

const assert = chai.assert;
//const describe = mocha.describe;
//const it = mocha.it;
// request.debug = true;

describe('0_auth', () => {
  it('should return token for unauthorized user', async () => {
  const result = await request({
  headers: config.headers,
  url: `${config.url}/rest/v1/auth/get-token`,
  method: "POST",
  json: {
      "deviceUuidSource": "DEVICE",
      "source" : "KIOSK",
      "deviceUuid" : "uniquedeviceuuid"
  }
});
   assert.isNotNull(result);
   assert.property(result, 'token');
   var auth_token=result.token;
   console.log(auth_token)
   }).timeout(15000);


 it('should return token for authorized user', async () => {
 const result = await request({
  headers: Object.assign(config.headers, { 'Authorization': 'Bearer '+auth_token }),
  url: `${config.url}/rest/v1/auth/with-password`,
  method: "POST",
  json: {
    "email" : "dulexun3hvw0@10minut.xyz",
    "password" : "Test123"
     }
   });
   assert.isNotNull(result);
   assert.property(result, 'token');
   assert.property(result, 'user');
   console.log('token:', result.token);
 }).timeout(15000);
 });

在进一步的测试中,我想将Bearer令牌传递给不同类config.js中的Authorization字段

config.headers = {

 'User-Agent': 'WOR API Tester', // android
  Source: 'MOBILE',
 'Accept-Language': 'EN',
 Authorization:'Bearer '+auth_token;


};
module.exports = config;

1 个答案:

答案 0 :(得分:0)

好的,现在可以接受下一次测试了,但是我有一个问题。 为什么保存在auth_token中的承载可以在除以下类之外的其他测试类中使用,所以我们在这行:

headers: Object.assign(config.headers, { 'Authorization': 'Bearer '+auth_token }),

在其他课程中,我只有以下一行

headers: config.headers,

并且没有关于在授权中使用auth_token的信息,在config.js中也没有关于令牌的信息

const chai = require('chai');
const request = require('request-promise-native');
const mocha = require('mocha');
const config = require('../config');

const assert = chai.assert;
//const describe = mocha.describe;
//const it = mocha.it;
// request.debug = true;

describe('0_auth', () => {
  var auth_token = ''
  it('should return token for unauthorized user', async () => {

const result = await request({
  headers: config.headers,
  url: `${config.url}/rest/v1/auth/get-token`,
  method: "POST",
  json: {
      "deviceUuidSource": "DEVICE",
      "source" : "KIOSK",
      "deviceUuid" : "uniquedeviceuuid"
     }
   });
   assert.isNotNull(result);
   assert.property(result, 'token');
   auth_token=result.token;
}).timeout(15000);


 it('should return token for authorized user', async () => {
const result = await request({
  headers: Object.assign(config.headers, { 'Authorization': 'Bearer '+auth_token }),
  url: `${config.url}/rest/v1/auth/with-password`,
  method: "POST",
  json: {
    "email" : "dulexun3hvw0@10minut.xyz",
    "password" : "Test123"
     }
   });
assert.isNotNull(result);
assert.property(result, 'token');
assert.property(result, 'user');
 }).timeout(15000);
 });
相关问题