Reputation: 5637
In other words, I have just selected a dropdown option successfully; however, I would like to now assert that the option text
is the expected value.
Note that I have selected the dropdown option value with elementSwitcher.element(by.linkText(option)).click()
below :
this.selectDropdown = function (name, option, uniqueId) {
var elem = element(by.id(uniqueId));
var elementSwitcher = elem.element(by.css("div[uib-dropdown]"));
elementSwitcher.element(by.css("button[uib-dropdown-toggle]")).click();
elementSwitcher.element(by.linkText(option)).click().then(function () {
var el = elementSwitcher.element(by.tagName('a')).getText();
console.log('*** JUST CLICKED. el text = ', el);
});
};
and the rendered HTML looks something like this :
<div class="btn-group dropdown open" uib-dropdown="">
<ul class="dropdown-menu accounts-dropdown" uib-dropdown-menu="" aria-labelledby="simple-dropdown">
<li ng-repeat="accountChoice in ctrl.accountOptions" class="ng-scope">
<a ng-click="ctrl.selectOption(accountChoice)" class="ng-binding">Assets</a>
</li>
</ul>
</div>
Problem is that I'm not getting access to the Assets
text I would expect, and my console.log()
is telling me:
W/element - more than one element found for locator By(css selector,
a) - the first result will be used
*** JUST CLICKED. el text = ElementFinder {
browser_:
ProtractorBrowser {
controlFlow: [Function],
Again, I would like to click the dropdown element (a
tag), then get the text
part of that same a
tag which was clicked.
Advice is appreciated.
**** UPDATE ****
As per Alex's answer below, I post my final helper function:
/**
* Helper function to select an angular-ui dropdown option, and return the <a> link text which was selected.
*
* @param name
* @param option
* @param uniqueId
*
* @returns {Promise} A promise that returns the link text when resolved.
*/
this.selectUibDropDownOption = function (name, option, uniqueId) {
var deferred = protractor.promise.defer();
var elem = element(by.id(uniqueId)); // i.e. by.id('drpAccount')
var elementSwitcher = elem.element(by.css("div[uib-dropdown]"));
elementSwitcher.element(by.css("button[uib-dropdown-toggle]")).click();
// i.e. by.linkText('Liabilities and Equity')
//elementSwitcher.element(by.linkText(option)).click(); // also works, but doesn't get link text
var link = elementSwitcher.element(by.linkText(option));
// Important to first get the text of the <a> tag, then click on the link.
link.getText().then(function (linkText) {
link.click();
deferred.fulfill(linkText);
});
return deferred.promise;
};
and from the calling function, where my describe/it
is defined :
var specItem = it(item.name, function () {
item.search.map(function (srchItem) {
// i.e. { "name": "Account", "option": "Assets", "uniqueId": "drpAccount" },
var optionText = pageObjects.selectUibDropDownOption(srchItem.name, srchItem.option, srchItem.uniqueId);
optionText.then(function (linkText) {
console.log('** Clicked on: ', linkText);
expect(srchItem.option).toEqual(linkText);
});
});
});
Upvotes: 2
Views: 3500
Reputation: 473863
What you see on the console is the string representation of a promise - getText()
returns a promise. If you want to see the actual link text value, resolve the promise:
elementSwitcher.element(by.tagName('a')).getText().then(function (linkText) {
console.log('*** JUST CLICKED. el text = ', linkText);
});
// or elementSwitcher.element(by.tagName('a')).getText().then(console.log);
Also, if you get the "stale element reference" or "element not found" error, you may need to get the text before actually clicking the link.
Or, why don't do it this way - find the link by the link text, get the text and click it:
var link = elementSwitcher.element(by.linkText(option));
link.getText().then(function (linkText) {
console.log(linkText);
link.click();
});
Upvotes: 3