简体   繁体   English

Cypress 异步执行返回 4 个相同的断言

[英]Cypress asynchronous execution returns 4 same assertion

I have a question.我有个问题。 I am using Cypress for my automation and I started using async and await for my tests.我正在使用 Cypress 进行自动化,我开始使用 async 并等待我的测试。 I am using POM design pattern.我正在使用 POM 设计模式。

My question: If I execute the following test:我的问题:如果我执行以下测试:

test.spec.ts class (test class) test.spec.ts class(测试类)

 import { login_po } from "../pom/1.Chiquito/login_po"; const pom = new login_po() describe("Put some name here.", async() => { it('TestCase1', async () => { await pom.navigateTo(); }); });

My POM class.我的 POM class。

 export class login_po { navigateTo() { cy.visit(`https://chiquito-qa.omnifitrgsites.co.uk/`).url().should('be.equal', 'https://chiquito-qa.omnifitrgsites.co.uk/').then(() => this.verifyAfterLogin()); } verifyAfterLogin() { cy.get('.header__logo-img'); } }

When I execute the test - Cypress makes 4 (same) assertions.当我执行测试时 - 赛普拉斯做出 4 个(相同的)断言。 在此处输入图像描述

If I remove 'async' - 'await' from the test class - Cypress makes 1 assertion.如果我从测试 class 中删除 'async' - 'await' - 赛普拉斯做出 1 个断言。

 import { login_po } from "../pom/1.Chiquito/login_po"; const pom = new login_po() describe("Put some name here.", () => { it('TestCase1', () => { pom.navigateTo(); }); });

在此处输入图像描述

Why this is happening?为什么会这样?

Cypress commands are not promises , and their interaction with async/await will not happen as you expect. Cypress 命令不是 promises ,它们与 async/await 的交互不会像您期望的那样发生。 Additionally, while POM is feasible and reasonable to do within Cypress, it is recommended that you use App Actions instead of a POM.此外,虽然 POM 在 Cypress 中是可行且合理的, 但建议您使用 App Actions而不是 POM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM