我阅读了有关赛普拉斯条件测试的文档中的警告,但由于某些原因仍然需要将其应用于特定测试。
我有一个函数可以做到这一点,但由于该函数中缺乏重试,某些选择器无法工作。
如何在条件测试中实现重试并避免不稳定的测试?
这是否有可能,或者一件事可以抵消另一件事吗?
export function elementExists(selector: string): boolean {
try {
return Cypress.$(selector).length > 0;
} catch (error) {
return false;
}
Run Code Online (Sandbox Code Playgroud) 此代码块用于读取 Excel 文件并按给定用户角色获取用户数据。
但如果excel文件中不存在该用户角色,则会返回一个未定义的值。
我们如何检查变量user
是否未定义或为空?
cy.task('getExcelData', Cypress.env('usersFilePath')).then((users) => {
const user = users.find(user => {
return user.userRole === 'userRole';
});
cy.wrap(user).should('not.be.empty');
cy.wrap(user).should('not.be.a',undefined)
cy.wrap(user).should('not.be.a',null)
signIn(user.username, user.password);
});
Run Code Online (Sandbox Code Playgroud)
cy.wrap(user).should('not.be.empty')
(这部分有效,但其他部分无效)
这是我收到的错误:
我正在尝试将 Snowflake 集成到我的 Cypress 测试中,但它在编译时不断抛出错误。错误:
Error: Webpack Compilation Error
./node_modules/vm2/lib/resolver-compat.js
Module not found: Error: Can't resolve 'async_hooks' in 'C:\snowflake\node_modules\vm2\lib'
resolve 'async_hooks' in 'C:\snowflake\node_modules\vm2\lib'
Parsed request is a module
using description file: C:\snowflake\node_modules\vm2\package.json (relative path: ./lib)
Field 'browser' doesn't contain a valid alias configuration
Run Code Online (Sandbox Code Playgroud)
尝试了以下简单步骤:
const snowflake = require("snowflake-sdk");
Run Code Online (Sandbox Code Playgroud)
当我运行规范文件时,出现上述错误。
如果我将错误跟踪到文件resolver-compat.js
,我可以看到导入失败的地方。
const {AsyncResource} = require('async_hooks');
Run Code Online (Sandbox Code Playgroud)
我已经手动完成了 npm installasync_hooks
但也没有运气。
包.json
"devDependencies": {
"cypress": "^11.2.0"
},
"dependencies": {
"async_hooks": "^1.0.0",
"snowflake-sdk": "^1.6.16"
}
Run Code Online (Sandbox Code Playgroud) javascript snowflake-cloud-data-platform cypress async-hooks cypress-task
我有一个前端 Angular 应用程序,在其中实现 Cypress 测试。
但 XLSX 文件的读取并不总是正确。
如果我这样做:
cy.readFile('cypress/downloads/myfile.XLSX');
Run Code Online (Sandbox Code Playgroud)
它实际上确实按预期读取了文件。
但是如果我这样做:
cy.readFile('cypress/downloads/myfile.XLSX').should('contain', 'mytext');
Run Code Online (Sandbox Code Playgroud)
即使我知道它在文件中,它也不会读取“mytext”。
我知道我在语法上是正确的,因为它可以很好地读取 .xls 文件中的文本。
有什么方法可以让 cypress 查找带有 .XLSX 文件的文本吗?
非常感谢。
我想用这个界面创建一个命令:
cy.getTableCell({ column: 'Name', row: 42 }).contains('Douglas Adams')
Run Code Online (Sandbox Code Playgroud)
where将返回与表的第 42 行对应的getTableCell
表单元格 ( ) 上的列。我想出了这个实现:td
'Name'
type GetTableCellParams = {
columnName: string;
rowIndex: number;
};
Cypress.Commands.add(
'getTableCell',
({ columnName, rowIndex }: GetTableCellParams) => {
cy.contains('th', columnName)
.invoke('index')
.then((index) => {
cy.get('tr')
.eq(rowIndex)
.within((row) => {
return cy.get('td').eq(index);
});
});
}
);
Run Code Online (Sandbox Code Playgroud)
它确实找到了正确的表格单元格。但是,由于它在回调中执行此操作,因此我无法对其执行任何操作 - 我希望能够调用可链接的方法,例如contains
、click
等。我如何重构它以便调用者可以访问此元素,能够调用contains
、click
和其他可链接的方法吗?
我还可以在函数可读性方面使用一些帮助。它看起来一团糟 - 我猜问题出在嵌套回调上......
我可以在同一个存储库中运行测试*.feature
吗?*.cy.ts
我的 cypress.config.ts 应该是什么样子?它们被列在当前的 E2E 规范中
specPattern: 'cypress/e2e/**/*.feature|cypress/e2e/**/*.cy.ts',
Run Code Online (Sandbox Code Playgroud)
列出两者,但都无法执行,预计至少 .feature 文件能够正确执行
specPattern: 'cypress/e2e/**/*.feature',
Run Code Online (Sandbox Code Playgroud)
工作正常,但我仍然需要列出 *.cy.ts 并使用 cypress 正确执行
e2e: {
async setupNodeEvents(on, config) {
const bundler = createBundler({
plugins: [createEsbuildPlugin(config)],
});
on('file:preprocessor', bundler);
await addCucumberPreprocessorPlugin(on, config);
return config;
},
specPattern: 'cypress/e2e/**/*.feature|cypress/e2e/**/*.cy.ts',
},
Run Code Online (Sandbox Code Playgroud) 我正在尝试从中导入“And”关键字,@badeball/cypress-cucumber-preprocessor
以便我可以在 Cypress/Cucumber 中使用它,但出现此错误
模块“@badeball/cypress-cucumber-preprocessor”没有导出成员“And” - ts(2305)
我不知道为什么。
其他关键字如“Given”、“When”和“Then”都可以正常导入。
我一直想知道如何在 Cypress Desktop (npx cypress open) 上隐藏成功的断言日志,但显示失败的断言日志?
我一直在 stackoverflow (类似的问题)、文档中搜索,我发现的关闭文档只是CYPRESS_NO_COMMAND_LOG=1
. 但是,这不是我所期待的结果。因为当我这样做时,它只是最小化运行程序,实际上并没有运行我的测试用例。因此,所有断言都没有结果。
假设我div
在 DOM 中有一个'200px'
宽度为 a 的class
元素'target'
。
为了用 cypress 测试它的宽度,我应该编写下面的代码:
cy.get('.target').should('have.css', 'width', '200px');
Run Code Online (Sandbox Code Playgroud)
还有一堆测试,测试元素的宽度和高度......
...
今天发生了一件奇怪的事情!
width
所有这些测试都失败了,因为发现的柏树的价值200.0000000000032038879956012px
不是200px
!
我想到的第一个解决方案是用实际数字(ex,)来测试它们,而不是柏树找到的200
字符串(ex, );'200px'
我认为这是一个昂贵的想法!
你认为我怎样才能克服这个问题!?
尝试访问 this () 上下文上的别名值,我按照https://docs.cypress.io/api/commands/as#Fixture中的文档进行操作
在 .then 回调中,dataExample 参数已填充正常,但在 this.example 上它是未定义的。
describe('cy.as and this', function () {
it('Testing cy.as and this', function () {
cy.fixture('example.json').as('example')
.then(function (dataExample) {
cy.log('ACCESSING this, dataExample', this, dataExample);
});
cy.log(`ACCESSING this.example : ${JSON.stringify(this['example'])}`, this);
});
});
Run Code Online (Sandbox Code Playgroud)
第一个日志输出以下内容: dataExample 已正确填充,但 contex.example 未定义
.then 之外的第二个日志, this.example 也未定义
如果我将 cy.fixture 行移到 beforeEach() 中,它就会起作用。冷有人解释一下这种行为吗?
describe('alias', () => {
beforeEach(() => {
cy.fixture('example.json').as('example');
});
it('can access all aliases as properties', function () {
expect(this['example']).not.to.eq(undefined); // true
cy.log('THIS', this); // curious …
Run Code Online (Sandbox Code Playgroud) 我想在悬停按钮时验证真实颜色。
我可以看到realhover()
它的作用,改变按钮的颜色,但我总是收到错误:
预期
<a.g-card__item-link>
CSS 属性值为background-color
,rgb(182, 222, 251)
但该值是rgb(207, 233, 252)
测试代码为:
// get link button 1st card hover color before and after
cy.get('.g-card__item-link')
.should('have.css', 'background-color', 'rgb(207, 233, 252)') //color before hover
cy.get('.g-card__item-link')
.realHover()
.should('have.css', 'background-color', 'rgb(182, 222, 251)') //I should get this color on hover
Run Code Online (Sandbox Code Playgroud) cypress ×11
cucumber ×2
cypress-cucumber-preprocessor ×2
javascript ×2
testing ×2
angular ×1
async-hooks ×1
chai ×1
css ×1
cypress-task ×1
e2e-testing ×1
hover ×1
logging ×1
snowflake-cloud-data-platform ×1
xlsx ×1