Protractor: encountering difficulty retrieving all elements with Protractor

Having some trouble retrieving all elements using CSS or a repeater.

this.clickRandomEmployee = function(){      
        employees = elementController.getAllElements('css','#employee-list li');
        numberOfEmployees = employees.length;
        console.log(numberOfEmployees); 
        var numRand = this.getRandomNumber(numberOfEmployees);
        console.log(numRand);
        elementController.doubleClick(employees.get(numRand));
    }

this.getAllElements = function(locatorType,value){
        var emps;       
        console.log(locatorType);
        console.log(value);
        if(locatorType=='css'){
            emps = element.all(by.css(value));
        }
        else if(locatorType=='repeater'){
            emps = element.all(by.repeater(value));
        }   
        return emps;
    };

The code above is being called from a test script in an attempt to find all elements, however it returns undefined. Any suggestions on how to resolve this issue?

Answer №1

Instead of using the getAllElements function, consider simplifying your code with these lines:

employees = element.all(by.css('#employee-list li'))

and

employees = element.all(by.repeater(value))

To ensure you are getting the value of the repeater before proceeding, you can use a then statement like this:

employees = element.all(by.css('.items li')).then(function(returnedList) {
    numberOfEmployees = returnedList.length;
...
})

Answer №2

retrieveAllItems yields a promise that resolves into an assortment of items. Remember, promises do not have a size property. Instead, utilize the calculate() function:

employees = elementHandler.retrieveAllItems('css','#worker-list li');
employees.calculate().then(function (numOfWorkers) {
    var randomNumber = this.getRandomValue(numOfWorkers);
    console.log(randomNumber);
    elementHandler.doubleTap(employees.get(randomNumber));
});

For more information, refer to:

  • calculate() versus size in Automation Testing

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

Update the information within a specific DIV element

Is there a way to refresh content inside a div that is already on the same page, rather than being loaded from another page? For instance: <?php $this = 1; ?> <div id= "refresh_this"> <?php echo $this; ?> </div> ...

Error in Angular2: "Promise" name not found despite successful installation of global dependencies

I'm currently developing an application using Angular 2 and Node.js. I've already installed all the necessary dependencies specified in package.json. Inside this file, there is a postinstall command that should install the required dependencies m ...

What steps can I take to ensure the thread safety of my C# .NET POCO?

I am currently learning C# .net and working on a Selenium C# Project that involves the BrowserFactory class and a POCO named Driver. The InitBrowser method is used to set the value. However, when attempting to run tests using Parallel Test in NUnitFramew ...

Testing Angular: Inability to achieve full code coverage for Ternary branch in unit testing

Currently, I am attempting to test a ternary branch that utilizes the window.location property. @Injectable() export class ABCService { private hostUrl = (window.location.host.indexOf('localhost') > -1) ? 'example.com' : window.lo ...

Clear the history of a dynamically generated button using jQuery

Greetings fellow StackOverflow users! I'm currently tackling a project that requires jQuery to implement a master/detail table layout in asp.net C#. The master and detail tables need to be generated dynamically. The issue I'm facing involves gen ...

Seamless integration of Applitools with WebdriverIO

Seeking assistance to integrate Applitools with my WebdriverIO project. Find the specifications below: Node Version: v12.18.2 NPM Version: 6.14.5 WebdriverIO Version: 6.3.6 The service in my wdio file is configured as follows: services: ['selenium-s ...

Error message: Uncaught TypeError - Unable to retrieve data using POST method in react with node and express servers

I am currently working on creating a Login / Register form using react for the client-side and node / express / (mongo) for the backend. The backend functionality is working smoothly as expected, with successful storage of credentials in the database upon ...

PHP code isn't properly redirecting to AJAX calls

I have the following JavaScript code that validates a form by calling a PHP script on the backend: $(function() { // Setting up form validation for the #register-form element $("#register_form").validate({ // Specifying the validation ru ...

Tips for displaying content from JavaScript onto a div element in HTML

Javascript function validateForm() { var result; var keywords = document.querySelectorAll('#keywords'); [].slice.call(websites).forEach(function(website) { if (website.value == '') { ...

The ng-change event in AngularJS is not being activated by IE 11

Hello everyone, I am currently working with the angularjs framework and implementing a datepicker functionality. Unfortunately, the input type date is not functioning correctly on Internet Explorer. As a workaround, I have utilized jquery and css to create ...

Setting the backEnd URL in a frontEnd React application: Best practices for integration

Hey there - I'm new to react and front-end development in general. I recently created a RESTful API using Java, and now I'm wondering what the best way is to specify the backend URL for the fetch() function within a .jsx file in react. Currently, ...

Exploring the basics of caching in Node.js and Express: a beginner's

Picture an application with numerous users and small sets of data (up to 10 name/value pairs), but the process to access this data involves complex calculations. The concept is based on a system with a 'history' state that is crucial for obtaini ...

Tips for managing various errors in an Express API - such as addressing 404, 405, 400, and 500 errors

I am still learning the ropes of node.js and am using the express framework to create a REST API. I am looking to manage multiple errors for my API, specifically handling 404, 405, 400, and 500 errors. While express provides a default error handler, I am u ...

Why is the axios.get promise not getting resolved?

I am currently working on fetching data from MongoDB atlas within a React app. However, despite using .then and .catch with axios.get(), I am encountering an unresolved promise. Here is the code snippet: const entry = axios.get('http://localhost:3001 ...

What could be the reason for the ineffective application of the padding attribute on my button?

Just a heads up, I've been attempting to customize the appearance of my HTML document through my JavaScript file. Specifically, I am currently using a forEach loop to style my buttons, but for some reason, they remain positioned right beside each othe ...

Saving the subtracted value from a span into a cookie until the checkbox is unchecked - here's how to

I am working on a piece of code that includes numeric values within a span. When the checkbox is clicked, it subtracts 1 from the main value, essentially reducing the total value. How can I achieve this so that even after the form is submitted, the deducte ...

Acquiring the safe area of the iPhone X through JavaScript

The CSS properties safe-area-inset-left, safe-area-inset-right, safe-area-inset-top, and safe-area-inset-bottom are available, but is there a way to retrieve these values using JavaScript? ...

Steps for programmatically closing a Dialog Window in a showmodeldialog window

When opening the window, I follow this approach: var MyArgs = new Array(ParmA, ParmB, ParmC, ParmD, ParmE, ParmF); var leftpost = getWindow_TotalWidth() - 1000 - 100; var WinSettings1 = "dialogHeight:580px; dialogWidth:950px;edge:Raised; center:Yes; resi ...

Troubleshooting Failure of Click() Method in Selenium Webdriver Using Internet Explorer Driver

I am currently utilizing the IE driver with IE11. I have encountered an issue where the Click method for certain elements only selects the element without actually performing the click action. Interestingly, this script works fine with ChromeDriver and Fir ...

The error message "The function XXX.isSupported is not defined" is displayed

Integrating a JavaScript library into Typescript When I use this function in JavaScript, it works perfectly: _clickHandler() { TouchID.isSupported() .then(authenticate) .catch(error => { AlertIOS.alert('TouchID not supported'); }); ...