2 回答

TA貢獻1799條經驗 獲得超8個贊
您可以根據以下內容隨機化類別:
const picked = Math.floor(Math.random()*3); // this gives you a random value from 0 - 3;
// using template string literals - we can add that into picked
cy.get(`div.tickets-info > div:nth-child(`${picked}`) > span > b`).type(3, {force: true})
從picked - 你可以有一個與類別相關的元素數組:
const categories = ["Junior", "student", "normal", "senior"]
您還可以將門票的值categories作為對象并將其用于計算總數。
const categories = [{
type: "junior",
value: 5
}, {
type: "student",
value: 3
}, {
type: "normal"
value: 10
}, {
type: "senior",
value: 3
}]
你可以說,使用選擇的值來計算總金額
const value = categories[picked].value * random_amount;
cy.get(".sum-field label:nth-child(1)").should("contain", value) // total

TA貢獻1841條經驗 獲得超3個贊
我需要對我們的應用程序做同樣的事情,所以我想出了一個自定義命令來輕松實現這一點。這是我提出的代碼cypress/support/commands.js:
Cypress.Commands.add('any', { prevSubject: 'element' }, (subject, size = 1) => {
return cy.wrap(subject).then(elementList => {
// this line enables me to use this command with either cy.get() or cy.wrap()
elementList = (elementList.jquery) ? elementList.get() : elementList;
elementList = Cypress._.sampleSize(elementList, size);
elementList = (elementList.length > 1) ? elementList : elementList[0];
return cy.wrap(elementList);
});
});
我還可以在自定義命令中使用帶有Cypress.log()的自定義日志記錄。為了清楚起見,我從上面的代碼中刪除了它。
然后,您可以像使用任何其他 cy 命令一樣在測試中使用它:
cy.get('div.tickets-info > div > span > b').any().type(3,{force:true});
或者如果您需要多個:
cy.get('div.tickets-info > div > span > b').any(2).each(element => {
cy.wrap(element).type(2, { force: true });
});
添加回答
舉報