Reputation: 18553
I'm using Protractor to test a web application and I've noticed that whenever I try to read an href
attibute, Protractor treats the value as an absolute URL. This prevents me from verifying that the URL rendered by my application is indeed absolute.
Let's say I want to have an absolute URL, including the domain in my meta tags:
<link rel="next" href="https://www.example.com/path-to-page">
It turns out that when I read this element in Protractor, it doesn't matter what the actual value is, I still get an absolute URL.
Let's say the back-end (https://www.example.com) actually renders the following tag:
<link rel="next" href="/path-to-page">
accessing it in Protractor using
element(by.css('link[rel="next"]')).getAttribute('href')
yields https://www.example.com/path-to-page
and not /path-to-page
, which is the actual value when I view the page source using the browser or curl
.
I find this approach overzealous. It can hide actual bugs. Is this a bug or a feature? What's the best way to get the actual path present in the attribute?
I've found this issue on Github but it was closed with a comment suggesting that the reporter seeks help on Stack Overflow.
Keep in mind that I don't just want to check the path part of the URL. I want to confirm that the attribute does indeed contain everything. The protocol, the domain, the path. If my back-end renders it differently, Protractor still pretends that it's correct because it figures out the domain based on the context of the page.
Upvotes: 1
Views: 239
Reputation: 694
Using browser.executeScript();
with Javascript will give you the actual href, and not the full link that Protractor (annoyingly) resolves.
Example:
let elem = element(by.css('link[rel="next"]'));
let url = await browser.executeScript('return arguments[0].getAttribute("href");',elem);
Upvotes: 1