How to use complex locators when testing with Jasmine and Protractor
Can anyone provide me with an example of how I can use multiple locators when testing using Jasmine and Protractor?
Let's say I have 3 list items in LINKS view, and all of these LIST OF LISTS have the same attributes; However, they are each links with a unique name to navigate to the new "Angular State" ...
For example, all three links have the following in them:
<li> ng-repeat="bb" ng-class="{'active': dropdown.isActive}" class="ng-scope active"</li>
and then each of the links have their own names like:
<a ui-sref="search" class="ng-binding" href="#/Search"> Search </a>
<a ui-sref="home" class="ng-binding" href="#/Search"> Home </a>
<a ui-sref="todo" class="ng-binding" href="#/Search"> ToDo </a>
The only thing that is unique to the three-page objects:
ui-sref="search"
, ui-sref="home"
,ui-sref="todo"...
How can I use one common locator from the list items and a unique locator from the link item to ensure that I am interacting with the correct link every time?
source to share
I am using a custom locator to work with ui-sref
:
// Usage:
var searchLinkElm = element(by.uisref('search'));
var homeLinkElm = element(by.uisref('home'));
var todoLinkElm = element(by.uisref('todo'));
// Interaction
it('works', function() {
expect(searchLinkElm.isPresent()).toBeTruthy();
searchLinkElm.click();
});
// within your onPrepare block
by.addLocator('uisref', function(toState, opt_parentElement) {
var using = opt_parentElement || document;
var prefixes = ['ui-sref'];
for (var p = 0; p < prefixes.length; ++p) {
var selector = '*[' + prefixes[p] + '="' + toState + '"]';
var inputs = using.querySelectorAll(selector);
if (inputs.length) {
return inputs;
}
}
});
source to share