31

I don't really understand what the $ and $$ commands are for. I thought they are just a replacement for 'by.css' but why the $$?

<element id = "eId"></element>

I thought, that given the above, these would be equivalent:

element(by.css('#eId'));

and

element($('#eId'));

However, the first one works and the second doesn't. Why, what's the difference between the three?

The docs are of little help. They seem to imply that "$" is for chaining only, e.g. element(by.css('#eId')).element($('#childId')); or "Select the first element, and then select the second element within the first element.' However, I have seen examples with $ being used to select the first element.

Anyway, that's a lot of text for "What are the differences between the three (by.css, $, and $$)?"

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
VSO
  • 11,546
  • 25
  • 99
  • 187
  • 1
    I viewed this because it had "bling bling" in the title lol :) – gerl Aug 07 '15 at 16:14
  • 3
    Just keepin it gangsta yo. Anyway, apparently 'bling bling' is an actual nickname for the '$$' shortcut, from reading the other posts here. Also, if I understand correctly, you can't search posts/google by symbols such as '$' or '#', etc, so I included the nickname in the name. – VSO Aug 07 '15 at 16:19
  • I get you. It's all good in the 'hood. :D – gerl Aug 07 '15 at 16:21

1 Answers1

40

$ and $$ are just convenient shortcuts.

$("selector") is an alternative for element(by.css("selector")).

$$("selector") is an alternative for element.all(by.css("selector")).


FYI, quote from the source code:

ElementFinder.prototype.$ = function(selector) {
  return this.element(webdriver.By.css(selector));
};

ElementArrayFinder.prototype.$$ = function(selector) {
  return this.all(webdriver.By.css(selector));
};

And the actual commit that initially made it happen.

Graham
  • 7,431
  • 18
  • 59
  • 84
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Clear and concise, thanks. Got both $ and $$ to work. I guess I was confusing myself over the basics. I was doing element.$() instead of just $(). Thanks. Lol, I feel like we are going to build a database of protractor basics here if I keep failing at writing scripts. – VSO Aug 07 '15 at 15:41