Search code examples
javascriptprotractorgulp-protractor

Protractor .getAttribute('href') not working


Goal: test that the 'href' value of first element in my 'div' equals to 'myLink'.

Html:

<div id="myId">
    <a href="myLink"/>
</div>

Protractor code:

var elems = element.all(by.id('myId'));
elems.first().getAttribute('href').then(function(attr) {
    expect(attr).toEqual('myLink');
});

Error message: "Expected null to equal 'myLink'."

Question: What is wrong in this code?


Solution

  • You are trying to get the href attribute from the div element.But the div element does'nt have href attribute,so protractors gives you null as output. Try the below code.

    var elems = element.all(by.css('#myId a'));
       elems.first().getAttribute('href').then(function(attr) {
           expect(attr).toEqual('myLink');
       });