Check to see if each individual item includes an element that utilizes protractor

I am attempting to verify whether each of the items with the class name .elem1 contains another element with the class name .elem2. I have tried the following code, but it does not produce the desired outcome:

expect(element.all(by.css('.elem1')).all(by.css('.elem2')).isDisplayed()).toBeTruthy();

Answer №1

Consider comparing the quantity of elem1 elements to those that contain elem2 elements:

var elem1count = element.all(by.xpath("//*[contains(@class, 'elem1')]")).count();
var elem1WithElem2count = element.all(by.xpath("//*[contains(@class, 'elem1') and .//*[contains(@class, 'elem2')]]")).count();
elem1WithElem2count.then(function (elem1WithElem2count) {
    expect(elem1count).toEqual(elem1WithElem2count);
});

This approach may not be the most elegant solution.

A similar concept can be achieved using filter():

var elem1count = element.all(by.css(".elem1")).count();
var elem1WithElem2count = element.all(by.css(".elem1")).filter(function (elem1) {
    return elem1.element(by.css('.elem2')).isPresent();
}).count();

elem1WithElem2count.then(function (elem1WithElem2count) {
    expect(elem1count).toEqual(elem1WithElem2count);
});

Alternatively, you can utilize reduce() to solve this problem:

var result = element.all(by.css('.elem1')).reduce(function(acc, elem1) {
    return elem1.element(by.css('.elem2')).isPresent().then(function (isPresent) {
        return acc && isPresent;
    });
}, false);
expect(result).toBe(true);

In this scenario, each elem1 element is assessed for the presence of an elem2 element, with the results being consolidated into a single boolean value.

You could also employ each() alongside expect statements to validate every elem1:

element.all(by.css('.elem1')).each(function(elem1) {
    expect(elem1.element(by.css('.elem2')).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 exporting a default element from within the script setup block in Vue 3?

It appears that the export default statement is not functioning properly within <script setup>. For example, exporting it in a file called test.vue: <template> <div id="test" class="test"> </div> </templ ...

Showing the loading screen while waiting for the static Next.js app to load

Looking for a way to implement a loading screen right before the entire static page finishes loading? I'm currently utilizing modules:export to create my static page, but struggling to listen to the window load event since NextJs has already loaded th ...

Unable to retrieve data from file input using jQuery due to undefined property "get(0).files"

I am facing an issue with retrieving file data in jQuery AJAX call from a file input on an ASP.NET view page when clicking a button. HTML <table> <td style="text-align:left"> <input type="file" id="AttachmenteUploadFile" name="Attachme ...

Angular: Truncating text based on element width - A guide

I am working on displaying a String text and determining how many words to hide based on the screen width. Here is my current approach: app.filter('words', function () { return function (input, words) { if (isNaN(words)) re ...

Creating a prompt within a while loop

Issue with the code is that it should only progress if the user inputs "rock", "paper" or "scissors". However, after re-entering any input the second time, it still moves on despite passing the condition in the while loop. For instance, entering "asdf" p ...

Tips for establishing a connection with an MQTT mosquito broker in a React application

I am currently working on establishing a connection between my React web application and a mosquito broker that is hosted on Docker. I have decided to utilize the MQTT.js library for this purpose, which you can find here. Below is the code snippet that I ...

Simple method for grouping and tallying elements within an array

Consider this array: const arr = [ {name: 'Server 1', country: 'DE'}, {name: 'Server 2', country: 'PL'}, {name: 'Server 3', country: 'US'}, {name: 'Server 4', country: &ap ...

Angular keeps throwing an error saying "Provider not found" when trying to inject a factory

Issue: Encountering an Unknown Provider Error in Angular App for: UnitProvider <- Unit Error Details: Error: [$injector:unpr] Unknown provider: UnitProvider <- Unit Codepen Link: View LIVE CODE Example I recently came across a fascinating vide ...

Guide to releasing an independent Angular component (njsproj) alongside a web application using Visual Studio 2017

We are in need of publishing our website locally to test it on Sitecore. Our special component for this purpose is called webapp. Recently, we introduced a new component (njsproj file) that contains our Angular application. However, when I publish webapp, ...

Switch between multiple unordered lists (ul) so that when one list is clicked, the other lists reset to their initial state as though they were never

When I click on the first item in the ul list, it should slideToggle() to show its corresponding items. Similarly, when I click on the second item in the ul list, its items should slideToggle(), but the first ul list remains visible as well. What I am tryi ...

What benefits does React offer that jQuery does not already provide?

What sets ReactJS apart from jQuery? If jQuery can already handle everything, why should we use React? I've tried to research on Google but still don't have a clear answer. Most explanations focus on terms like "views," "components," and "state" ...

VueX threw an error stating that it cannot read property 'getters' because it is undefined in Nuxt.js

Just starting out with Vuejs and Nuxt.js. I recently set up a Nuxt project but encountered an error when trying to run it: https://i.sstatic.net/ROtOs.png Any assistance would be greatly appreciated. Thank you. ...

What is the correct way to use Deviceready in an Ionic application?

I am currently working on a mobile application built with Cordova and Ionic. The default page of the application requires the use of the SQLLite plugin. https://github.com/brodysoft/Cordova-SQLitePlugin The issue I am facing is that the view contains n ...

Learn how to transform a date into a dropdown menu with separate options for day, month, and year selection

https://i.sstatic.net/8IUId.png]2 <p> <label for="id_date_of_birth">Date of birth:</label> <input type="date" name="date_of_birth" value="2019-06-14" id="id_date_of_birth"> </p> I'm retrieving informat ...

The ng-model does not bind properly when the input value is set through JavaScript instead of directly from

I'm encountering an issue with a text box in my project. The data is being populated using JavaScript, which reads a QR code and sets the value accordingly. I am using ng-model to bind to a variable in my controller, and while it works perfectly when ...

The fixed left div and scrollable right div are experiencing issues with their alignment

I am having an issue with a section where the left div is fixed and the right div is scrollable. However, the content of the right div scrolls even before the scroll top reaches the section. My goal is for the right div to scroll only when it reaches the t ...

Exploring Cypress: Leveraging the Power of Variable Assignment

Recently, I encountered an issue while working with a variable in a Cypress each loop. Despite incrementing the variable within the loop, it resets to zero once outside of it. Can someone shed light on why this happens and suggest a solution? Thank you. p ...

Javascript: Issue encountered while the page was loading

Whenever I make an API call, an error keeps popping up every time the page loads. I can't seem to figure out why it's happening. Can anyone shed some light on this? I've tried to include the link: https://i.stack.imgur.com/3Ul0S.png This ...

Gradually eliminate the background color in one smooth motion

When new messages appear, I want them to be subtly highlighted as users hover over them. The background color will slowly disappear on hover thanks to the CSS code provided below. However, this effect should only happen once, which is why I need to remov ...

Separate the sender and receiver using Socket.io

My latest web application is built using the JavaScript stack (MongoDB, ExpressJS, AngularJS, NodeJS). I have successfully implemented registration, authentication, and a chat feature using Socket.io. However, I now need a method to distinguish between cli ...