gpt4 book ai didi

javascript - 在 mocha 中将变量从 before() 传递到 it()

转载 作者:行者123 更新时间:2023-12-05 09:27:22 25 4
gpt4 key购买 nike

自动化的新手,我正在尝试将 token 值从 block() 之前传递到 it() 并且我变得不确定。

能够生成成功并保存在token

const { spec } = require('pactum');
const request = require('request');
const userConfig = require('../config/credential.json')

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

let token = "";
let authToken = "";

describe('POST /jobs', function () {
before(function () {
var option = {
'method': 'POST',
'url': 'https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'client_id': `${userConfig.client_id}`,
'grant_type': 'password',
'Password': `${userConfig.Password}`,
'UserName': `${userConfig.UserName}`,
'scope': `${userConfig.scope}`,
'client_secret': `${userConfig.client_secret}`
}
};
request(option, function (error, response) {
if (error) throw new Error(error)
const modifyResponse = JSON.parse(response.body)
token = modifyResponse["access_token"]
})
})
it('Create Job should return 201 status code', async () => {
authToken = this.token
console.log('------------>', authToken)
await spec()
.post('https://localhost:3000/jobs')
.withHeaders('Authorization', `Bearer ${authToken}`)
.withHeaders('X-Client-Id', 'AzureDataLoaderUI')
.withHeaders('X-Message-Created-Ts', '2022-JUN-07 04:06')
.withHeaders('X-Message-Id', '4e8fb14c-c71f-46f8-b41a-561d393037ce')
.withHeaders('X-Transaction-Created-Ts', '2022-JUN-07 04:06')
.withHeaders('X-Transaction-Id', 'f11c97dc-a6eb-4ff7-aff6-de47c4686941')
.withHeaders('X-User-Id', 'AutoTestUser')
.withHeaders('Ocp-Apim-Subscription-Key', 'a731ccf0510849c690d23bb42ecef9d3')
.withHeaders('aeg-sas-key', 'b6LrYspQUAY2XnxkrDFDFDYsb8U2zqMQAzaQoZcQ=')
.withJson({ "product": "IRT", "entity": "CENTRE PROVISIONING AND RECIPIENT DETAILS", "operation": "UPDATE", "jobName": "AUTOPERF02", "studyId": "AUTO02" })
.expectStatus(201);
});
});

最佳答案

我认为您还需要等待 request() 调用,或者改用 Cypress cy.request()

let token = "";
let authToken = "";

describe('POST /jobs', function () {
before(async function () {
var option = {
'method': 'POST',
'url': 'https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token',
'headers': {
'Content-Type': 'application/x-www-form-urlencoded'
},
form: {
'client_id': `${userConfig.client_id}`,
'grant_type': 'password',
'Password': `${userConfig.Password}`,
'UserName': `${userConfig.UserName}`,
'scope': `${userConfig.scope}`,
'client_secret': `${userConfig.client_secret}`
}
};
await request(option, function (error, response) {
if (error) throw new Error(error)
const modifyResponse = JSON.parse(response.body)
token = modifyResponse["access_token"]
})
})
it('Create Job should return 201 status code', async () => {
authToken = token
...

使用cy.request,您无需等待

cy.request(option)
.then(response => {
// if error, it's already thrown
token = response.body["access_token"]
})

使用 Mocha done() 表示请求已完成

以上 async 仅适用于基于 Promise 的请求。

等待 request(options, callback) 完成传入 Mocha 的 done() 函数并在设置 token 后调用它。

这是一个简单的例子,使用 JSONPlaceholder 来请求一些数据

const request = require('request');

let token;

before(function (done) {
request({
'method': 'GET',
'url': 'https://jsonplaceholder.typicode.com/todos/1'
}, function (error, response) {
const modifyResponse = JSON.parse(response.body)
token = modifyResponse.title
done() // signal end of request
})
})

it('sees the token', () => {
expect(token).to.eq('delectus aut autem') // passes
})

关于javascript - 在 mocha 中将变量从 before() 传递到 it(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72526254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com