What is the best way to verify if the text values of a collection of DOM-nodes in an array match a specific string

My objective is to validate if all names in an array are equal to e2e. I have a set of objects generated using protractor's element.all(by.css(...). The next step involves confirming if any element within the array has a text value that differs from my requirement.

To achieve this, I attempted creating an array from the existing collection and utilized the Array.some method, as depicted below:

const accountNames = element.all(by.css('td > p:nth-child(3)'))
const notMatch = Array.from(accountNames).some(element => 
element.getText() !== 'e2e')

expect(notMatch).toBeFalsy()

I anticipate obtaining both an array of strings and a boolean outcome with regards to comparing elements against a specific text.

However, I am currently receiving an empty array. What could potentially be causing this issue?

Answer №1

This code is functioning correctly, but I am considering if there is a more visually appealing solution:

it('filters kits by account hierarchy', () => {
mapPage.filter('e2e')
const accountNames = element.all(by.css('td > p:nth-child(3)')).getText()
.then(text => Array.from(text).some(item => item !== 'e2e'))

expect(accountNames).toBeFalsy()
})

Answer №2

There are several techniques available for manipulating arrays of elements which can be found here. I prefer using either the filter or reduce method to achieve this.

Additionally, there are two other methods you could experiment with, although they may not necessarily be simpler than your current solution.

Using Reduce

//By using reduce, we iterate through each element in the array and 
// consolidate them into a single value (true or false in this case)
let allValuesAreE2E = $$('td > p:nth-child(3)').reduce(function (acc, elem) {
    if (!acc) return false;

    return elem.getText().then(function (text) {
        return text === 'e2e';
    });
}, 'initVal');

expect(allValuesAreE2E).toBeFalsy();

Alternatively, Filter will iterate over each element in an array and discard those that do not meet specific criteria (such as having text equal to 'e2e').

const allContainE2E = $$('td > p:nth-child(3)')).count().then(numElements => 
  return numElements === $$('td > p:nth-child(3)')).filter(element => {
        return element.getText().then(text => text === 'e2e')
      })
  })
{

expect(allContainE2E.count()).toBeGreaterThan(0) // using Jasmine

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 highest and lowest points of the visible image within a transparent PNG file

As a newcomer to Three.js and graphics in general, I've been on the lookout for a method to identify the highest and lowest visible points in a PNG image with transparency. I understand that graphics manipulation involves manipulating an array of pixe ...

Unlock AngularJS expression through ng-click functionality

I've encountered an issue with accessing AngularJS expressions after a click event takes place. index.html <div class="row"> <a href="#/company"><div class="tile col col-comp cat-a-g grow-appear" ng-click="onSelect(item)" ng-repea ...

Exploring the Efficiency of jQuery AJAX and JSON Data Handling

I have implemented a method to store JSON data in a text file for querying using jQuery Ajax on my webpage. Currently, the text file contains approximately 10 facets of data with the potential for an additional 30 facets. This JSON data consists of questio ...

Guide on centering an element on a screen using Selenium and Python

I am currently working on automating a webpage with Robot Framework, Selenium, and Python. As part of the automation process, I need to scroll an element to the middle of the screen before clicking on it. Although I have tried using the following code: se ...

Exploring the differences between leveraging RxJS fromEvent and @ViewChild versus using the keyup event and RxJS Subject to detect changes in input

I have a simple component that allows users to search using the values typed into a search box. I've come across two different ways to implement this functionality and both seem to work well. Method 1 utilizes fromEvent from RxJS to create an observa ...

Implementing dynamic classes for each level of ul li using JavaScript

Can anyone help me achieve the goal of assigning different classes to each ul li element in vanilla Javascript? I have attempted a solution using jQuery, but I need it done without using any libraries. Any assistance would be greatly appreciated! con ...

JNI C++ with a 3D array of floating-point numbers

I am facing difficulties sending a 3D array from C++ to Java using JNI. The array structure should be: float data[6][26][5] Here is what I have so far, but it's not working yet. JNIEXPORT jobjectArray JNICALL Java_JNITest_getArray (JNIEnv * env, jo ...

Modifications to contenteditable elements result in a change to their IDs

Having some trouble with the behavior of a contenteditable div. The code structure is like this: <div contenteditable="true"> <p id="element-id-1">element-id-1</p> <p id="element-id-2">element-id-2</p> </div> E ...

Transforming Form Input into Request Payload for an AJAX Call

I'm facing a challenge in submitting my form data through a POST AJAX request and haven't been able to come across any solutions so far. Due to the dynamic creation of the form based on database content, I can't simply fetch the values usin ...

No Ajax request is being made for the WebMethod

I am attempting to utilize a WebMethod by using a function that includes an Ajax Call request. However, I keep receiving an Internal Server error 500. Below is the function with the Ajax call: function ajaxCall(URL) { var serverData = ""; $.ajax({ u ...

One common issue is being unable to target input[type=file] when multiple forms are present on a page using JavaScript

Question: I have multiple dynamic forms on a web page, each with a file input field. How can I specifically target the correct file input using $(this) in JavaScript? Here is an example of one of my forms: <form enctype="multipart/form-data" action="c ...

Is there a way to retrieve values from TextFields and Select elements by simply clicking on a button?

I am currently working on a project using react, redux, and material ui. I need to retrieve data from a TextField in order to make an order. The code snippet below showcases my current implementation: <Select value={product.color_set[0].title}> { ...

Generate a selection of complementary, triadic, and monochromatic color palettes using just one hex color code

My goal is to generate three unique color palettes based on a single hex/rgb value given by the user. The first palette will feature the complementary color of the initial input, followed by a full range of colors in subsequent palettes. I aim to expand be ...

Python numpy array not checking for value in tuple

My challenge lies in a function that returns a tuple, and my goal is to determine if a specific element exists within the tuple. Despite not knowing the types of elements present in the tuple, I am certain that I require an exact match. To illustrate: 1 in ...

Vue Array Proxy Class Fails to Trigger Reactivity

My custom Array extension has a feature where it intercepts changes made to its properties using Proxy, which is returned from the constructor. However, when used in a Vue component, it encounters issues. For example, when a filter is added, the display do ...

Difficulty encountered in storing information in the database

I have been developing a survey generator but I am facing an issue where everything is empty. The client enters one or many questions, along with one or many answers for each question. These inputs are sent to my database. I have successfully coded the d ...

PHP class variable not updating properly when using XMLHttpRequest

I'm currently working on a project for my university assignment. As I am still in the learning phase, I find myself getting a bit frustrated trying to figure out why a certain class variable is not functioning as expected. This is the PHP class struc ...

Incorporate the AngularJS controller into your JavaScript code

I am facing an issue with my jQuery UI dialog that contains a dynamic <select> populated with Angular and AJAX. The problem is that the AngularJS script still runs even when the dialog is not visible. To solve this, I added a condition to stop the s ...

How to Handle the Absence of HTML5 Spellcheck in Specific Web Browsers

While HTML5 spellcheck functionality may vary across different browsers, there are instances where it might not be supported in certain corporate environments. In the event that HTML5 is not supported in a particular browser, it's essential to first c ...

Why isn't nesting directives working in AngularJS?

I am facing an issue with one parent directive and multiple child directives. My requirement is to nest one directive inside another, resulting in the use of multiple directives. Could someone assist me in identifying any errors in this code or provide e ...