Exploring the potential of for loops and promises in verifying text across multiple elements

Currently, I am engaged in developing a function that is responsible for extracting the text of elements post utilizing a filter feature. Upon inspecting the displayed text output, it appears that the elements are being retrieved successfully; nonetheless, I find myself grappling with comprehending JavaScript promises. Notably, 'activeFilters' has been designated as a variable.

this.verifyColorFilterFunctional = function(color) {
    var bool = true;
    activeFilters.count().then(function (count) {
        var amt = count - 1;
        for (var i = 0; i < amt; i++){
            activeFilters.get(i).getText().then(function(text) {
                bool = (color === text);
                console.log(bool);
            });
            if (!bool) {
                break;
            }
        }
    });
    return expect(bool).to.become(true);
};

Upon scrutiny, I have observed that while the console.log accurately prints out 'true' and 'false', there exist two discrepancies. Firstly, when 'false' is determined, the execution fails to halt as intended within the specified if statement. Additionally, an error 'typeError: true is not a thenable' surfaces, leading me to believe that although the logic seems sound in my rationale, it does not align with JavaScript's interpretation. Any assistance or guidance on this matter would be immensely appreciated.

Answer №1

When using Protractor's element.all(), you can utilize the getText() method to retrieve the text displayed in the elements as an array. This allows you to easily compare the resulting array by using the expect method.

this.verifyColorFilterFunctional = function(color) {
    activeFilters.getText().then(function (textArray) {
     expect(textArray).to.equal(Array(textArray.length-1).fill(color));
   });
}

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

Issue with automatic completion for classes and IDs in Visual Studio Code

Auto completion does not seem to work when I attempt to use a previously written class or ID in my HTML file. It doesn't function in the same HTML file, CSS file, or JS file. While ctrl + space works for some classes within the same HTML file, it is n ...

The three.js raycaster is able to detect objects both in front of and behind the specific object I am trying to select

I am currently working on rendering a 3D model of a house using Three.js and encountering an issue with the raycaster in my code. The problem I'm facing is that it's selecting every object both behind and in front of the specific object I want to ...

PHP: Avoiding duplicate JavaScript imports

Hey there, I'm running into an issue with incorporating JavaScript, specifically the Google Maps API. Let's say I have a page that includes the library like this: <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=pla ...

Prevent Vertical Scrolling with Jquery on right-click functionality

When a user right-clicks in my application, I present them with some options. However, when the user clicks on an option such as "copy," the page always scrolls to the top. I am struggling to prevent this automatic scrolling. I attempted to use the anima ...

Storing files offline in Firefox 3.5 with file:// protocol

While experimenting with the code for offline storage in Firefox 3.5, I referred to a tutorial on . When the page loads, I am prompted with a dialog asking to store data, but after clicking Allow, the dialog does not disappear. The application functions co ...

Obtain the complete source code by using the responseText property of the XMLHttpRequest object

I am looking to retrieve the entire source code of a webpage, such as www.google.com. Currently, I have a PHP file that can copy the source code of a page: <?php echo file_get_contents('http://www.google.com'); ?> Here is my function ...

AngularJS and Bootstrap tabs clashing with each other

I have implemented the JavaScript Bootstrap 3 tabs according to the documentation, as shown below: <!-- Nav tabs --> <ul class="nav nav-tabs" role="tablist"> <li class="active"><a href="#home" role="tab" data-toggle="tab">Home< ...

Navigating between different windows in Selenium 2 using Java

Hey there! I'm currently trying to switch to a popup window and click a button on that popup, but for some reason I keep encountering errors. Check out this sample webpage: Here's the code snippet I'm using: for (String handle : driver.g ...

Combining Jenkins with xvfb and selenium for seamless automated testing

I've been following the guidelines provided in order to set up the Xvfb plugin successfully. Currently, the xvfb executable can be found at /usr/bin/Xvfb. Despite ensuring that my project configuration and Jenkins settings match the instructions prec ...

Retrieve only the initial tag content using jquery

My goal is to extract the "22" from the following code... <div class="left"> <a class="count-link" href="http://url1.com"> <span>22</span> users </a> <a class="count-link" href="http://url2.com"> <span>10</span ...

Is the jquery autocomeplete plugin malfunctioning when using numbers in the input?

I encountered a requirement to display stock number suggestions within a search box. To achieve this, I decided to implement the Jquery autocomplete plugin. Through an ajax call to a function in my cfc, I was able to retrieve all the stock numbers and stor ...

Ways to minimize an array using group by

I have a large dataset that needs to be grouped by COAGrpCode and ldgrGrp. Specifically, I need to sum the values of Opening, PrdDr, PrdCr, and Closing for each unique combination of COAGrpCode and ldgrGrp. Below is a sample of the data, which consists of ...

"Exploring the Depths: How Node.js Utilizes Recursive Calls

I need assistance generating a comprehensive list of all users' flairs on a specific subreddit. To achieve this, Reddit breaks down the requests into chunks of 1,000 and offers both "before" and "after" parameters for fetching purposes. However, I am ...

Problem encountered when attempting to use 'delete' as a property name

I am currently encountering an issue with a form that deletes a gallery from a database. Previously, the code was functioning properly but after some recent updates such as switching from jquery.interface to jquery-ui, I am now facing difficulties. Wheneve ...

Exploring AngularJS routing integration with Rails

I have recently started incorporating AngularJS into my Rails application. However, I am facing an issue where my route providers with Angular are not functioning as expected. Instead of displaying my template, the Rails view is being displayed. routes.rb ...

Optimizing data loading with Redux Sagas: A guide on loading individual objects

Recently, I've been immersed in a React project that utilizes Redux and Sagas. The structure I have set up allows my component to dispatch an action requesting a list, which is then handled by Sagas calling a function to fetch the data from the backen ...

Laravel triggers a 'required' error message when all fields have been filled

As I attempt to submit a form using an axios post request in laravel, I encounter an issue with the validation of the name and age fields, along with an image file upload. Here is a breakdown of the form structure: Below is the form setup: <form actio ...

Creating a function generator using a string parameter in TypeScript

Is there a way to pass a string parameter to a function and retrieve an object with that key? function getFunction(name: string): { [name]: () => number } { return { [name]: () => { console.log(1); return 2; }, }; } const { m ...

The header on the express req object is not persisting when navigating between routes

I have been struggling to set the sessionId in my express req object so that it can be accessed across different routes. Interestingly, when I try to set it using middleware, it works perfectly fine. However, when attempting to do the same within a route, ...

Managing concurrent users updating the same form on a web application

Imagine a scenario where user A opens a form with pre-filled data. While user A makes changes to the form data, user B also opens the same form with the data intended for user A. Just as user B begins modifying the data, user A clicks on the submit butto ...