首页 > 解决方案 > 是否可以从非规范代码中将事件记录到赛普拉斯?

问题描述

假设我有一个简单的代码文件:

实用程序.js

function add(x, y){
 return x + y;
}

util.spec.js

describe("utils", ()=>{
  it("should add 2 and 4 together", ()=>{
    sum = add(2,4);
    expect(sum).to('eq', 6);
  })
});

是否可以直接在add()将事件记录到柏树输出的函数中添加一些命令?测试在远程服务器上运行,并且需要详细日志。

像这样的东西: util.js

function add(x, y){
 // cy.log('add function was just called with:', {x, y})
 return x + y;
}

标签: javascriptcypress

解决方案


赛普拉斯命令只是 javascript,所以是的......当你尝试它时发生了什么?

请记住,您不能从函数返回命令值,并且时间可能不是您所期望的,但作为“副作用”应该没问题。

时序/顺序

function add(x, y){
 cy.log('add function was just called with:', {x, y})   // logs 2nd
 return x + y;
}

describe("utils", ()=>{
  it("should add 2 and 4 together", ()=>{
    sum = add(2,4);
    console.log(sum)                                    // logs first
    cy.wrap(sum).should('eq', 6)
  })
});

推荐阅读