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();
})
});
})
})
由于賽普拉斯是異步的,我的代碼包含一個由于超時而無法獲取請求的承諾,我該如何延遲請求。如何根據賽普拉斯檔案在此代碼上應用承諾。我現在已經添加了完整的檔案以進一步澄清,請檢查。你現在可以通過運行這個來檢查一下嗎?
uj5u.com熱心網友回復:
因為 Cypress 命令已經是異步的,所以您根本不需要 Promise。相反,您可以通過多種方式存盤變數,我個人最喜歡的是賽普拉斯環境變數。
...
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')
uj5u.com熱心網友回復:
Cypress在運行塊之前會自動等待所有命令beforeEach()完成it(),因此您不需要任何 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()
})
})
uj5u.com熱心網友回復:
你可以在 package.json 中設定 jest timeOut。“開玩笑”:{“testTimeout”:15000,}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517330.html
標籤:打字稿异步柏请求-承诺
