Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 8348803

Browse files
committed
docs(element): add in-code documentation for methods on element
1 parent cc4f7b5 commit 8348803

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

lib/protractor.js

+75-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ var buildElementHelper = function(ptor, opt_usingChain) {
6262
return base;
6363
}
6464

65+
/**
66+
* The element function returns an Element Finder. Element Finders do
67+
* not actually attempt to find the element until a method is called on them,
68+
* which means they can be set up in helper files before the page is
69+
* available.
70+
*
71+
* Example:
72+
* var nameInput = element(by.model('name'));
73+
* browser.get('myurl');
74+
* nameInput.sendKeys('Jane Doe');
75+
*
76+
* @param {webdriver.Locator} locator
77+
* @return {ElementFinder}
78+
*/
6579
var element = function(locator) {
6680
var elementFinder = {};
6781

@@ -85,17 +99,46 @@ var buildElementHelper = function(ptor, opt_usingChain) {
8599
return using().findElement(locator).findElement(subLocator);
86100
};
87101

102+
/**
103+
* Return the actual WebElement.
104+
*
105+
* @return {webdriver.WebElement}
106+
*/
88107
elementFinder.find = function() {
89108
return using().findElement(locator);
90109
};
91110

111+
/**
112+
* @return {boolean} whether the element is present on the page.
113+
*/
92114
elementFinder.isPresent = function() {
93115
return using().isElementPresent(locator);
94116
};
95117

118+
/**
119+
* Calls to element may be chained to find elements within a parent.
120+
* Example:
121+
* var name = element(by.id('container')).element(by.model('name'));
122+
* browser.get('myurl');
123+
* name.sendKeys('John Smith');
124+
*
125+
* @param {Protractor} ptor
126+
* @param {=Array.<webdriver.Locator>} opt_usingChain
127+
* @return {function(webdriver.Locator): ElementFinder}
128+
*/
96129
elementFinder.element =
97130
buildElementHelper(ptor, usingChain.concat(locator));
98131

132+
/**
133+
* Shortcut for chaining css element finders.
134+
* Example:
135+
* var name = element(by.id('container')).$('input.myclass');
136+
* browser.get('myurl');
137+
* name.sendKeys('John Smith');
138+
*
139+
* @param {string} cssSelector
140+
* @return {ElementFinder}
141+
*/
99142
elementFinder.$ = function(cssSelector) {
100143
return buildElementHelper(ptor, usingChain.concat(locator))(
101144
webdriver.By.css(cssSelector));
@@ -105,24 +148,43 @@ var buildElementHelper = function(ptor, opt_usingChain) {
105148
};
106149

107150
/**
108-
* @type {function(webdriver.Locator): ElementArrayFinder}
151+
* element.all is used for operations on an array of elements (as opposed
152+
* to a single element).
153+
*
154+
* Example:
155+
* var lis = element.all(by.css('li'));
156+
* browser.get('myurl');
157+
* expect(lis.count()).toEqual(4);
158+
*
159+
* @param {webdriver.Locator} locator
160+
* @return {ElementArrayFinder}
109161
*/
110162
element.all = function(locator) {
111163
var elementArrayFinder = {};
112164

165+
/**
166+
* @return {number} the number of elements matching the locator.
167+
*/
113168
elementArrayFinder.count = function() {
114169
return using().findElements(locator).then(function(arr) {
115170
return arr.length;
116171
});
117172
};
118173

174+
/**
175+
* @param {number} index
176+
* @return {webdriver.WebElement} the element at the given index
177+
*/
119178
elementArrayFinder.get = function(index) {
120179
var id = using().findElements(locator).then(function(arr) {
121180
return arr[index];
122181
});
123182
return ptor.wrapWebElement(new webdriver.WebElement(ptor.driver, id));
124183
};
125184

185+
/**
186+
* @return {webdriver.WebElement} the first matching element
187+
*/
126188
elementArrayFinder.first = function() {
127189
var id = using().findElements(locator).then(function(arr) {
128190
if (!arr.length) {
@@ -133,17 +195,29 @@ var buildElementHelper = function(ptor, opt_usingChain) {
133195
return ptor.wrapWebElement(new webdriver.WebElement(ptor.driver, id));
134196
};
135197

198+
/**
199+
* @return {webdriver.WebElement} the last matching element
200+
*/
136201
elementArrayFinder.last = function() {
137202
var id = using().findElements(locator).then(function(arr) {
138203
return arr[arr.length - 1];
139204
});
140205
return ptor.wrapWebElement(new webdriver.WebElement(ptor.driver, id));
141206
};
142207

208+
/**
209+
* @type {webdriver.promise.Promise} a promise which will resolve to
210+
* an array of WebElements matching the locator.
211+
*/
143212
elementArrayFinder.then = function(fn) {
144213
return using().findElements(locator).then(fn);
145214
};
146215

216+
/**
217+
* Calls the input function on each WebElement found by the locator.
218+
*
219+
* @param {function(webdriver.WebElement)}
220+
*/
147221
elementArrayFinder.each = function(fn) {
148222
using().findElements(locator).then(function(arr) {
149223
arr.forEach(function(webElem) {

0 commit comments

Comments
 (0)