-5

I'm testing an app with Protractor; I want to simulate a click on a button 5 times but I don't want to write the same code x5. how can I do it?

element(by.css('button.click')).click();
ZaitseV
  • 179
  • 2
  • 14

4 Answers4

4

Use loop

for(i=0; i<5; i++) {
  element(by.css('button.click')).click();
}
Munawir
  • 3,308
  • 8
  • 32
  • 49
1

Loops offer a quick and easy way to do something repeatedly.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

If you're looking for Protractor with loops:

Using protractor with loops

Community
  • 1
  • 1
Jry9972
  • 433
  • 7
  • 16
0

I think you should read documentation ... this is a basic

for (i = 0; i < 5; i++) { 
    element(by.css('button.click')).click();
}
ad_revenge
  • 91
  • 4
0

Alternatively, you can also do it with browser.actions() chaining the click actions:

var link = element(by.css('button.click'));

actions = browser.actions();
for (i = 0; i < 5; i++) {
    actions = actions.click(link);
}
actions.perform();

As a side note, you can replace element(by.css('button.click')) with $('button.click') - protractor supports $ and $$ for CSS locators.

alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148