我正在使用柏树。但据我了解,柏树是建在摩卡之上的。所以,我想检查我正在处理的项目使用的摩卡版本。我正准备进行package.json搜索,但没有发现摩卡依赖项。这是否意味着 mocha 内置于 cypress 中?那么我如何确定内部使用的cypress mocha版本呢?
背景:
大家好,我正在尝试编写一个 Cypress 测试,该测试应该检查在单击图像本身后是否调用了图像 URL。
目前我运行测试,它在第一次运行时找到图像 URL,但是当重新运行时,测试无法找到它,因为图像已保存到浏览器缓存中。
有没有办法在每次 Cypress 测试之前清除浏览器缓存,以便在运行测试时始终调用/找到图像 URL?
目前的解决方案:
it('should request webp image format for zoomed pdp image', () => {
cy.intercept(
'**/cdn/cs/set/assets/blt2bd0405f3cb027cb/5005769.jpg?fit=bounds&format=webply&quality=80&width=1600&height=1600&dpr=1.5'
).as('zoomedImageRequest');
cy.gotoPage('product/transparent-red-sorting-case-to-go-5005769');
cy.get(pdp.pdpImage).click();
cy.wait('@zoomedImageRequest').then((xhr) => {
const zoomedImageUrl = xhr.response.url;
expect(zoomedImageUrl).to.contain('format=webply');
});
});
Run Code Online (Sandbox Code Playgroud) javascript automated-tests cypress cypress-custom-commands cypress-intercept
我正在尝试解决一个问题,但还没有找到好的解决方案。
我的组织有一些测试,这些测试在运行时创建用户电子邮件,而在其他时候则不创建。当为测试创建用户电子邮件时,它会被分配一个别名@userEmail。我想在 after hook 中编写一个函数,无论是否创建/分配用户别名,该函数都会运行所有测试。
该函数基本上会检查@userEmail别名是否存在。如果它确实运行数据库查询来清除我们的用户,则不执行任何操作。
/cypress/support/actions/user-setup.js这就是在我们的文件中创建别名的方式
const setupEmail = function (alias) {
cy.wrap(getEmail(alias)).as('userEmail');
};
Run Code Online (Sandbox Code Playgroud)
这是我的aftereach()方法
afterEach(function () {
cy.get('@userEmail').then((email) => {
cy.dbQuery('clearSSNandPhoneNumber', email);
});
});
Run Code Online (Sandbox Code Playgroud)
现在,这非常适合创建别名的测试。但是,如果测试未创建别名,则 afterEach 函数会使整个测试失败。
似乎这不是检查别名的建议方法。如何创建一个解决方案,在别名存在时运行数据库查询,在别名不存在时不执行任何操作?
我在测试受 oauth 保护的应用程序时遇到问题。当没有公共页面时,问题就会显现出来 - 如果用户未经过身份验证,就会立即重定向到 OAuth 服务器。
我设法以更简单的设置重现该问题:
以下是各自的应用程序(在 Flask 中):
假应用程序
from flask import Flask, redirect, render_template_string
app = Flask(__name__)
app_host="fake-app"
app_port=5000
app_uri=f"http://{app_host}:{app_port}"
oauth_host="fake-oauth-server"
oauth_port=5001
oauth_uri=f"http://{oauth_host}:{oauth_port}"
@app.route('/')
def hello():
return render_template_string('''<!doctype html>
<html>
<body>
<p>Hello, World MainApp!</p>
<a id="loginButton" href="{{ oauth_uri }}?redirect_uri={{ app_uri }}">Login</a>
</body>
</html>
''',
oauth_uri=oauth_uri,
app_uri=app_uri
)
@app.route('/goto-oauth')
def goto_oauth():
return redirect(f"{oauth_uri}?redirect_uri={app_uri}")
if __name__ == '__main__':
app.run(host=app_host, port=app_port)
Run Code Online (Sandbox Code Playgroud)
假oauth服务器:
from flask import Flask, render_template_string, request
app = Flask(__name__) …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
cy.get('input:checkbox').first().as('firstCheckbox').then(() => {
cy.get('@firstCheckbox').should('be.checked').click()
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('be.visible')
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('not.exist')
cy.get('@firstCheckbox').should('not.be.checked').click()
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('be.visible')
cy.get('.globalMessage-display > :nth-child(1) > .alert').should('not.exist')
cy.get('@firstCheckbox').should('be.checked')
})
Run Code Online (Sandbox Code Playgroud)
cy.get('@firstCheckbox').should('be.checked').click()将复选框移动到最后一个索引位置而不是第一个索引位置后。根据我对别名的理解,这应该不重要,因为我引用它并且('@')它在哪个位置并不重要。不幸的是,下一个cy.get('@firstCheckbox')访问新的第一个元素,而不是我最初引用的元素。我也无法从文档中弄清楚。错误在哪里?
https://docs.cypress.io/guides/core-concepts/variables-and-aliases
https://docs.cypress.io/api/commands/as
@agoff 的更新:
它似乎更适合您的代码,但我注意到新代码存在一个新问题。如果您取消选中该复选框,则该类将使用 js 重新加载,导致它短暂地从 dom 中消失。之后它就找不到它了,测试中止。我也尝试过重新加载,但它再也找不到该元素了。这该如何处理呢?
选中的复选框类的名称为custom-control-input ng-untouched ng-pristine ng-valid。取消选中类名后更改为custom-control-input.ng-untouched.ng-valid.ng-dirty一秒钟,然后元素被删除并使用类名重新加载到 DOM 中custom-control-input ng-untouched ng-pristine ng-valid。问题在于,尽管搜索并找到了具有该类的初始cy.wrap($el)元素,但第二个元素仍尝试查找该元素。这是我不明白的另一件事。custom-control-input.ng-untouched.ng-valid.ng-dirtycustom-control-input ng-untouched ng-pristine ng-valid
我正在尝试在 Cypress 中端到端测试文件上传页面,其中包括测试文件上传进度条是否有效。
不幸的是,在本地环境中,上传会立即发生,因此不会显示进度条。
我知道您可以使用模拟慢速网络来限制响应速度。cy.intercept()但是,这并不会降低请求上传速度:
cy.intercept('post', `/route`, (req) => {
req.on('response', (res) => {
res.setThrottle(1000)
})
}).as('post')
Run Code Online (Sandbox Code Playgroud)
有什么方法可以对传出请求应用限制吗?
我有这个cypress.json配置文件:
{
"env": {
"example": "Hello World!"
}
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中:
Cypress.env("example")
Run Code Online (Sandbox Code Playgroud)
我可以向环境系统添加任何类型的定义吗?(与 Typescript 或 eslint 一起使用)我很想在这里使用智能感知。
我正在使用 cypress 来测试文本字段是否正确显示输入的文本。
cy.get('.creation-name').should('not.have.text');
const testName = 'Test name';
cy.get('.creation-name').type(`${testName}`);
Run Code Online (Sandbox Code Playgroud)
TextField 是一个 MUI 组件,这是一个 React 项目。
<div className="create-name">
<TextField
id="Name"
label="Create name"
onChange={(event) => setName(event.target.value)}
/>
</div>
Run Code Online (Sandbox Code Playgroud)
我可以从视频中看到文本输入正常,但是当我尝试检查它是否存在时,它失败了。我尝试了多个不同的行来获取测试文本值,包括:
//got AssertionError for all of these
cy.get('.creation-name').invoke('val').should('equal', 'Test name');//expected '' to equal Test name
cy.contains(`${testName}`).should('have.text', `${testName}`);//expected '' to equal Test name
cy.get('[id="Name"]').should('have.text', `${testName}`);//expected '' to equal Test name
cy.get('.creation-name').invoke('text').should('equal', 'Test');//expected Create name to equal Test name
cy.get('.creation-name').should('have.text', `${testName}`);//expected Create name to equal Test name
Run Code Online (Sandbox Code Playgroud)
看来我没有得到任何东西,或者我正在瞄准标签。这是我第一次使用 cypress,所以我希望我没有错过任何明显的事情。
我正在映射 Carsdata,它是 json。无法在 cypress 中测试这一点。
尝试过:
cy.get(#any-make-dropdown).select('chevroletMalibu')
Run Code Online (Sandbox Code Playgroud)
以及其他选择。
<FormControl sx={{ m: 1,width: 300, bgcolor: 'whitesmoke' }}>
<InputLabel id="demo-simple-select-label">Any Make</InputLabel>
<Select
id="any-make-dropdown"
value={value}
label="Any Make"
onChange={handleChange}
>
{Carsdata.map((c) => (
<MenuItem key={c.Id} value={c.Name}>
{c.Name}
</MenuItem>
))}
</Select>
</FormControl>
Run Code Online (Sandbox Code Playgroud)
<FormControl sx={{ m: 1,width: 300, bgcolor: 'whitesmoke' }}>
<InputLabel id="demo-simple-select-label">Any Make</InputLabel>
<Select
id="any-make-dropdown"
value={value}
label="Any Make"
onChange={handleChange}
>
{Carsdata.map((c) => (
<MenuItem key={c.Id} value={c.Name}>
{c.Name}
</MenuItem>
))}
</Select>
</FormControl>
Run Code Online (Sandbox Code Playgroud) 我刚刚开始将 mochawesome 与 Cypress (9.7) 一起使用。我们的测试结构基本上是许多规范文件,每个文件都遵循以下格式:
\ndescribe(\'(A): description of this spec\', () => {\n describe (\'(B): description of test abc\', () => {\n before(() => {\n // do specific set up bits for this test\n })\n it(\'(C): runs test abc\', ()\xc2\xa0=> {\n // do actual test stuff\n })\n })\n})\nRun Code Online (Sandbox Code Playgroud)\n在每个规范文件中,将有一个“A”描述块,但可以有许多“B”级块(每个块都有一个“C”) - 这样做是因为之前的块每个“C”总是不同的 - 我不能使用 beforeEach。
\n当我运行各种规格文件时,每个规格文件的结构都与上面类似,mochaewsome 输出大部分是正确的 - 我在“A”级别为每个规格文件获得一个可折叠块,每个文件在 B 级都有多个可折叠块,每个块都有测试信息符合 C 级预期。
\n但是...圆形图表仅在 B 级显示。我所希望的是,可能有 A 级的聚合图表,以及所有 A 级块的进一步聚合图表。
\n不确定我是否已经很好地解释了这一点(!),但希望有人理解,并可以提供建议?!
\ncypress ×10
javascript ×3
material-ui ×2
mocha.js ×2
reactjs ×2
aggregate ×1
alias ×1
input ×1
intellisense ×1
interception ×1
jquery ×1
mochawesome ×1
package.json ×1
repository ×1
testing ×1
typescript ×1
version ×1