Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use browser.getCurrentUrl() in a protractor test?

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?

like image 577
Blaise Avatar asked Mar 18 '15 17:03

Blaise


People also ask

How do I use getCurrentUrl in protractor?

wait(function() { return browser. getCurrentUrl(). then(function(url) { return /index/. test(url); }); }, 10000, "URL hasn't changed");

What is protractor browser?

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.

Does protractor know when your URL changes?

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.

How do I perform a test in protractor?

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.

Can protractor be used to test cross browser compatibility?

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.

Should I use protractor or selenium protractor for testing AngularJS based application?

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.


1 Answers

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.

like image 192
alecxe Avatar answered Nov 15 '22 03:11

alecxe