Differences between the `getLocationAbsUrl` and `getCurrentUrl

When working with Protractor, there are two methods available in the globally accessible browser object:

This method returns the current absolute URL from AngularJS.

The getCurrentUrl() method schedules a command to retrieve the URL of the current page.

It can be confusing to understand the distinction between these two methods. Personally, I have only used getCurrentUrl() up until now.

What scenarios call for using getLocationAbsUrl()? In what situations would this method be useful?


Unlike other selenium language bindings, I haven't come across anything quite like getLocationAbsUrl(). It seems to be specific to Protractor.

Answer №1

GitHub link for getCurrentUrl function in WebDriver

webdriver.WebDriver.prototype.getCurrentUrl = function() {
  return this.schedule(
      new webdriver.Command(webdriver.CommandName.GET_CURRENT_URL),
      'WebDriver.getCurrentUrl()');
};

This code utilizes the schedule() -> command() method wrappers to handle promises from the WebDriver.getCurrentUrl() function.


GitHub link for Protractor's getLocationAbsUrl function

functions.getLocationAbsUrl = function(selector) {
  var el = document.querySelector(selector);
  if (angular.getTestability) {
    return angular.getTestability(el).
        getLocation();
  }
  return angular.element(el).injector().get('$location').absUrl();
};

This is simply a wrapper around the $location.absUrl() method with an added wait for the AngularJS library to finish loading.


Difference between Current URL and Absolute URL

Assuming the app URL is:

http://www.example.com/home/index.html#/Home

The Current URL refers to more of a URI:

/home/index.html#/Home

The Absolute URL resolves to the full address:

http://www.example.com/home/index.html#/Home

When to use absolute URLs: If you need the complete domain address instead of just the local navigation path, opt for the Absolute URL.

  • If your application fetches the Current URL, utilize the getCurrentUrl() function in tests.

  • If your code requests the Absolute URL, make use of the getLocationAbsUrl() function in tests.

Answer №2

Previously mentioned was the function getLocationAbsUrl, which provides the full path to the current location.


browser.get('<a href="http://angular.github.io/protractor/#/api" rel="nofollow">http://angular.github.io/protractor/#/api</a>');
expect(browser.getLocationAbsUrl())
    .toBe('<a href="http://angular.github.io/protractor/#/api" rel="nofollow">http://angular.github.io/protractor/#/api</a>');

The getCurrentUrl is specifically used in a promise (schedule) to fetch the URL of the current page. It is recommended to only use getCurrentUrl when scheduling actions.


webdriver.WebDriver.prototype.getCurrentUrl = function()
{
    return this.schedule(new webdriver.Command(webdriver.CommandName.GET_CURRENT_URL),'WebDriver.getCurrentUrl()');
}

Answer №3

Essentially, both should perform the same function by providing the URL of the page.

This issue was brought to light in a bug report:

https://github.com/angular/protractor/issues/132

The problem arose with IEDriver for Selenium, which consistently returned the initial page instead of the current one. To address this, the protractor team incorporated a JavaScript call to Angular's $location to ensure that the correct URL is always retrieved using JavaScript rather than the Webdriver protocol.

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

What could be causing the "Error - Only secure origins are permitted" message to appear for my service worker?

Whenever I attempt to implement a service worker on my progressive web application page, why does the browser console display this specific error message? ERROR "Uncaught (in promise) DOMException: Only secure origins are allowed JavaScript Code: ...

Is it possible to create tabs using Ajax without using jQuery?

Exploring the realm of tabs that dynamically load content into a div without redirecting to another page unveils numerous possibilities. Although the existing examples mostly rely on ajax with jQuery or similar libraries, I am inclined towards exploring a ...

Surprising Interactions Between Ajax and PHP

I am encountering an issue with my ajax request to a php page. The PHP script successfully sets a cookie, but the message sent back using the echo function is not appearing in the response. I have confirmed this by logging the XMLHTTPRequest Object in the ...

Trigger an AJAX request to execute a PHP script when a button is clicked

When I click a button, I want to run an ajax function that calls a PHP script and displays the resulting data under a specific div. However, it is not currently working as expected. Upon checking the console, I noticed that no value is being passed to the ...

Error message "env: '/bin/flask': No such file or directory" pops up while executing the command "npm run start-backend"

Currently on the hunt for a super basic website template that combines Flask and React. I've been using this repository and carefully following the installation steps laid out https://github.com/Faruqt/React-Flask Working on Windows 10 using git Bas ...

The website http://www.salesforce.com is experiencing a no such element exception in Selenium, despite the element actually being present

Seeking an element with the locator By.cssSelector("#e1") on the website: SALESFOCE.COM. Attempting to locate an element within this first button (image below), which happens to be a <div>. https://i.sstatic.net/qrDIT.png Despite trying m ...

Comparison of AngularJS module and $scope: exploring the differences

Seeking guidance on AngularJS as a beginner. I am curious about the distinction between the following two code examples: 1. Defining a controller using 'this': var app = angular.module('greeting', []); app.controller('HelloCtrl&a ...

Implementing modifications to an object and ensuring those adjustments are accurately displayed upon return within AngularJS services

I'm facing an issue where I am attempting to update my Object within my AngularJS service and then return it. However, the value remains unchanged, and the original Object is returned as it was initially defined. Even using a Promise Object didn' ...

How can we ensure the discord client object is easily accessible within event files?

I'm a beginner with Discord.js and I've been trying to figure out how to allow event and command files to access the main client instance. I want to be able to call client.database in an event file for CRUD operations, but I'm confused on ho ...

Prevent the execution of an event while another event is already running in jQuery

Two events are in play here: one with the onclick function which scrolls to a specific div upon menu item click, and the other utilizes the onscroll function to "highlight" the corresponding menu item when scrolling near the div. The burning question is: ...

present path in Angular.js

I've been working on integrating routing into my Angular 1 page. Here are the routes I added: .config(function ($routeProvider, $locationProvider) { $routeProvider .when('/mailbox/:id', { controller: 'BoxList&ap ...

I need some help with adjusting the number of rows shown per page in MaterialReactTable

I've been utilizing MaterialReactTable and my goal is to display only 5 items on each pagination page. Despite setting muiTablePaginationProps, I still see 10 items per page. How can I resolve this issue? <MaterialReactTable columns={columns} ...

The Postman application is unresponsive and the hashed password has not been created

I am currently attempting to create a hashed password using bcryptjs and then display that password using console.log for easy access and use. For testing my POST request, I am using Postman where my approach involves sending the request body with email a ...

The session in express4.2.0 is not defined

I'm having trouble setting a session with node.js using express4.2.0. Below is my code snippet along with some comments: APP.js var express = require('express'); var path = require('path'); var favicon ...

The tooltip in nvd3 is displaying the index instead of the label

I'm encountering an NVD3 tooltip issue with my multichart (multiline chart). The XAxis labels are set as JAN, FEB, MAR... DEC. However, when I hover over the graph, it displays 0, 1, 2, 3... 11 as the tooltip title instead of the month names. Here is ...

The ".select" function in Selenium/Watir does not work as expected in Safari browser, regardless of the version

I am encountering difficulty selecting a value in a select field while using Safari. The process works smoothly in Chrome, Firefox, and IE. The object is located successfully but no value is selected. Additionally, there are no error messages returned. I ...

The Kendo UI Combobox triggering the change event twice

After noticing some strange behavior in all the comboboxes within my application, I realized that the Kendo UI ComboBoxes were triggering the change event twice, resulting in two HTTP requests being made when the code inside only required one. Despite cond ...

Custom JSON filtering

I have a JSON object called Menu which contains various menu items for my system that I am developing using VueJS + Vuetify. I need to filter these menu items based on the "text" field, regardless of position in the text and without differentiating between ...

Why does AngularJS keep showing me an "Argument 'HomeController' is not a function" message?

I am currently in the process of developing a Cordova application and I plan to utilize Angular for creating pages that will be displayed based on user selections. At the moment, I am working on this in a web environment before transitioning to Cordova. & ...

What is the best way to categorize elements in an array of objects with varying sizes based on two distinct properties in JavaScript?

I am faced with a scenario where I have two distinct arrays of objects obtained from an aggregate function due to using two different collections. Although I attempted to utilize the map function as outlined here, it proved unsuccessful in resolving my is ...