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

Is it possible that adding html tables together could result in the numbers being concatenated instead of summed?

When attempting to calculate the total sum of values in an html table column, my variable seems to be returning concatenated strings instead of the actual sum. For example, instead of 1 + 2 + 3 = 6, I am getting 1 + 2 + 3 = 123. The values in the "votes" ...

The issue with ng-change is causing the previously selected drop down option to be cleared

After successfully implementing live searching with ng-change, I encountered an issue with a pre-selected drop-down box. Despite setting the selected="selected" attribute to make option three the default selection, the drop-down box jumps to the top optio ...

Step-by-step guide on how to include the "content-based-recommender" package in a Reactjs project

I recently started learning React.js and I'm interested in using the "Content-Based Recommender" package from npm. However, after downloading it, I encountered an issue with importing it. The documentation suggests the following: const ContentBasedRec ...

Recover Checkmark Status from Data

Currently, I am storing form data in json format and encountering an issue when trying to load values from a previously created json file. The plain old JavaScript function provided below successfully loads values into a form from a file, effectively resto ...

What options do I have for personalizing this Google Bar Graph?

I am currently working on creating a Google bar chart. You can view my progress here: http://jsfiddle.net/nGvdB/ Although I have carefully reviewed the Google Bar Chart documentation available here, I am struggling to customize the chart to my needs. Spec ...

Creating a customized post method in Angular's resource API

I am looking to streamline my code for posting data to a SharePoint list by utilizing a resource factory. Currently, I have been posting data using the following method: this.save = function(data) { data["__metadata"] = { "type": getItemTypeForListNam ...

Leverage Vue's ability to assign data from a parent component to

I am struggling to bind the data (inputData) from the parent component to my child component. I have checked my code multiple times but cannot find where the mistake is. MainApp.js let vm = new Vue({ el: "#app", components: { &ap ...

What are the steps to troubleshooting using VS Code and Chrome browser with Webpack?

Can anyone provide guidance on how to debug webpack in VS Code with the Chrome browser? As a beginner in programming, I'm struggling because webpack minifies everything into one line which makes traditional debugging methods ineffective. I haven' ...

Nuxt Page Featuring One Exclusive Product

I am just getting started with Nuxt and I'm in the process of creating a Single Product. I have some questions: How can I generate multiple pages using SSR and create a unique HTML for each page? Should CSR be developed first before implementing SSR, ...

Handling Circular Dependency: Injecting a Service into an HTTP Interceptor in AngularJS

I'm currently working on developing an HTTP interceptor for my AngularJS application to manage authentication. Although the code I have is functional, I am wary of manually injecting a service as I believed Angular is designed to handle this process ...

Is it possible for Jaydata relationships to function seamlessly without the need to be

I am attempting to set up a basic model with Parent -> Child relationships (correctly declared and functioning, I believe). This is my approach: var parent = new $data.Types.Parent(); $data.context.Parents.add(parent); parent.Code = 123; var child = ...

Using Node.js and Express for redirecting to a custom URL

Having an issue with redirection on my small nodejs/express app. The goal is to redirect to an external URL with input values from a form after submitting. index.html <form method="POST" action="https://192.0.2.1/abc.html"> <input name="name ...

AngularJS closes when clicked outside

I've been attempting to add a close on outside click functionality to my code, similar to this example: http://plnkr.co/edit/ybYmHtFavHnN1oD8vsuw?p=preview However, I seem to be overlooking something as it's not working in my implementation. HT ...

What are some techniques for breaking down or streamlining typescript code structures?

Within my TypeScript class, I have a skip function. In the interface, I've specified that the data is coming from the backend. Now, on the frontend, I want to be able to rename the backend variables as demonstrated below. There are multiple variables ...

What could be causing the "10 $digest error" to appear in my code?

My goal was to create a basic app that could detect the size of the browser and display it. But, I encountered an error message saying "Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!" app.controller('AppCtrl',function($win ...

Retrieving text from an XPath element with Selenium WebDriver in JavaScript

I've been experimenting with Selenium and Node to extract text content from websites. For instance, I'm currently working with the URL: and using this XPath /html/body/section/div[1]/div[3]/div[1]/div[3]/form/button When I check in an Xpath se ...

The nested arrays in JavaScript produce different results when viewed in developer tools compared to when they are printed using toString()

There is a strange issue with the nested array in my current project that I need help with. When I log the nested array in console.log as individual values, it displays correctly. For example: console.log("Array: " + arr) //Array: 0,0,0,0,0,0,0,1 Howev ...

Navigate back to the previous route within the Vue router hierarchy

In my Vue application, I have a Settings page with child routes such as settings/user, settings/addUser, etc. I am looking to implement a back button that when pressed, takes the user back to the specific page they visited within the Settings section. Usin ...

What is the best way to display the remaining items in an array when a user clicks the next button?

Having a total of 12 cameras, I want to be able to select 4 cameras from a drop-down menu option and have only those 4 cameras displayed. Then, when I click on the next button, I need to show the remaining cameras in the selected layout format. How can thi ...

The div element disappears upon calling the load() function

Currently, I am working to implement sorting criteria (such as by price or author) on my jsp page. I am utilizing ajax to refresh the content of a div when a new sorting option is selected. However, upon calling the reload function, the div just disappears ...