如何折射柏木承诺代码


import SignInPage from '../pages/signInPage.cy'
import ValidateAccountPage from '../pages/validateAccountPage.cy'
import DeptPage from '../pages/deptPage.cy'
import HomePage from '../pages/homePage.cy'
import DeleteAccount from '../pages/deleteAccount.cy'
type NewAccountCredentials = { username: string, password: string, vcode: number, uid: string };
const URLS = {
remote: {
client: "http://54.39.177.218:8080",
server: "http://54.39.177.218:3020/api/v2"
}
}
const urlTarget = "remote";
const clientUrl = URLS[urlTarget].client;
const serverUrl = URLS[urlTarget].server;
const signIn = new SignInPage()
const validateAccount = new ValidateAccountPage()
const deptPage = new DeptPage()
const homePage = new HomePage()
const deleteAccount = new DeleteAccount()


describe('Smoke test', () => {

let value
let credentials
beforeEach(async () => {

cy.fixture('addDebtDetails').then(function (data) {
value = data
})
cy.viewport(390, 844);
// create a new non-validated account in the back-end
credentials = await new Promise<NewAccountCredentials>((resolve, reject) => {
cy.request(serverUrl + '/test-accounts/free').then(response => {
expect(response.body).to.have.property("username");
resolve(response.body);
})
});

// load the app - should default to the sign-in page
cy.visit(clientUrl, {
onBeforeLoad: (win) => {
win.sessionStorage.clear();
win.localStorage.clear();
}
});

})
it('verifying home page before debts have been added', async () => {

// sign-in
signIn.SignInMethod(credentials.username, credentials.password)
// validate account
validateAccount.validateAccountMethod(credentials.vcode.toString())
// verify that we are on the home page and see the correct greeting and workspace name
homePage.HomePageMethod()


/* CLEANUP AFTER EACH TEST */
deleteAccount.DeleteAccountMethod(credentials.password)
// must always delete the created account even if any of the above testing fails
await new Promise<void>((resolve, reject) => {
cy.request("DELETE", `${serverUrl}/test-accounts/uid/${credentials.uid}`).then(response => {
expect(response.status).to.be.equal(200);
resolve();
})
});
})
it('verifying debt page  after debt is added', async () => {
/* BEFORE EACH TEST */

// sign-in
signIn.SignInMethod(credentials.username, credentials.password)
// validate account
validateAccount.validateAccountMethod(credentials.vcode.toString())
cy.wait(2000)
// verify that we are on the home page and see the correct greeting and workspace name
deptPage.AddDeptMethod(value.nickName, value.currentBalance, value.annualPercentageRate, value.minimumPayment)
deptPage.AddCalenderDetails(value.calenderYear, value.calenderMonth, value.calenderMonthAndDay)
homePage.HomePageMethodAfterDebt()
/* CLEANUP AFTER EACH TEST */
deleteAccount.DeleteAccountMethod(credentials.password)
// must always delete the created account even if any of the above testing fails
await new Promise<void>((resolve, reject) => {
cy.request("DELETE", `${serverUrl}/test-accounts/uid/${credentials.uid}`).then(response => {
expect(response.status).to.be.equal(200);
resolve();
})
});
})
})

由于柏树是异步的,我的代码中包含一个promise,它由于超时而无法获得请求,我该如何延迟请求。如何根据柏树文档在该代码上应用promise。我现在已经添加了完整的文件以供进一步澄清,请检查它。你现在可以通过运行这个来检查吗。

Cypress在运行it()块之前自动等待beforeEach()中的所有命令完成,因此您不需要任何Promises。

如果您对凭据有顾虑,请重复检查属性";用户名";在每次测试的顶部:

expect(credentials).to.have.property('username');

这同样适用于您的清理代码,如果您将其移动到afterEach()中,则不需要该部分中的Promise。

完整测试

describe('Smoke test', () => {
let value
let credentials
beforeEach(() => {
cy.fixture('addDebtDetails')
.then((data) => value = data)
// create a new non-validated account in the back-end
cy.request(serverUrl + '/test-accounts/free')
.then(response => {
expect(response.body).to.have.property("username");
credentials = response.body;
})
});
// load the app - should default to the sign-in page
cy.viewport(390, 844);
cy.visit(clientUrl, {
onBeforeLoad: (win) => {
win.sessionStorage.clear();
win.localStorage.clear();
}
});
})
afterEach(() => {
/* CLEANUP AFTER EACH TEST */
deleteAccount.DeleteAccountMethod(credentials.password)
// must always delete the created account even if any of the above testing fails
cy.request("DELETE", `${serverUrl}/test-accounts/uid/${credentials.uid}`)
.then(response => {
expect(response.status).to.be.equal(200);
})
})
it('verifying home page before debts have been added', () => {
// same check as above, should still be passing
expect(credentials).to.have.property('username');   
// sign-in
signIn.SignInMethod(credentials.username, credentials.password)
// validate account
validateAccount.validateAccountMethod(credentials.vcode.toString())
// verify that we are on the home page...
homePage.HomePageMethod()
})
it('verifying debt page  after debt is added', () => {
// same check as above, should still be passing
expect(credentials).to.have.property('username');   
// sign-in
signIn.SignInMethod(credentials.username, credentials.password)
// validate account
validateAccount.validateAccountMethod(credentials.vcode.toString())
// verify that we are on the dept page...
deptPage.AddDeptMethod(value.nickName, value.currentBalance, value.annualPercentageRate, value.minimumPayment)
deptPage.AddCalenderDetails(value.calenderYear, value.calenderMonth, value.calenderMonthAndDay)
homePage.HomePageMethodAfterDebt()
})
})

因为Cypress命令已经是异步的,所以根本不需要Promise。相反,您可以通过多种方式存储变量,我个人最喜欢的是Cypress环境变量。

...
beforeEach(() => {
cy.request(serverUrl + '/test-accounts/free').then(response => {
expect(response.body).to.have.property("username");
Cypress.env('credentials', response.body);
})
cy.visit(clientUrl, {
onBeforeLoad: (win) => {
win.sessionStorage.clear();
win.localStorage.clear();
}
});
});

在上面的内容中,您可以通过Cypress.env('credentials')引用这些凭据

您可以在package.json中设置jest-timeOut。"笑话":{"testTimeout":15000,}

最新更新