为什么我的对象实例的变量无法在 cypress/it 块中正确访问?



在我的类对象中,我有一个名为category的变量,它将在构造函数中初始化。

export class Page {
constructor(category) {
this.category = category;
}
categorySession = `[data-cy="${this.category}-form"]`

enterValue(Value){
cy.get(this.categorySession).find('Value').type(`${Value}`).should('have.value',`${Value}`)
}
}

当我运行测试时,在cypress中,它向我抛出一个错误[data-cy="undefined-form"],但从未找到它。

import {Page} from "./pages/valuePage"
const LiquidityPage = new Page('Liquidity')
console.log(LiquidityPage.category) <--- it show Liquidity
describe('E2E_case', () => {
describe('Portfolio Value', () => {
it('Input Portfolio Value', () => {
cy.visit('http://localhost:30087/')
LiquidityPage.enterValue(123)  // this gives me this error -  Timed out retrying after 4000ms: Expected to find element: [data-cy="undefined-form"], but never found it.

})
})

为什么[data-cy="undefined-form"]而不是我的期望值[data-cy=" liquid -form"]

您还需要在构造函数中设置sessionCategory。

这样可以避免选择器字符串中的undefined值。

export class Page {
constructor(category) {
this.category = category;
this.categorySession = `[data-cy="${this.category}-form"]`
}
...
}

但这似乎很明显,你一定已经试过了?

相关内容

最新更新