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();
clinton3141
  • 4,751
  • 3
  • 33
  • 46
Parthi
  • 142
  • 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
  • 14,128
  • 14
  • 53
  • 77
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,431
  • 18
  • 59
  • 84
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195