如何在 Cypress 中点击 x 次
How to click x number of times in Cypress
我的网站上有一个对象列表,所有对象旁边都有 'Add' 按钮。当单击第一个 'Add' 按钮时,该对象被添加并且该行消失并被下一个替换。对象名称相同。在保存之前,我想 .click() 三次以添加列表中的前三个对象。我该怎么做?
我知道 .click() 可以单击单个对象。我也知道 .click ({ multiple: true}) 单击页面上的所有对象。但是,我希望它在第三次点击后停止。
目前设置点击多次添加列表中的所有对象(不正确):
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click({ multiple: true });
问题出在您的选择器上。如果你有 n 个按钮,并且你想点击所有按钮,那么你需要匹配所有按钮,而不是只匹配一个按钮。因此,寻找一个匹配所有选择器的选择器(例如,您的添加按钮独有的 class)。
然后,您可以使用 .each() 遍历它们并在达到某个索引时跳出循环:
cy.get('#your_selector_to_your_add_buttons')
.each(($el, $index) => {
if($index == 3){
return false;
}
cy.wrap($el).click()
} )
要锤击一个按钮,你可以使用这个:
for(let n = 0; n < 10; n ++){
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.click()
}
multiple: true
用于点击多个元素,例如点击页面上的所有按钮。
你只需要在点击后移动你的断言并说点击直到不存在
.click({ multiple: true })
.should('not.exist');```
你可以使用 lodash
https://lodash.com/docs/4.17.15#times
import { times } from 'lodash'
times(2, () => {
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.click()
})
你也可以这样做:
Cypress._.times(10, () => {
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button').click()
})
我能找到的最好的方法是使用 Wait 几毫秒,或者在继续执行下一个命令之前等待别名资源解析。
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click();
cy.wait(500)
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click();
cy.wait(500)
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click();
根据 Kamran Eyyubov 和 monikapatelIT 的评论,我能够使用 cypress 公开的 Lodash 库想出以下内容来模拟按钮混搭。
// Fetch a reference outside the loop so it can be referenced inside the
// loop.
cy.get('#submit-button')
.as('submitButton');
// Simulate a button mash. Click the button 10 times.
Cypress._.times(10, function () {
cy.get('@submitButton')
// The submit button is disabled on the first click, so
// force is set to TRUE to prevent the test from failing
// due to clicking a disabled element.
.click({force: true});
});
我的网站上有一个对象列表,所有对象旁边都有 'Add' 按钮。当单击第一个 'Add' 按钮时,该对象被添加并且该行消失并被下一个替换。对象名称相同。在保存之前,我想 .click() 三次以添加列表中的前三个对象。我该怎么做?
我知道 .click() 可以单击单个对象。我也知道 .click ({ multiple: true}) 单击页面上的所有对象。但是,我希望它在第三次点击后停止。
目前设置点击多次添加列表中的所有对象(不正确):
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click({ multiple: true });
问题出在您的选择器上。如果你有 n 个按钮,并且你想点击所有按钮,那么你需要匹配所有按钮,而不是只匹配一个按钮。因此,寻找一个匹配所有选择器的选择器(例如,您的添加按钮独有的 class)。 然后,您可以使用 .each() 遍历它们并在达到某个索引时跳出循环:
cy.get('#your_selector_to_your_add_buttons')
.each(($el, $index) => {
if($index == 3){
return false;
}
cy.wrap($el).click()
} )
要锤击一个按钮,你可以使用这个:
for(let n = 0; n < 10; n ++){
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.click()
}
multiple: true
用于点击多个元素,例如点击页面上的所有按钮。
你只需要在点击后移动你的断言并说点击直到不存在
.click({ multiple: true })
.should('not.exist');```
你可以使用 lodash https://lodash.com/docs/4.17.15#times
import { times } from 'lodash'
times(2, () => {
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.click()
})
你也可以这样做:
Cypress._.times(10, () => {
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button').click()
})
我能找到的最好的方法是使用 Wait 几毫秒,或者在继续执行下一个命令之前等待别名资源解析。
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click();
cy.wait(500)
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click();
cy.wait(500)
cy.get('#FieldListDialog > div > table > tr > td > button.button.add-button')
.should('exist')
.click();
根据 Kamran Eyyubov 和 monikapatelIT 的评论,我能够使用 cypress 公开的 Lodash 库想出以下内容来模拟按钮混搭。
// Fetch a reference outside the loop so it can be referenced inside the
// loop.
cy.get('#submit-button')
.as('submitButton');
// Simulate a button mash. Click the button 10 times.
Cypress._.times(10, function () {
cy.get('@submitButton')
// The submit button is disabled on the first click, so
// force is set to TRUE to prevent the test from failing
// due to clicking a disabled element.
.click({force: true});
});