为什么作为变量传递的参数在 Cypress/Typescript 中不显示其分配的值? [重复]

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

我遇到了 Typescript/Cypress 的特殊行为。简而言之,代码如下


//Step 1: Declares variable
let wireTitle: string = ""


//..test case goes on..//

//Step 2: Assigns value to the variable
cy.get("ol li").first().then((element) => {
            wireTitle = element.find('.prelead-title').text()}


//Step 3: Logs the result to assert the value has displayed correctly
cy.get("ol li").then(() => {console.log(wireTitle)})

//Step 4: Performs the assertion
cy.get("div.prelead-modal-component").contains(wireTitle)

奇怪的行为发生在步骤 3 和 4 之间 - 控制台将变量显示为已分配值,但在运行测试用例时我可以看到显示错误

cy.contains() cannot be passed an empty string.

有人知道可能导致此问题的原因吗?

我已经检查了步骤 3 中的值,我知道错误不是由于分配逻辑不起作用。

我执行了很多 then() 命令以及强制等待 4 秒,但对结果没有任何影响,表明问题不是由 Cypress 的异步特性引起的。

我也尝试了不同的 Cypress 命令(should()),但结果是相同的 - 每次变量的值都是空字符串

typescript variables arguments cypress
1个回答
0
投票

在步骤 2 和 3 中,您有

then
等待异步操作。但在第 4 步中你不需要,它会立即运行(我猜)。因此,当
contains
在赋值之前执行时,就会出现竞争条件。

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