我尝试在“每个之前”请求生成一个自定义编号,但我的“每个之前”不想进一步。
BeforeEach
:
beforeEach(async () => {
const customerNumber = await createCustomerWithoutPID(
firstName,
lastname,
email,
)
cy.log(customerNumber)
const requestId = await generateRequestID(customerNumber)
const generateJWT = await generateJWTtoken(customerNumber, requestId)
cy.visit(
`https://www.uat.telenet.be/nl/create-profile/?flow=completeProfile&jwt=${generateJWT}`,
)
cy.log('disable recaptcha')
cy.turnOffRecaptcha()
})
要求:
export const createCustomerWithoutPID = async (
firstName: string,
lastName: string,
email: string,
): Promise<string> => {
const env = Cypress.env('CYPRESS_ENV')
const responseBody: Cypress.Response<CustomerResponse> = await new Promise(
() => {
cy.request({
method: 'POST',
url: `http://test-data-service.${env}.corp.telenet.be/api/customer-accounts`,
headers: {
accept: 'application/json',
'Content-Type': 'application/json',
},
body: {
firstName: firstName,
lastName: lastName,
emailAddress: email,
},
})
},
)
return responseBody.body.customerNumber
}
我认为我的 Promise 没有正确解决,但我不知道使用“await/async”语法解决这个问题的最佳方法是什么......这是生成链接所需的 JWT 令牌的第一步,首先是 CustomerNumber,然后是 RequestId,然后将其提取到 xml 请求中并发送,以便我可以获得我的 JWTToken。
如果有人知道如何重构我的代码,以便我的
beforeeach
工作。我收到了请求的正文,但我只需要 customerNumber,但它没有显示。
我希望返回响应正文的 customerBody,但它从未显示出来
我认为您必须通过传入
customerNumber
参数并使用它来提取 resolve
来返回 responseBody.body.customerNumber
。
此函数内不需要 async/await
const createCustomerWithoutPID = (firstName:string, lastName:string, email:string)
: Promise<string> => {
return new Promise((resolve) => {
cy.request({...})
.then(response => resolve(response.body.customerNumber)
})