protractor: element ... .getAttribute(...) returning Objects unexpectedly

I’m collecting all of the anchor tags on a page with the intent of checking to ensure that they aren’t dead links… but I’m unexpectedly finding that I’m unable to get valid HREF values for half of them.

element.all(by.css('a')).each(function (element) {
    var linkTarget = element.getAttribute('href');
    expect(typeof linkTarget).toBe("string");
});

If I run code in the browser on the same site, I find that all of my anchor tags have an href of type “string”. Am I using the API incorrectly? I’m using Protractor on a non-Angular page: does this functionality depend on Angular?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Your problem is that element.getAttribute() is returning a Promise. You want to do something like:

element.all(by.css('a')).each(function (element) {
    var linkTarget = element.getAttribute('href').then(function(attr) {;
        expect(typeof attr).toBe("string");
    });
});

Since pretty much everything in Protractor returns a promise I was wondering when is required to then and when it is not.

As per this excerpt from the web page, why is this OK

// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);

instead of the following?

// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click().then(function() {
    var completedAmount = element.all(by.css('.done-true'));
    expect(completedAmount.count()).toEqual(2);
});

Do I have any guarantee that what is triggered by the click has finished executing by the time the rest of the code is executed?

Compare the values to two elements…

driver.findElement(By.id('input1')).getAttribute('value').then(function(a) {
  driver.findElement(By.id('input2')).getAttribute('value').then(function(b) {
    expect(a).to.equal(b);
  });
});

Anyone know a better way to do this?