1

How to click the same button more than 50 times by using loop statement in protractor? Will protractor support this action?

Here's my locator :

var nudge= element(by.xpath("//a[@class='isd-flat-icons fi-down']"));
nudge.click();
iblamefish
  • 4,631
  • 3
  • 32
  • 44
Parthi
  • 132
  • 1
  • 1
  • 12

2 Answers2

2

You can try simple for loop in javascript:

var nudge= element(by.xpath("//a[@class='isd-flat-icons fi-down']"));

for (i = 0; i < 50; i++) { 
    nudge.click();
}

The above script will click the button exactly 50 times. Before implementing this script consider:

  • The above script will click the button as fast as possible
  • Some sites can become unresponsive after even such small load
Pavel Janicek
  • 13,368
  • 13
  • 49
  • 76
1

You can also do this through the browser actions (should be better performance-wise, since actions are sent in a single command when you "perform" them):

var nudge = $("a.isd-flat-icons.fi-down");

var actions = browser.actions();
for (i = 0; i < 50; i++) { 
    actions = actions.click(nudge);
}
actions.perform();

Note that, if you want to introduce a delay between every click action, you can do that by having a custom "sleep" browser action:

var nudge = $("a.isd-flat-icons.fi-down");

var actions = browser.actions();
for (i = 0; i < 50; i++) { 
    actions = actions.click(nudge).sleep(500);
}
actions.perform();

The $ here is a shortcut for the "by.css" locator, which would be, generally speaking and according to the Style Guide, a better choice when using an XPath location technique.

Graham
  • 7,035
  • 17
  • 57
  • 82
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148