Adding custom locator shortcuts to Protractor

The Protractor has and convenient labels for selectors CSS: $$$

$(".myclass")  // means: element(by.css(".myclass"))
$$(".myclass")  // means: element.all(by.css(".myclass"))

Can I enter custom shortcuts for other locators?


To be more specific, what to do if we want to have the labels $rand $$rfor calls "repeaters." To be able to write:

$r("item in items")  
$$r("item in items")

instead:

element(by.repeater("item in items"))
element.all(by.repeater("item in items"))
+4
source share
1 answer

To create a shortcut, add a custom locator to the global namespace and prototype ElementFinderand ElementArrayFinder:

global.$r = function(selector) {
  return protractor.element(protractor.by.repeater(selector));
};

global.$$r = function(selector) {
  return protractor.element.all(protractor.by.repeater(selector));
};

ElementFinder.prototype.$$r = function(selector) {
  return this.all(protractor.by.repeater(selector));
};

ElementFinder.prototype.$r = function(selector) {
  return this.element(protractor.by.repeater(selector));
};

ElementArrayFinder.prototype.$$r = function(selector) {
  return this.all(protractor.by.repeater(selector));
};

Using:

$r("item in items")
$$r("item in items")
$("#id").$r("item in items")
$("#id").$$r("item in items")
+4
source

All Articles