当赛普拉斯满足某些条件时测试失败

Dha*_*amo 0 cypress

我有以下方法utils.js

/**
 * To verify the any array of objevt with given keyword
 * @param {*} list 
 * @param {*} searchKeyword 
 * @returns true if any mismatch found and vice versa
 */
export const verifyMatchedItems = (list, searchKeyword) => {
  var matchedItems = {
    title: [],
    region: [],
    country: [],
  };

  var nonMatchedItems = [];

  list.map((item, index) => {
    if (item.title.includes(searchKeyword)) {
      matchedItems.title.push(item.title);
    } else if (item.region.includes(searchKeyword)) {
      matchedItems.region.push(item.region);
    } else if (item.country.includes(searchKeyword)) {
      matchedItems.country.push(item.country);
    } else {
      nonMatchedItems.push(item);
    }
  });

  cy.log("Matched-Items:" + JSON.stringify(matchedItems));

  if (nonMatchedItems.length>0) {
    cy.log("nonMatchedItems:" + JSON.stringify(nonMatchedItems));
  }

  return nonMatchedItems.length>0;
};
Run Code Online (Sandbox Code Playgroud)

test我这样使用它,

      //fetch all data in Search Results page and store it
      cy.getResultList().then((searchResultDetails) => {
        
        //verify the keyword is matched with result attributes
        var flag = Utils.verifyMatchedItems(searchResultDetails, searchKeyword);
        
        expect(flag).to.be.equal(false);
Run Code Online (Sandbox Code Playgroud)

如果该flag值为 true,那么我希望测试失败。但是当我尝试时,它失败了,但没有执行任何先前的步骤,例如cy.log(). 我们如何才能让 cypress 抛出错误/使测试失败并显示自定义消息:Failed to due bla bla bla...

Byp*_*ass 7

如果有人想要这个,请尝试将其包装expect()cy.then().

这是因为前面的函数包含一些 Cypress 命令(仅cy.log()在这种情况下)。

要在这些命令之后进行最终检查,请将其放入 Cypress 队列中。

//fetch all data in Search Results page and store it
cy.getResultList().then((searchResultDetails) => {
        
  //verify the keyword is matched with result attributes
  var flag = Utils.verifyMatchedItems(searchResultDetails, searchKeyword);
        
  cy.then(() => expect(flag).to.be.equal(false))
})
Run Code Online (Sandbox Code Playgroud)