I have been struggling with these lines of Protractor code today:
element(by.linkText("People")).click();
browser.waitForAngular();
var url = browser.getCurrentUrl();
...
It appears that getCurrentUrl
always fails when placed after a waitForAngular()
statement.
The error output is too vague:
UnknownError: javascript error: document unloaded while waiting for result
So, what is the correct way to click on a hyperlink and check the new url?
Here are my tests:
If I getCurrentUrl()
before the link is clicked,
it('can visit people page', function () {
var url = browser.getCurrentUrl();
element(by.linkText("People")).click();
expect(true).toBe(true);
});
The test will pass.
If I getCurrentUrl()
after the link is clicked,
it('can visit people page', function () {
var url = browser.getCurrentUrl();
element(by.linkText("People")).click();
expect(true).toBe(true);
url = browser.getCurrentUrl();
});
An error is thrown in Protractor with the UnknownError
output above. What went wrong?
wait(function() { return browser. getCurrentUrl(). then(function(url) { return /index/. test(url); }); }, 10000, "URL hasn't changed");
Browser API is one of the most used API in Protractor, most of the code always dependent on Browser API. Browser API not just controls the operation of browser methods but also all the webdriver related activity will be taken care of by this. In Protractor, "browser" is an instance of ProtractorBrowser class.
Since Protractor 4.0.0 you can now use expected conditions to know when your url changed. The asnwer I found comes from this one, I have no real credits for it.
Once the Protractor is set up, in order to perform the test, one needs a spec file and a configuration file. While the spec file is the actual test script, the configuration file specifies the details of the test such as where to find the test files, which browser and framework are to be used for running them along with other configurations.
When performing tests on a remote server, Protractor can be used to test Cross Browser Compatibility for a wide range of devices using a Real Device Cloud. BrowserStack’s real device cloud provides access to a fleet of 2000+ desktop browsers and real mobile devices like iPhone, iPad, Galaxy, OnePlus, Pixel, Xiaomi, and Redmi, to name a few. 5.
When come to Angular JS based application we don’t have to worry too much because we have Protractor in place. Google strongly recommends Selenium Protractor for end-to-end automation for AngularJS because both are maintained and owned by Google, and they build JavaScript test automation framework to handle AngularJS component in a better manner.
Instead of waitForAngular()
call, wait for the URL to change:
browser.wait(function() {
return browser.getCurrentUrl().then(function(url) {
return /index/.test(url);
});
}, 10000, "URL hasn't changed");
Originally suggested by @juliemr at UnknownError: javascript error: document unloaded while waiting for result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With