Spicchio
Spicchio

Reputation: 136

Protractor, checking the target "_blank" of an element

I'm using cucumber with protractor for test my app. I have a problem when I try to check that the value of the attribute "target" of an element, is "_blank".

Here my html:

<div class="cta-group">
    <div class="cta-link">
        <a href="https://www.starting_to_write.com/" aria-label="Writing for the holidays" target="_blank" data-linktext="Writing for the holidays" data-clicktype="general" data-is-click-tracking-enabled="true">
            Writing for the holidays
        </a>
    </div>
</div>

Here my attempt to get and check that the value of "target" is "_blank":

Then('I want to see a CTA link with a target blank to open the writing for the holidays', {timeout: 90 * 1000}, function (next) {
  let cta_class = element(by.css('a[data-linktext="Writing for the holidays"]'));
  let target = cta_class.getAttribute("target");
  //expect(target).to.equal('_blank');
  target.getText().then(function(text){
        console.log("target at the moment is: ",text);
        return expect(text).to.equal('_blank');
  });
  expect(cta_class.isPresent()).to.eventually.be.true;
  next();

});

When I launch the test, the console.log don't show "target at the moment is: " and don't check nothing for the expectation. Can someone help me? Thank you.

Upvotes: 0

Views: 462

Answers (1)

Farmijo
Farmijo

Reputation: 245

You don't need to use

target.getText() 

there. That is a method for elements, and target, in your case, is a promise (that will be the value that you are expecting for assert when it turns resolved. Check it: https://www.protractortest.org/#/api?view=webdriver.WebElement.prototype.getAttribute).

In your case, something like this should work:

cta_class.getAttribute("target").then( (value) => {
    return expect(text).to.equal('_blank');

})

Upvotes: 1

Related Questions