Protractor map function fails to provide a defined output

When working with an application containing multiple widgets, each with its own title and content, I want to effectively map the elements of each widget for easier testing purposes.

For instance, let's consider a page:

this.widgets = element.all(by.css('ul.widget-grid')).map(function(widget, index) {
    return {
        index: index,
        title: widget.element(by.css('div.title')).getText()
    };
});

Then, in my specification file:

expect(page.widgets[0].index).toBe(0);
expect(page.widgets[0].title).toBe('The Title');

However, I am encountering issues where my expectations are returning undefined.

What could be causing this issue? My development environment uses Protractor 2.0.

Answer №1

I was perplexed by this situation, so I decided to provide an answer to assist others...

Even though I had a grasp on the fact that map() yields a promise, my expectation was that when used in an expect, it would be resolved and then behave like an array. However, that assumption turned out to be incorrect. Instead of an array, it actually returns an object that mimics an array but is not truly an array.

Answer №2

When faced with a similar issue, I found that none of the suggested solutions worked for me. After some research, I discovered the actual solution to be as follows:

element.all(by.css('ul.widget-grid')).map(function(widget, index) {
    return {
        index: index,
        title: widget.element(by.css('div.title')).getText()
    }
}).then(function(map){
      this.widgets = map;
   });

Answer №3

map() will provide a promise that resolves into an array of objects.

To properly check the index and title of the first widget, you can use the following code snippet:

var firstWidget = page.widgets[0];

expect(firstWidget.index).toEqual(0);
expect(firstWidget.title).toEqual('The Title');

Answer №4

I encountered some challenges with Protractor's ElementArrayFinder.prototype.map function.
To resolve this issue, I opted for a traditional approach using a for loop:

 var myElements = element.all(by.css('ul li')) - 1;
 var n = myElements.length - 1;

 for (var i = 0; i < n; i++) {
     var elem = myElements.get(i);
     var open = elem.element(by.css('.some-class'));
     open.click();
     var childElem = filter.element(by.css('[some-attribute]'));
     expect(childElem.isPresent()).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

What is the method for performing a spelling check on every property within an array of Objects?

I'm working on a program that takes a user input as an argument and then searches for a similar match in an array of objects. The array of objects is retrieved from a database. When the user inputs a name, the search criteria should find objects with ...

Learn how to efficiently pre-load data using the prefetchQuery method in React-Query

Attempting to pre-fetch data using react-query with prefetchQuery. The network tab in browser DevTools shows the requested data coming from the back-end, but strangely, the data is not present in the react-query DevTools cache. Any ideas on what might be c ...

VueJs - Dynamic select menu

Hello, I am currently working on implementing a dropdown feature on my website using Vue. As a newcomer to Vue, I am using conditional rendering to display different sets of cards based on user selection. For example, the dropdown options include Cars, Mot ...

Determine the presence of a Facebook user by using JavaScript

I am trying to find a method to verify the existence of a Facebook user based on their ID. As far as I understand, there is a Graph API that can provide a Facebook user's profile picture using their ID in this format: I have noticed that if an inval ...

I am looking to transfer an image stored in a database onto a card

One issue I am facing is that when trying to display an image from a database, uploaded by a user and showing on a card with additional information like name and price, an icon resembling a paper with green appears in the output. Here's my code snippe ...

Is it possible to generate an ArrayBuffer or Float32Array by combining two separate underlying buffers?

Is there a way to create an ArrayBuffer with two underlying buffers and smartly iterate over them, rather than allocating a new JavaScript ArrayBuffer of size data1size + data2size? ...

"Implementing Nested Actions in Vuex: A Guide on Calling Actions within Actions

I am currently working on a small Vue App with a Rails API where I am using Vue, Vue-Resource, and Vuex. I have successfully fetched all users from the database and now I am trying to update one of them. The patch operation for updating the user is working ...

Build a JavaScript image carousel featuring custom text for each image

I recently completed a tutorial on creating JavaScript image sliders. I have successfully added a text box to display on each image, but now I need the text to be specific for each individual image. The images are sourced from an img folder and are labeled ...

What is the process for generating a collection of web elements?

Despite efforts, I continue to encounter the 'selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document' exception when trying to create a list from the <div class ...

Prevent tooltips from disappearing when hovering over them

I need assistance in keeping my tooltip visible when the mouse is on it, but hiding it when the mouse moves out. Can you please help me achieve this? $(document).ready(function(){ $('[rel=tooltip]').bind('mouseover', function() { ...

Setting a CSS Variable with the Help of jQuery

I need help with changing the background color of a specific .div element when it is clicked on. I want the color to be a variable that can be changed by the user, and this change should occur when the document is ready. Currently, I am using a CSS variab ...

Tips on how to highlight a clicked list item:

I'm currently attempting to access the key={1} Element within my li. My goal is to set the activeItem in State in order to compare it with the clicked item later on. If they are equivalent, I want to apply an active class to that specific element. How ...

The console is displaying an undefined error for _co.photo, but the code is functioning properly without any issues

I am facing an issue with an Angular component. When I create my component with a selector, it functions as expected: it executes the httpget and renders a photo with a title. However, I am receiving two errors in the console: ERROR TypeError: "_co.photo ...

Loading a gallery dynamically using AJAX in a CakePHP application

I am currently working with Cakephp 2.8.0 and I am facing an issue with implementing ajax in my application. I have a list of categories displayed as li links, and upon clicking on a category, I need to remove certain html code, locate the necessary catego ...

Using React to retrieve data from Firebase through axios

I recently uploaded some mp3 files to my Cloud Storage on Firebase. To retrieve these files, I am utilizing axios for the necessary operations. After modifying the permissions under the "rules" tab to: allow read, write, request; I am still encou ...

Encountering an issue with React JS Array Filtering: running into the error message "

I am encountering an error stating that includes is not a function, and the record.id is not being recognized in VS Code. I'm not sure where the problem lies? import React, { Component } from 'react' import axios from "axios" export de ...

Ensure to verify within the ngOnInit function whether the checkbox has been selected

Hi everyone, I'm currently facing a situation where I have a list of checkboxes on one screen. When I select some checkboxes, they move to another screen and remain selected there. However, the issue is that when I try to use a button on the second sc ...

Unable to produce scrolling animation using JavaScript

I'm trying to implement a feature on my website where the page scrolls with a sliding effect when the user presses the "Scroll" button. However, I've encountered issues as it doesn't seem to work despite adding the necessary tags to my HTML ...

Angular: Cleaning an image tag in sanitized HTML text

I have a scenario where I am integrating an HTML snippet from a trusted external source into my Angular component. To ensure security, I am utilizing Angular's DomSanitizer and specifically the bypassSecurityTrustHtml method to process the snippet bef ...

AngularJS directive $destroy is a useful tool for managing the

My angular application has been configured with the use of ng-view. Within one specific view, there is a dynamically loaded component alongside the view itself. This component acts as a directive that compiles its contents to allow for further interaction ...