我的代码太复杂了,我应该如何让它变得简单?
My code is too complicated how should I make it simple?
我在使 Cypress 代码更加精简方面遇到了问题。
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(0).dblclick().type('{uparrow}', {multiple: true, force: true})
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(1).dblclick().type('{uparrow}', {multiple: true, force: true})
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(2).dblclick().type('{uparrow}', {multiple: true, force: true})
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(3).dblclick().type('{uparrow}', {multiple: true, force: true})
我应该如何简化它,使其不成为其自身的复制粘贴?
您可以使用循环
for (const el of [0, 1, 2, 3]) {
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(el).dblclick().type('{uparrow}', {multiple: true, force: true});
}
或数组法:
[0, 1, 2, 3].forEach(el => {
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(el).dblclick().type('{uparrow}', {multiple: true, force: true});
});
您可以使用 Cypress 的 .each
命令。 Documentation here
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').each(($el, index) => {
cy.wrap($el).dblclick().type('{uparrow}', {multiple: true, force: true})
});
如果您只需要对前 4 个项目采取行动,您可以根据 argument
参数的值执行条件。
...
if (index < 4) {
cy.wrap($el).dblclick().type('{uparrow}', {multiple: true, force: true})
}
...
我在使 Cypress 代码更加精简方面遇到了问题。
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(0).dblclick().type('{uparrow}', {multiple: true, force: true})
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(1).dblclick().type('{uparrow}', {multiple: true, force: true})
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(2).dblclick().type('{uparrow}', {multiple: true, force: true})
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(3).dblclick().type('{uparrow}', {multiple: true, force: true})
我应该如何简化它,使其不成为其自身的复制粘贴?
您可以使用循环
for (const el of [0, 1, 2, 3]) {
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(el).dblclick().type('{uparrow}', {multiple: true, force: true});
}
或数组法:
[0, 1, 2, 3].forEach(el => {
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').eq(el).dblclick().type('{uparrow}', {multiple: true, force: true});
});
您可以使用 Cypress 的 .each
命令。 Documentation here
cy.get('input[ng-change="$matterHourCtrl.forceRound()"]').each(($el, index) => {
cy.wrap($el).dblclick().type('{uparrow}', {multiple: true, force: true})
});
如果您只需要对前 4 个项目采取行动,您可以根据 argument
参数的值执行条件。
...
if (index < 4) {
cy.wrap($el).dblclick().type('{uparrow}', {multiple: true, force: true})
}
...