admin管理员组

文章数量:1225001

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?

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?

Share Improve this question edited Jul 13, 2015 at 19:08 alecxe 474k127 gold badges1.1k silver badges1.2k bronze badges asked Mar 18, 2015 at 17:48 BlaiseBlaise 22.2k29 gold badges111 silver badges174 bronze badges 5
  • i don't see anywhere in your test where you're actually referencing your url variable. I use something like expect( browser.getCurrentUrl() ).toContain( "contact-us" ); JK, I see it's commented out. – Christopher Marshall Commented Mar 18, 2015 at 18:04
  • No I haven't reference url yet. Just a call to browser.getCurrentUrl breaks the test! – Blaise Commented Mar 18, 2015 at 18:06
  • Have you tried a delay using browser.sleep(2000) then trying your expect block after your link click? – Christopher Marshall Commented Mar 18, 2015 at 18:08
  • 1 Why do we have to delay by sleep? Will browser.waitForAngular() do the delay? – Blaise Commented Mar 18, 2015 at 18:10
  • It should by default, but I've run into so many issues with protractor that sometimes manually triggering events works better. :\ – Christopher Marshall Commented Mar 18, 2015 at 18:12
Add a comment  | 

2 Answers 2

Reset to default 11

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.

This piece of code works correctly

var handlePromise = browser.driver.getAllWindowHandles();
handlePromise.then(function (handles) {
    // parentHandle = handles[0];
    var popUpHandle = handles[1];

    // Change to new handle
    browser.driver.switchTo().window(popUpHandle).then(function() {
        return browser.getCurrentUrl().then(function(url) {
            console.log("URL= "+ url);
        });
    })
});

本文标签: javascriptHow to use browsergetCurrentUrl() in a protractor testStack Overflow