如何使用Cypress PageObject类的可重复使用的选择器?
我正在尝试在柏树中使用pageObject,我想在同一文件中将选择器放在同一文件中,以便我可以在多个功能中重复使用它们,并且只需要在需要更新时才在一个地方更改它们。
这是一个简单的示例:
class HomePage {
searchLink = "a[class='search button']";
clickSearchLink() {
cy.get(this.searchLink).click();
}
}
export const homePage = new HomePage();
这很好,我的测试可以调用函数,例如homepage.clicksearchlink(),但是我有两个问题:
- 当我运行eslint时,它会失败。
e2e/page-objects/home-page.js 2:14错误解析错误:意外 令牌=
- 这样做意味着我的规格文件中可用搜索链接,我希望它仅在此类中看到。我尝试将SearchLink设置为LET或VAR,但这只是在ClickSearchLink()函数中无法使用。
是否可以在此课程中让我的选择器并通过覆盖?
I'm trying to use a pageObject in Cypress, I want to have my selectors in the same file so I can reuse them in multiple functions and only need to change them in one place if they need updating.
Here is a simple example:
class HomePage {
searchLink = "a[class='search button']";
clickSearchLink() {
cy.get(this.searchLink).click();
}
}
export const homePage = new HomePage();
This works fine, my test can call the functions e.g. homePage.clickSearchLink() but I have two issues:
- When I run eslint it fails with
e2e/page-objects/home-page.js 2:14 error Parsing error: Unexpected
token =
- Doing it this way means searchLink is available in my spec files, I want it to only be seen in this class. I tried setting searchLink to a let or var but that just made it unusable in the clickSearchLink() function.
Is it possible to have my selectors in this class and still pass linting?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将打字稿目标撞到ES6/ES2015,则可以使用私有字段。
对于ESLINT错误,添加一个构造函数。
/cypress/tsconfig.json
If you bump the Typescript target to es6/es2015 you can use private fields.
Private Class Features
For the ESLint error, add a constructor.
/cypress/tsconfig.json