Cypress:如何将每个元素点击记录到文件中?

问题描述 投票:0回答:1

我创建了一个日志文件,在其中记录测试期间的各种操作。 我还想记录我执行的任何点击元素。 但是,我不想在每个点击命令之前添加特定的日志指令,但我希望它无缝地记录点击。

我尝试像这样覆盖单击命令:

Cypress.Commands.overwrite(
 'click',
 (originalFn, subject, positionOrX, y, options = {}) => {
    Logger.log('I just clicked ' + subject)
    return originalFn(subject, positionOrX, y, options);
 }
);

但不知何故它破坏了我的测试。

例如,我有这个登录命令,这是我在 before 挂钩中使用的第一个命令:

Cypress.Commands.add('loginSys', (username = Cypress.env("USERNAME"), password = Cypress.env("PASSWORD")) => {

    cy.clearAllLocalStorage()

    cy.intercept('POST', '*login*').as('login')
    cy.visit(getCMUrl())
    cy.login(username, password)
    cy.wait('@login').then((interception) => {
        const token = interception.response.body.token
        Cypress.env('ACCESS_CODE', token)
    })
})

我收到以下错误:

Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.

The command that returned the promise was:

> cy.type()

The cy command you invoked inside the promise was:

> cy.log()

Because Cypress commands are already promise-like, you don't need to wrap them or return your own promise.

当我覆盖单击命令时,会发生此错误。

cypress cypress-custom-commands
1个回答
0
投票

.login()
命令中将有一个
.type()
命令(可能是两个,用户和密码)。

Cypress 是这样说的 .type()

如果该元素当前未获得焦点,则在发出任何击键之前,Cypress 将首先向该元素发出

.click()
以使其获得焦点。

可能解释了您的

.click()
覆盖和“损坏”
cy.login()
之间的联系。

因此,登录覆盖时不要使用

cy.log()
,而应使用替代的 Cypress.log()

在编写您自己的自定义命令时有用

© www.soinside.com 2019 - 2024. All rights reserved.