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

Today I am struggling with these lines of Protractor code:

element(by.linkText("People")).click(); browser.waitForAngular(); var url = browser.getCurrentUrl(); ... 

It seems that getCurrentUrl always fails when placed after the waitForAngular() statement.

The error output is too foggy:

UnknownError: javascript error: document unloaded while waiting for result

So, what is the correct way to click on a hyperlink and check for a new URL?


Here are my tests:

If I getCurrentUrl() before clicking the link,

 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 clicking the link,

 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 UnknownError in Protractor with an UnknownError output above. Something went wrong?

+5
source share
2 answers

Instead of calling waitForAngular() 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 in UnknownError: JavaScript error: document uploaded while waiting for result .

+9
source

This piece of code is working 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); }); }) }); 
+2
source

Source: https://habr.com/ru/post/1215615/


All Articles