What is the best way to loop through and assign various values to an angular model using $index in Protractor?

To create three text boxes with different values, I need to click a button three times using the following code:

for (var i = 0; i < 3; i++) {
    element(by.id('protractor-type')).click();
};

After clicking thrice, three text boxes will appear. The HTML code for the text box is:

<input type="text" required="" ng-model="config.eTypes[$index]" ng-change="updateEySettings()" class="form-control ng-pristine ng-invalid ng-invalid-required ng-touched" placeholder="Eg: Dancing" tabindex="0" aria-required="true" aria-invalid="true">

Using the code below, you can give a value to only one textbox:

element(by.model('config.eTypes[$index]')).sendKeys('1st box');

However, to provide values to all textboxes, attempt like this was unsuccessful:

element.all(by.repeater('type in config.eTypes track by $index')).then(function(arr){
     arr[0].sendKeys('cat');
     arr[1].sendKeys('ball');
     arr[2].sendKeys('bat');
 });

If you have suggestions on how to achieve this properly, please share.

Answer №1

When working with Protractor, it is best to use functions for iteration as they are reliable. The for() loop tends to execute much faster than clicking buttons in Protractor. Here's an example:

for (var i = 1; i <= 3; i++) {
    clickButton(i);
}
function clickButton(value){
    element(by.id('protractor-type')).click();

    // Verify the presence of the desired element after clicking the button using a custom wait() function
    browser.wait(function(){
        return element.all(by.model('config.eTypes[$index]')).then(function(array){
            return array.length === value;
        });
    });

    // Optionally send values to the input fields here if needed
};

This code will click the button 3 times and ensure that 3 input elements are created each time it is clicked. You can populate the input fields using .all() function. Since there is no ng-repeat attribute on your element, you cannot use by.repeater. Here's how to do it:

element.all(by.model('config.eTypes[$index]')).then(function(array){
     array[0].sendKeys('cat');
     array[1].sendKeys('ball');
     array[2].sendKeys('bat');
 });

I hope this explanation proves useful.

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 steps can I take to modify my webpage, like a dashboard, on the internet?

Whenever I click on a menu item in the sidebar of my HTML file, I want only the body section to change without affecting the navigation or sidebar content. For instance, if I select the "Coding Convention" menu in the sidebar, I would like the page to swi ...

Tips for adjusting the position of an infowindow in ArcGIS

I have implemented the use of infowindow in arcgis to display certain information. https://i.sstatic.net/Dnpas.jpg Currently, the infowindow is appearing directly over my icon. Is there a way to adjust its position if it covers the icon? ...

The WordPress site you are trying to access does not have the 'Access-Control-Allow-Origin' header included in the requested resource

I am currently facing an issue with my MVC application where I am using jQuery .get() to fetch data from our company's Wordpress site to display on a page. Surprisingly, this functionality works fine in IE 11 but fails in Chrome and Edge browsers. Be ...

Incorporate a PHP file using AJAX in a development project with Vue

After utilizing vue-cli for constructing a Vue project, I encountered an issue with one of its scripts in the package.json file: "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot", I rely on this script for my project development. Ho ...

Determine time without discerning the timezone of the device

This script checks the current time against a specified deadline time. // Get current date/time var now = new Date(); // Set up deadline date/time var deadline = new Date(); //deadline is 1830hrs; deadline.setHours(18); deadline.setMinutes(30); // Chec ...

Utilizing Rails to incorporate a comment box through jquery on individual posts within a index page showcasing all listed posts

As I display a list of posts on my index file, I want each post to have its own comments section. To achieve this, I have added a "Show Comments" button for each post. However, when I click the button, jQuery triggers all comment boxes on the page instead ...

What steps should I take to ensure my .js.erb files are compatible with Rails 7?

I have been following a Rails Tutorial on Building a Link Shortener with Rails 6 and Ruby 2.xx to create an app. However, I am using Rails 7.0.4 and Ruby 3.0.0. I encountered an issue with my create.js.erb file not functioning properly. After some research ...

Troubleshooting Challenges with JSON and jQuery Encoding

Having an issue with displaying data from a JSON file using jQuery: { "nombre": "Jesús Ramírez", "edad": "25 Años", "imagen":"file.jpg" } Here's my code: JAVASCRIPT $.getJSON(file.json, function(data) { var name= data.nombre; ...

What steps can I take to stop my React child component from fetching data each time it re-renders or mounts?

Before delving into the code, let me provide a brief overview of the question at hand. https://i.sstatic.net/oRc7m.png The image above showcases a work in progress Next.js application that displays the status of my Philips Hue lights by interacting with ...

Ways to determine if the v-menu is currently visible or hidden

I am currently working on a button with a logo displayed. The hover functionality is working fine, but I want the icon to change to a hamburger when the menu is opened and stay that way. Is there an option in v-menu that checks if the menu is open or shoul ...

There is a lack of communication between the tomcat application and the database

I am currently working on a web application that is stored in a war file and runs with Tomcat. Initially, I start my MySQL 5.7 database, followed by running a Spring Boot project created with the necessary files. After completing these steps, I deploy my ...

Creating a dynamic select functionality in Drupal forms

Being more focused on backend development, I am facing a challenge that may be simple for jQuery experts. In Drupal, I have two arrays - one containing names of views and the other having displays for each view. To populate these arrays, here is the code s ...

Issue with JSONP success function not being triggered

Here is an example of an Ajax call being made to a different domain: <script> $.ajax({ url: 'url?callback=?', dataType: 'jsonp', success: function (data) { alert(data[0].DeviceName) ...

Error: This Service Worker is restricted to secure origins only due to a DOMException

Having trouble implementing this on my website; it keeps showing the following error. Help, please! Service Worker Error DOMException: Only secure origins are allowed. if ('serviceWorker' in navigator && 'PushManager' in wind ...

Retrieve the currently selected element following the button press

My webpage features a Carousel with the following structure: <div class="my-controls"> <button class="button123"> <span class="abc">Next-image</span> </button> </div> <div class="my-carousel"> ...

What is the best method for loading a script in a PHP file that is triggered by an Ajax request?

Below is the JavaScript code that I am using: <script> if (str == "") { document.getElementById("txtHint1").innerHTML = ""; return; } else { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, ...

Illustrating Angular Scope and Controller Relationships in Sequence Diagrams

Seeking advice on creating sequence diagrams that involve Angular scopes or both Angular scopes and directives. Wondering if it's best to follow a flow like: User -> View -> $scope -> Controller -> Service Including $scope in the diagram ...

"Exploring the world of custom middleware in NextJs on a localhost

Question regarding nextjs page middleware: the documentation states that the request object should include geo, IP, ua, cookies, and nexturl properties. Visit the link for more information. I am attempting to retrieve the user's location using page m ...

NodeJS package 'jquery' npm not functioning properly (issue with $.on())

I've successfully installed and loaded jquery by using $ = require('jquery'); However, when I attempt to utilize it: app.get('/', function (req, res) { res.render('index'); $.on('ready', function () { ...

What is the best way to include a variable (string) in an XPath contains function?

Is there a way to locate an element containing specific text without hardcoding it? How can I make the text dynamic by turning it into a variable and passing it to the xpath function? public static void selectEvent(String eventName) { WebElement eventLi ...