Protractor test freezes after attempting to click on an element

I have encountered a challenge while attempting to write a protractor test that selects an item from a custom dropdown menu. The issue arises when trying to click on an element other than the last one in the list, as it hangs and times out. Interestingly, when I remove the click() method invocation, everything seems to function properly. Given that all these calls are made asynchronously, I am struggling to find a way to halt the loop once it locates the desired element. Here is a glimpse of my code:

    var it = null;
    for(var i = 1; i <= totalNumberOfAccounts; i++) {
        var listItemLocator = '//div[@id="payment-accounts"]/div/ul/li[' + i + ']/label/div/div[2]/div[2]/span[2]';
        var item = browser.driver.findElement(protractor.By.xpath(listItemLocator));
        item.getText().then(function(value) {
            if(value === accountNumber) {
                it = item;
            }
           console.log(value);
        })
        .then(function clickOption() {
            console.log('Clicking...');
            if (it) {
                console.log('Clicking desired item');
                it.click();
                console.log('Clicked..');
            }
        })
    }

Additionally, I experimented with another approach:

this.selectRichSelectOption = function (selector, item) {

    var selectList = browser.driver.findElement(selector);
    selectList.click();

    var desiredOption = '';
    var i = 1;
    selectList.findElements(protractor.By.tagName('li'))
        .then(function findMatchingOption(options) {

            console.log(options);

            options.some(function (option) {

                console.log('Option:');
                console.log(option);
                var listItemLocator = '//div[@id="payment-accounts"]/div/ul/li[' + i + ']/label/div/div[2]/div[2]/span[2]';
                console.log(listItemLocator);
                var element = option.findElement(protractor.By.xpath('//label/div/div[2]/div[2]/span[2]'));
                console.log('Element:');
                console.log(element);
                i++;

                element.getText().then(function (value) {
                    console.log('Value: ' + value);
                    console.log('Item:');
                    console.log(item);
                    if (item === value) {
                        console.log('Found option..');
                        desiredOption = option;
                        return true;
                    }
                    return false;
                });
            });
        })
        .then(function clickOption() {
            console.log('Click option');
            console.log(desiredOption);
            if (desiredOption) {
                console.log('About to click..');
                desiredOption.click();
            }
        });
};

However, this new approach yielded even stranger results. Suddenly, the getText() method call returns an empty string. Yet, fetching attributes such as class returns the correct value. Where did the text value disappear to?

If anyone has insight or solutions, please assist me.

Answer №1

It appears there is a problem with the loading of the page. Once you make a selection, the page fails to fully load.

You might want to consider implementing a browser.sleep(timeInMs); function.

Answer №2

For smoother coding experience, consider integrating the async functions of node 8 or higher like await. I personally encountered a similar issue and it was resolved by using await to wait for specific elements to show up or meet certain criteria.

await browser.wait(EC.presenceOf(element(by.xpath('path leading to element based on attribute'))))

Wishing you success in your endeavors!

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

Exploring the power of searching JSON data using ReactJS

After reading through the "Thinking in React" blog, I'm inspired to create something similar. Instead of filtering an array table, I want it to display the row that matches the input value. I have attempted this and created a jsfiddle example here: j ...

When a user chooses an item, the ui-select dropdown appears hidden behind additional fields on the screen

In my AngularJS application, I am utilizing ui-select within a table that repeats rows using ng-repeat. The following code snippet displays how ui-select is implemented: <ui-select name="{{'selProperty' + $index}}" ng-model="thing.property" ...

Optimal approach for managing numerous modals containing map data in Next.js

Hey there, I'm facing an issue while trying to utilize map data in conjunction with modals. Although I have set the state for all modals, when I use an array object within the map data, the modals end up showing duplicated. To provide clarity, let me ...

Why is Ajax/FormData rounding my decimal values?

When sending data from a form to my PHP script, I am utilizing the new FormData() method to retrieve the values. However, there are additional values that I append later on which are not part of the form: var fd = new FormData(document.getElementById(&apo ...

Select an image based on the input value provided

I'm new to coding and I'm attempting to replicate the search functionality of icomoon where typing a word displays related images. However, I'm facing an issue where I can't seem to get the value entered in the input field to trigger an ...

The standalone package for webpack's CLI tool has been introduced as webpack-cli

Currently venturing into the world of React.js, I decided to follow the tutorials provided on tutorialspoint. However, during the implementation phase, I encountered an error message in the console upon executing the 'npm start' command: C:&bsol ...

How to handle a Vue element click event through programming

In my Vue instance, I am looking to have a response triggered by a click on an uploaded thumbnail. Utilizing the Vue package called FineUploader Vue with the template layout as outlined in the documentation (refer to end of question). After uploading an i ...

The maximum JSON length property limit was exceeded in the Ajax POST data in a JavaScript file

I'm encountering an issue when trying to send a large amount of data from a .js file to a .cs file through an ajax call. The error message I'm receiving is: "Error during serialization or deserialization using the JSON JavaScriptSerializer. The ...

Middleware that is commonly used by both error handling and regular request-response chains

I am currently working on implementing a middleware that can set a variable in the sequence of both error and non-error scenarios. Is there a way to achieve this without disrupting the normal flow of middleware? Specifically, when I include the err param ...

Showing a picture as a result using Express in connection with Node.js API to AngularJS

My approach was to save the image in a server directory and store its path in MongoDB. When a client (AngularJS) requests details, Express should send an array of objects where one object property contains the image and the rest contain data. I obtained th ...

Creating a form that can identify both letters and numbers using JavaScript

Is it possible to create an <input> field that can recognize both letters and numbers while disregarding spaces and capitalization? Here is my current progress, aiming for the feedback to display as "right" when 9 H 6 U 8 f is entered in the field, ...

Questions about setting up a local development environment for Angular.js

After completing a few tutorials on Angular.js, I was eager to start building projects from scratch locally. However, despite my efforts, I have not been able to successfully set up my local development environment. I tried copying the package.json from A ...

Tips for implementing a repeater item to function beyond the ng-repeat directive

Recently, I decided to play around with AngularJS and now I seem to be facing a small issue with ng-class. Specifically, I'm attempting to change the color of an awesome font icon. <div ng-controller="MyCtrl"> <i ng-class="{'test': ...

Angular promise fails to resolve after an extended period of waiting for a response

My application is designed to send GET requests to my node/express server. In order to monitor changes in the server code, I have implemented a setTimeout function. While the promise function on the client side initially functions properly for a short peri ...

Ways to block past dates on bootstrap date picker starting from the current date and how to prevent dates beyond 90 days in the future from being selected on the

I am currently facing an issue with disabling previous dates from the current date in my code. While the functionality works well for the "From Date" date picker, I am having trouble implementing the same restriction for the "To Date" date picker after 90 ...

When an element in vue.js is selected using focus, it does not trigger re

One of my tasks involves tracking the last selected input in order to append a specific string or variable to it later on. created: function () { document.addEventListener('focusin', this.focusChanged); } focusChanged(event) { if (event ...

Color schemes for items in the Windows store app

I'm having trouble changing the background color of an item in my split application. I've tried using CSS, but nothing seems to work. Here is the template for the item (default style): <div class="itemtemplate" data-win-control="WinJS.Bindin ...

Using Jquery to store input values from within <td> elements in an array

I'm trying to capture user input from a dynamically changing table with 2 columns. How can I retrieve the data from each column separately? The size of the table is adjusted by a slider that controls the number of rows. Below is the structure of my ta ...

Steps to trigger an alert when the entered quantity exceeds the current stock levels

After developing an IMS System, I encountered a problem where my stock is going into negative figures. To resolve this issue, my primary goal is to trigger an alert whenever the quantity entered by a user exceeds the available stock. For example, if a us ...

Issues with logging functionality in my React Native application

I am currently facing an issue with my React Native app running on the Android Studio emulator. The logging does not appear in my terminal or in the active remote debugger in Chrome when debugging is enabled. When I attempt to log a simple text in one of m ...