Tips on utilizing browser.getCurrentUrl() within a Protractor examination

I’ve been wrestling with these lines of Protractor code today:

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

It seems that getCurrentUrl always throws an error when placed after a waitForAngular() statement.

The error message is rather cryptic:

UnknownError: javascript error: document unloaded while waiting for result

So, how should I correctly click on a link and verify the new url?


Let’s take a look at my test scenarios:

If I call getCurrentUrl() before clicking on 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 without any issues.

However, if I check getCurrentUrl() after clicking on 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 triggered in Protractor with the UnknownError message mentioned earlier. Where did I make a mistake?

Answer №1

Instead of using waitForAngular(), it is recommended to wait for the URL to change:

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

This approach was originally suggested by @juliemr on a GitHub issue regarding UnknownError: javascript error: document unloaded while waiting for result.

Answer №2

This code snippet is functioning as intended

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

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The button attached to the jQuery .toggle() function requires two clicks to activate

My objective is twofold: To dynamically load the Disqus script embed.js only when the "Show Comments" button is clicked. To toggle the visibility of the disqus_thread div using the same button while also changing the button's text. The issue I am f ...

In the JavaScript example provided, do child classes inherit their parent class prototype?

Here's the code I'm working with: class Rectangle { constructor(w, h) { this.w = w; this.h = h; } } Rectangle.prototype.area = function () { return (this.w * this.h); }; class Square extends Rectangle { construct ...

Having challenges retrieving information from MySQL in AngularJS

As a beginner in angularJS, I am trying to display all customers from MySQL. Here is the code I have written in the controller and service: app.controller('CustomersController', function ($scope, customersService, $http) { init(); function ini ...

Vue.JS encounter a hiccup while attempting to store data in the local storage

My application is set up to fetch data from a rest api, and it consists of two key components: 1 - (main) Display the results 2 - (list) To display a list of selected items from the main results The feature I am trying to implement involves adding a save ...

Enhance the app.get response by integrating data fetched from an http.get request

Currently, I am utilizing express and passing the path as a URL parameter. app.get("/download", function (req, res) { var location; var options = { host: 'example.com', port: 80, path: req.query.url.replace(/ /g, ...

The intended 'this' keyword is unfortunately replaced by an incorrect '

Whenever the this keywords are used inside the onScroll function, they seem to represent the wrong context. Inside the function, it refers to the window, which is understandable. I was attempting to use the => arrow notation to maintain the correct refe ...

What steps can I take to ensure the thread safety of my C# .NET POCO?

I am currently learning C# .net and working on a Selenium C# Project that involves the BrowserFactory class and a POCO named Driver. The InitBrowser method is used to set the value. However, when attempting to run tests using Parallel Test in NUnitFramew ...

How can I transfer data to a different component in Angular 11 that is not directly related?

Within the home component, there is a line that reads ...<app-root [message]="hii"> which opens the app-root component. The app-root component has an @input and {{message}} in the HTML is functioning properly. However, instead of opening t ...

Implement the anti-flickering script for Google Optimize in a NextJS/ReactJS environment

While working on a NextJS/ReactJS project, I am experimenting with setting up Google Optimize for some tests. One issue I have encountered is the flickering effect that occurs when Optimize changes visual elements during experiments. To address this probl ...

Conceal an element along with its space, then signal the application to show alternative content using React

Greetings everyone! I seek assistance with a React Application that I am currently developing. As a newcomer to the Javascript world, I apologize if my inquiry seems trivial. The application comprises of two main elements: a loader, implemented as a React ...

"Discover the process of transforming HTML, CSS, and script files into a cohesive JavaScript format

I have developed a unique web chat widget from scratch using HTML, CSS, JavaScript, and AJAX calls. Now, my goal is to convert it into a script that can be easily embedded in any other websites or webpages. Similar to how third-party widgets work, users sh ...

Sending information back to the server without causing a postback, all while remaining unseen

I have a straightforward JavaScript function on my ASP.NET page that writes data to a hidden field. However, in order to retrieve this data the form needs to be submitted back to the server. The issue is that submitting the form causes the page to reload ...

Creating a pattern for values ranging from 18 to 99, including leading zeros, using regular expressions

Looking for a Regular Expression that matches numbers from 018 to 099, inclusive. The numbers must range between 18 and 99, and include a leading zero for values less than 100. ...

Utilizing only select functions from lodash can be more beneficial than installing the entire library, as it reduces the amount of unnecessary dependencies

While working on my project, I incorporated underscore.js for its functionality. However, I recently discovered the need for Lodash's fill function. The idea of adding Lodash to my project seems excessive due to overlapping features with underscore.js ...

Can you explain the correct folder configuration for a SpringBoot project that incorporates static files?

I'm having trouble figuring out why my SpringBoot web application isn't displaying in the browser. Here's what my debug log is showing: 2017-08-04 14:54:24.760 DEBUG 23863 --- [tp1027569178-15] o.s.w.servlet.view.InternalResourceView : For ...

Extracting values from URL query parameters in Vue.js

When dealing with Vue.js callback URLs, I encounter situations where I need to extract a parameter value from the URL. For instance, consider this return URL: http://localhost:8080/#/sucesspage?encryteddata=abdeshfkkilkalidfel&9a I attempted to retrie ...

Designing an Angular ui-router feature: Implementing a dynamic navbar that seamlessly integrates into all primary states as well as their sub-states

I am facing a challenge with my website's navbar. I want it to be displayed on most pages, but not all of them. I tried using ui-view to load it only for specific pages, but I have hit a roadblock. My goal is to connect the navbar to main states such ...

Dynamically changing the borders of WebGL canvases: a guide to adding and

Here is my code snippet for creating an HTML canvas: <canvas id="glCanvas" class="canvases" width="20" height="20></canvas> To set the color, I am using the following: gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); I am w ...

What would be a colloquial method to retrieve the ultimate result from the iterator function?

I've got a rather complex function that describes an iterative process. It goes something like this (I have lots of code not relevant to the question): function* functionName( config: Config, poolSize: number ): Generator<[State, Step], boo ...

Click on each link in a table using Python Selenium webdriver

Has anyone been able to solve this issue? I've tried multiple times but still can't get it to work. The website consists of a table with links, and my goal is to loop through each link and click on them. https://i.stack.imgur.com/Ckwpu.png Thi ...