我当时在演示电子商务网站saucedemo.com的网站上使用赛普拉斯练习UI测试。我试图测试排序选择按钮。它对于standard_user帐户来说工作得很好,但对于problem_user帐户,则在任何情况下都无法进行排序(有z排序,za排序,价格从低到高排序和价格从高到低排序),但是我的代码通过了这两种情况,所以我想知道我是否正确编码?这是我的代码
Cypress.Commands.add("checkAtoZsorting", () =>{
cy.get('[ class="product_sort_container"]')
.select('az')
.should("have.value","az")
cy.get('[class="inventory_item_name"]').should(($item1) => {
expect($item1).to.have.length(6)
});
cy.get('[class="inventory_item_name"]').eq(0).should("have.text","Sauce Labs Backpack");
cy.get('[class="inventory_item_name"]').eq(1).should("have.text","Sauce Labs Bike Light");
cy.get('[class="inventory_item_name"]').eq(2).should("have.text","Sauce Labs Bolt T-Shirt");
cy.get('[class="inventory_item_name"]').eq(3).should("have.text","Sauce Labs Fleece Jacket");
cy.get('[class="inventory_item_name"]').eq(4).should("have.text","Sauce Labs Onesie");
cy.get('[class="inventory_item_name"]').eq(5).should("have.text","Test.allTheThings() T-Shirt (Red)");
});
Cypress.Commands.add("checkZtoAsorting", () =>{
cy.get('[ class="product_sort_container"]')
.select('za')
.should("have.value","za")
cy.get('[class="inventory_item_name"]').should(($item1) => {
expect($item1).to.have.length(6)
});
cy.get('[class="inventory_item_name"]').eq(0).should("have.text","Test.allTheThings() T-Shirt (Red)");
cy.get('[class="inventory_item_name"]').eq(1).should("have.text","Sauce Labs Onesie");
cy.get('[class="inventory_item_name"]').eq(2).should("have.text","Sauce Labs Fleece Jacket");
cy.get('[class="inventory_item_name"]').eq(3).should("have.text","Sauce Labs Bolt T-Shirt");
cy.get('[class="inventory_item_name"]').eq(4).should("have.text","Sauce Labs Bike Light");
cy.get('[class="inventory_item_name"]').eq(5).should("have.text","Sauce Labs Backpack");
真的需要帮助....
答案 0 :(得分:0)
我复制了您的代码,并使用 standard_user 和 problem_user 对我有用。
commands.js(不变):
Cypress.Commands.add("checkAtoZsorting", () =>{
cy.get('[ class="product_sort_container"]')
.select('az')
.should("have.value","az")
cy.get('[class="inventory_item_name"]').should(($item1) => {
expect($item1).to.have.length(6)
});
cy.get('[class="inventory_item_name"]').eq(0).should("have.text","Sauce Labs Backpack");
cy.get('[class="inventory_item_name"]').eq(1).should("have.text","Sauce Labs Bike Light");
cy.get('[class="inventory_item_name"]').eq(2).should("have.text","Sauce Labs Bolt T-Shirt");
cy.get('[class="inventory_item_name"]').eq(3).should("have.text","Sauce Labs Fleece Jacket");
cy.get('[class="inventory_item_name"]').eq(4).should("have.text","Sauce Labs Onesie");
cy.get('[class="inventory_item_name"]').eq(5).should("have.text","Test.allTheThings() T-Shirt (Red)");
});
Cypress.Commands.add("checkZtoAsorting", () => {
cy.get('[ class="product_sort_container"]')
.select('za')
.should("have.value", "za")
cy.get('[class="inventory_item_name"]').should(($item1) => {
expect($item1).to.have.length(6)
});
cy.get('[class="inventory_item_name"]').eq(0).should("have.text", "Test.allTheThings() T-Shirt (Red)");
cy.get('[class="inventory_item_name"]').eq(1).should("have.text", "Sauce Labs Onesie");
cy.get('[class="inventory_item_name"]').eq(2).should("have.text", "Sauce Labs Fleece Jacket");
cy.get('[class="inventory_item_name"]').eq(3).should("have.text", "Sauce Labs Bolt T-Shirt");
cy.get('[class="inventory_item_name"]').eq(4).should("have.text", "Sauce Labs Bike Light");
cy.get('[class="inventory_item_name"]').eq(5).should("have.text", "Sauce Labs Backpack");
});
spec.js:
describe('sort', () => {
it('sort standard_user', () => {
cy.visit('/');
cy.get('input#user-name').type('standard_user');
cy.get('input#password').type('secret_sauce');
cy.get('input[type="submit"]').click();
cy.checkAtoZsorting();
cy.checkZtoAsorting();
});
it('sort problem_user', () => {
cy.visit('/');
cy.get('input#user-name').type('problem_user');
cy.get('input#password').type('secret_sauce');
cy.get('input[type="submit"]').click();
cy.checkAtoZsorting();
cy.checkZtoAsorting();
});
});
备注:将cy.get()
与类属性一起使用时,可以使用此格式:
cy.get('.product_sort_container')