Guide on utilizing protractor to confirm equality of two spans in varying positions?

<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>

<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>

What method can I use in Protractor to verify that the values of these two spans are identical, even though one span is nested under an 'a' tag and the other is under a label tag?

Should I be using the 'equal' element for this task?

Answer №1

An easy solution would be to locate both spans and compare their texts:

var firstSpan = element(by.css("a.showld")).element(by.binding("locations.selectedCount")),
    secondSpan = element(by.css('label[key="search.selectedLocations"]')).element(by.binding("locations.selectedCount"));

firstSpan.getText().then(function (firstText) {
    var secondText = secondSpan.getText();
    expect(secondText).toEqual(firstText);
});

It's important to note that getText(), like many other methods in Protractor, returns a promise that must be resolved. In this case, we are resolving the first promise explicitly with then() and allowing expect() to implicitly resolve the second promise. For more information, visit: Comparing values of two promises.

Another method could be:

var spans = element.all(by.binding("locations.selectedCount"));
spans.first().getText().then(function (firstText) {
    var lastText = spans.last().getText();
    expect(lastText).toEqual(firstText);
});

However, this approach may not be as scalable and is best suited for only 2 elements.


A more scalable solution involves using map() and Array.reduce(). By gathering all span texts into an array and checking if all items in the array are equal:

var spans = element.all(by.binding("locations.selectedCount"));

spans.map(function (span) {
    return span.getText();
}).then(function (texts) {
    var allTextsAreTheSame = texts.reduce(function(a, b) {
        return (a === b) ? a : false;
    });
    expect(allTextsAreTheSame).toBe(true);
});

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

Prevent redundant Webpack chunk creation

I am currently working on integrating a new feature into my Webpack project, and I have encountered a specific issue. Within the project, there are two entry points identified as about and feedback. The about entry point imports feedback, causing both abo ...

"Encountering a mysterious internal server error 500 in Express JS without any apparent issues in

My express.js routes keep giving me an internal server error 500, and I have tried to console log the variables but nothing is showing up. Here are the express routes: submitStar() { this.app.post("/submitstar", async (req, res) => { ...

Selenium and JavaScript integration for opening new browser tabs

Hello there, I am looking to open new tests ('it' method) in new tabs and currently trying this approach: driver = new Builder().forBrowser('chrome').build(); beforeEach(() => { // driver.manage().window().open('url') ...

Using JQuery with the latest version of Google Sites is a game-ch

My coding knowledge is very limited, especially beyond VBA. I've hit a roadblock and it seems like I'm overlooking something obvious. What I'm attempting to accomplish: I need this code to be integrated into a New Google Sites page (which h ...

Tips for accessing information from an Angular Resource promise

I am currently facing an issue when trying to read data from an angular promise that was returned. Before, I had the following code: var data = category.get({id:userid}); Later, I realized that the data being returned was an unresolved promise. So, I ma ...

Exploring the method of retrieving nested JSON objects in Angular

When my API sends back a JSON response, my Angular application is able to capture it using an Interface. The structure of the JSON response appears as follows: { "release_date":"2012-03-14", "genre_relation": ...

Upon attempting to retrieve a package version, NPM responds with an error stating "bash command not found."

I recently set up my project with a package.json file that includes the nodemon package among others. When I run #npm list --depth 0 in the terminal, this is what I see: ├─┬ [email protected] However, when I try to check the version of nodemo ...

Swagger is unable to locate a user schema when utilizing Yaml files

Hello, I am a beginner using Swagger and trying to create simple endpoint documentation. However, I am facing an issue with my schema type and cannot figure out what's wrong. To start off, I organized my folder structure in the root src directory whe ...

Navigating through elements in an array in node.js

Within my array, I store the languages that users prefer. Here is an example: let language = [] var Lang = "it" language.push(Lang) This process is repeated with various languages. The array would eventually contain these values: language ...

Setting a specific time for a div element with opacity: A step-by-step guide

Is there a way to adjust the timing for the appearance of the add-to-cart when hovering over the product-list-item? Currently, it appears when I hover over the item and disappears when I move my mouse away. .product-list-item { position: relative; w ...

Getting the environment variable from Rails in your JavaScript: A guide

I am currently working on implementing a key system within my application. In the code I am using, there is logic that looks like this: $('.key-test-button').on('click', function(){ if ($('.key-test-input').val() === "MYK ...

What is the best way to retrieve all information from GitLab API using Axios?

I am looking to retrieve data from all the projects stored on my GitLab server. As I understand, GitLab usually displays a default of 20 projects per page, so I need to adjust the setting to show more projects at once: https://gitlab-repo.com/api/v4/proje ...

Can you please provide guidance on how to retrieve the class of an image that I had previously identified using its xpath

After locating the image using its xpath, now I need to verify if it contains the class "thatOneImage". <pre>image = browser.find_element_by_xpath('.//*[@id=\'image\']')</pre> Any suggestions on how to ac ...

Issue with adding a key:value pair to res.locals.object in Node/Express not functioning as expected

Currently, I am working on constructing an object within res.locals by executing multiple MongoDB operations and utilizing the next() middleware. Once I have added an object to res.locals, such as: res.locals.newObject, my goal is to subsequently add addi ...

Struggling to retrieve Json data through Ajax in Rails 5

Hey there! I'm currently exploring the world of Rails action controllers with Ajax, and I've run into a bit of a snag. I can't seem to retrieve Json data and display it in my console.log using my Ajax function. The GET method works perfectly ...

Recalling the use of jquery toggle throughout the website?

I have successfully implemented a button to hide and unhide a lengthy menu with a click, but I am struggling to make the menu state persistent across multiple pages. I would like the last toggle action by the visitor to be remembered. Any suggestions on ho ...

Manage image placement using CSS object-position

I have the following code snippet: img{ width: 100%; height: 1000px; object-fit: cover; object-position: left; } <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

The way an event can be outrun or vanish due to another event

let bar = new function(){ $scope.MessageSpan="Item added successfully";} When the Add button is clicked, the above function is invoked. However, if I want to hide this message when any other button is clicked, I would have to update the span text for all ...

When you neglect to include 'window' in your Angular expressions, you may encounter the error message "Window referencing is prohibited!"

Switching from AngularJS v1.2.17 to v1.2.28 brought about a surprise - one of my pages started throwing JavaScript errors, specifically: Error: [$parse:isecwindow] Referencing the Window in Angular expressions is disallowed! Expression: model.skAccoun ...

Ordering items in Angular Filter OrderBy is based on a specific string sequence, rather than following

As I iterate through a list of objects in an ng-repeat, I am facing the challenge of sorting them based on their 'approved' attribute. The possible values for 'approved' are 'APPROVED', 'DENIED', or 'PENDING&apo ...