Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...