Instead of directly modifying protractor, a wrapper is created to encapsulate the functionality and simplify its usage in tests.
For instance, rather than using the .click() function or manually locating elements, the wrapper provides a method called clickElement for clicking on elements.
resolveSelector: function (selector) {
var el;
if (typeof selector === 'string') {
el = self.getElement(selector);
} else {
el = selector;
}
return el;
},
getElement: function (selector) {
verbose('getElement: ' + selector);
return element(by.css(selector));
},
waitForElement: function (selector) {
verbose('waitForElement: ' + selector);
var el = self.resolveSelector(selector);
return self.wait(function () {
return self.executeScript(function (el) {
return el ? $(el).is(':visible') : false;
}, el.getWebElement()).then(function (val) {
return val;
});
}, 15000, 'Element is not visible. (' + selector + ')');
},
waitForElementClickable: function (selector) {
verbose('waitForElementClickable: ' + selector);
var el = self.resolveSelector(selector);
var condition = function () {
return self.waitForElement(el).then(function () {
return self.isElementEnabled(el);
});
};
return self.wait(condition, 15000, 'Element not clickable. (' + selector + ')');
},
clickElement: function (selector) {
verbose('clickElement: ' + selector);
var el = self.resolveSelector(selector);
return self.waitForElementClickable(el).then(function () {
return el.click();
});
},
The clickElement
function within the wrapper performs necessary actions, with verbose
used for logging when a certain configuration option is enabled.
To ensure test reliability and flexibility for adjustments, it's recommended to utilize a custom driver instead of relying solely on direct protractor usage, especially useful when working with a selenium cloud service for executing tests.
In our tests, we refer to driver.clickElement('#someID');
or
driver.clickElement('.someClass');
.
Over time, our driver has expanded significantly to accommodate various needs.