所以我尝试在 cypress (commands.js 文件)中添加自定义命令,如下所示:
\n Cypress.Commands.add("login", (email, password) => {\n cy.intercept(\'POST\', \'**/auth\').as(\'login\');\n cy.visit(\'/auth\');\n cy.get(\'[formcontrolname="email"]\').type(email);\n cy.get(\'[formcontrolname="password"]\').type(password);\n cy.get(\'form\').submit();\n cy.wait(\'@login\').then(xhr => {\n expect(xhr.request.body.email).to.equal(email);\n expect(xhr.request.body.password).to.equal(password);\n });\n });\nRun Code Online (Sandbox Code Playgroud)\n但我收到此错误:
\n\'参数类型字符串不可分配给参数类型 keyof Chainable ... \xc2\xa0\xc2\xa0Type 字符串不可分配给类型“and” | “作为”| “选择文件” | “模糊”| “检查”| “孩子们”| “清晰”| “清除Cookie”| “清除Cookies” | “清除本地存储”| “点击”| ... \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0Type 字符串不可分配给类型“intercept”\'
\n我发现这个问题Argument type string is not assignable to parameter type keyof Chainable... in Cypress,但这里的答案仅适用于 index.d.ts 文件,但是我有一个 index.js 文件(cypress版本 10.3.0 这对我不起作用。有人能帮我解决这个问题吗?
\n我试图用它cy.log来帮助我理解测试失败的原因。不幸的是,我似乎无法让它记录我想要的消息。经过一番摆弄后,我进行了这个测试:
describe.only("In what order are these messages printed?", () => {
it("I would expect the LOG to be printed before the EXPECT", () => {
cy.log("Hello from cy.log");
expect(1 + 1).to.equal(2);
});
});
Run Code Online (Sandbox Code Playgroud)
我希望“Hello”首先出现,但我得到的是:
此外,在一次失败的测试中,cy.log似乎根本没有出现,这对我来说是不幸的。同样,这是一个简单但极端的例子。
describe.only("Here is my real problem", () => {
it("I would expect the LOG to be printed before the test fails", () => {
cy.log("Hello from cy.log");
expect(2 + 2).to.equal(42);
});
});
Run Code Online (Sandbox Code Playgroud)
这会产生这样的结果:
如果是这样,我可以求助于console.log. 尽管遗憾的是我们无法在 Cypress 主窗口本身中整齐地报告日志。
或者我是否不小心设置了一个扰乱时间的设置cy.log …
我被困在我的项目列表中找到一个特定的按钮...该按钮存在 3 次完全相同data-testid,但父项不同。我以
错误:
cy.click()只能在单个元素上调用。您的主题包含 3 个要素。{ multiple: true }如果您想连续单击每个元素,请通过。
HTML:
<div data-testid="list-item">
<div>
<div>
<span data-testid="status1">
<button data-testid="details_button">click</button>
</div>
</div>
</div>
<div data-testid="list-item">
<div>
<div>
<span data-testid="status2">
<button data-testid="details_button">click</button>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
如何选择 status1 或 status2 的详细信息按钮?
我的尝试是:
cy.get('[data-testid=status1]')
.get('[data-testid="details_button"]').click()
Run Code Online (Sandbox Code Playgroud)
cy.get('[data-testid=status1]')
.parent().parent()
.get('[data-testid="details_button"]').click()
Run Code Online (Sandbox Code Playgroud)
我想在Cypress中做出如下断言:
cy.get(a).should('be.visible').or(()=>{
cy.get(b).should('be.visible');
});
Run Code Online (Sandbox Code Playgroud)
换句话说,我想检查条件 A 或条件 B 是否为真。如何在赛普拉斯中做到这一点?
我正在使用 Cypress 测试数据表单,但卡在了在页面上显示警报的步骤上。
这是测试,但不起作用。
describe('Alert is displayed with warning text', () => {
it('Assert that the alert is displayed after entering data', () => {
cy.visit('/')
cy.get('input').type('some data').blur()
cy.on ('window:alert', (text) => {
cy.wrap(text).should('eq', 'alert text')
})
})
})
Run Code Online (Sandbox Code Playgroud)
如何测试页面上弹出的此警报?
我想以一致的时间启动 Cypress,例如今年七月的第一个星期一。我可以用来clock覆盖当前时间:
cy.clock(new Date(2023, 7, 1, 12, 0, 0), ['Date']);
Run Code Online (Sandbox Code Playgroud)
cy.tick但每当我想模拟一段时间的流逝时,我就必须使用它。有没有办法设置测试的初始时间但让时间自行流动?因此,调用后 5 秒cy.clock是“2023-07-01 12:00:05”,而不是永久“2023-07-01 12:00:00”。
Cypress 10 几天前发布,带来了大量重大突破性变化。作为第一次 Cypress 用户(从 Protractor 和 Cucumber 迁移),我没有意识到这一点。我正在尝试使用 Cucumber 安装 Cypress,但所有文档(也许还有cypress-cucumber-preprocessor插件本身)都是为 Cypress 9 编写/配置的。
有没有人cypress-cucumber-preprocessor在 Angular 项目中成功配置或迁移 Cypress 10,如果是,步骤是什么?
cucumber typescript angular cypress cypress-cucumber-preprocessor
如果 Cypress 发出的请求没有与之关联的拦截,如何使 Cypress 测试失败?
假设 cypress 导航到一个页面并发出 10 个请求。如果这些请求中的任何一个没有被拦截,我怎样才能让 cypress 自动失败。我不希望赛普拉斯提出的任何请求不被嘲笑。
这可能吗?
我有以下文件夹结构:
tests
cypress.env.json
cypress
fixtures
integration
test
test.spec.ts
plugins
support
Run Code Online (Sandbox Code Playgroud)
我错过了这个问题不必要的文件。
所以我的 cypress.env.json 看起来像这样:
tests
cypress.env.json
cypress
fixtures
integration
test
test.spec.ts
plugins
support
Run Code Online (Sandbox Code Playgroud)
现在我尝试访问 test.spec.ts 文件中的 env 变量:
{
"loginTestStage": {
"admin": {
"username": "value1",
"password": "value2"
},
"user": {
"username": "value3",
"password": "value4"
}
},
"vatid" : {
"x1": "value5",
"x2": "value6",
"x3": "value7",
"x4": "value8"
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行测试时,我总是在这里收到此错误:
Cannot read properties of undefined (reading 'admin')
Run Code Online (Sandbox Code Playgroud)
因此无法找到 Env 变量的值。如何解决这个问题?
提前致谢
testing error-handling automation environment-variables cypress
我正在 React 前端上运行 cypress v10,并尝试让我的 cookie 在我的一些测试中持续存在。
我正在测试登录和退出我的应用程序的流程。从 V10 开始,cy.session() 似乎是可行的方法。然而,我见过的大多数例子只是在 beforeEach() 的开头添加一个 cookie 到一个命名会话对象中。
我在测试模块开始时没有最初可用的 cookie,我创建了一些测试(如下所示),在使用 cookie 登录时进行更多测试,然后注销,删除 cookie。
所以我对如何将 session() 实现到以下代码的组合中有点困惑。可以使用一些结构方向,谢谢!
describe('Auth (e2e)', () => {
it('should load and redirect to /login', () => {
cy.visit('https://localhost:3000/');
cy.wait(500);
cy.url().should('include', 'login');
});
it('login button should be disabled initially', () => {
cy.get('#btn-login').should('have.attr', 'disabled');
});
it('login button should be enabled upon enter valid details', () => {
cy.get('#tbxUsername').click().type('hfisher');
cy.get('#tbxPassword').click().type('#VS1DEV');
cy.get('#btn-login').should('not.have.attr', 'disabled');
});
it('should navigate to the home/dashboard upon logging …Run Code Online (Sandbox Code Playgroud) cypress ×10
typescript ×2
alert ×1
angular ×1
automation ×1
cookies ×1
cucumber ×1
cypress-cucumber-preprocessor ×1
get ×1
javascript ×1
reactjs ×1
testing ×1