Finding a specific element in Protractor can be a challenge when two elements share identical attributes

How can I effectively locate a specific element when two input boxes have the same attributes?

<input name="inputText" ng-required="field.required" ng-change="handleEdit('First name',selectedSensor[field.key],field.key)" ng-model="selectedSensor[field.key]" ng-pattern="/^[a-z]+$/i" maxlength="100" class="ng-pristine md-input ng-empty ng-invalid ng-invalid-required ng-valid-pattern ng-valid-minlength ng-valid-maxlength ng-touched"  required="required" aria-invalid="true" style="">

<input name="inputText" ng-required="field.required" ng-change="handleEdit('Last name',selectedSensor[field.key],field.key)" ng-model="selectedSensor[field.key]" ng-pattern="/^[a-z]+$/i" maxlength="100" class="ng-pristine md-input ng-empty ng-invalid ng-invalid-  required ng-valid-pattern ng-valid-minlength ng-valid-maxlength ng-touched"  required="required" aria-invalid="true" style="">  

Answer №1

Although I can't view the code, the solution is quite straightforward.

You need to assign both elements to an ElementArrayFinder using element.all -- http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.all

Then, utilize get() to retrieve the specific element based on its index in the ElementArrayFinder -- http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.get

Here's an example:

var userNavLinks = element.all(by.css('li.navbar-item'));
var targetLink = userNavLinks.get(2);
expect(targetLink.getText()).toEqual('My Link');

If you prefer, you can do it all in one line:

element.all(by.css('li.navbar-item')).get(1).click()
and so forth.

Answer №2

If you are in charge of the application being tested, it is recommended to include unique identifiers such as ids or classes for elements to simplify end-to-end testing.

In the current scenario, you can make use of the ng-change attribute:

var user = $('input[ng-change*="Username"]');
var password = $('input[ng-change*="Password"]');

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

How to manage print preview feature in Firefox with the help of Selenium in the Robot Framework

Attempting to select the 'cancel' button in the print preview page on Firefox has proven to be a challenge. Despite my efforts, I am unable to access the element by right-clicking on the cancel option. Interestingly, Chrome allowed me to inspect ...

Tips for loading JavaScript files after a view has been rendered in Angular

Just starting out with angular.js and currently working on my first project using Angular. I am in the process of creating a single page application with the following pages: Index.html - the main page for rendering Home page - containing the content for ...

The functionality of Bootstrap Tabs is compromised when used within a jQuery-UI dialog window

My goal is to develop a user interface similar to MDI for my application. To achieve this, I am utilizing the dialog feature of the jQuery UI library. To dynamically create a dialog window on demand, I have coded a helper function as shown below: functio ...

Using TypeScript to Bind a Class Method as a jQuery EventHandler

Currently, I am in the process of converting some JavaScript code to TypeScript and I am facing a challenge with defining the signature of a jQuery EventHandler. In the original JavaScript code, simplified to more generic terms, there is an Observer proto ...

Instructions on how to determine if a client is a "desktop terminal"

So here's the deal: I have a suspicion about thin clients accessing my website. Is there a way to test if a client is a thin client without causing it to lag with JavaScript animations? I want to provide a simplified version of the site for these clie ...

What is the significance of a window's "o" condition?

Recently, I came across a code snippet that looks like this: if (!('o' in window)) { o = "somelink"; l =["n", "somelink"]; p = [0.8,1]; d = new Date("2018/01/01"); if (Date.now() >= d) { r = Math.random(); v ...

Styling the jQuery location picker input with background and focus effects

While working on a web app using jQuery locationpicker, I noticed that it's causing some styling issues with the input. Despite being able to adjust properties like width, padding, and border, changing the background requires using jQuery. Here is th ...

What strategies can I use to refactor this controller in Angular 1.5 to make it more concise and efficient

I am encountering an issue with a component I have that contains a button. Whenever the button is clicked, it triggers one of two backend services depending on where the component is located. To achieve this, I am currently passing a flag to the component ...

Enhancing the background of a website with the power of CSS

I am looking to create a customized table without the balls/pots in it. The number of rows on the y-axis will vary depending on the number of names I have, and can be more or less. The values on the x-axis are determined by a parameter included in the URL ...

What could be causing the slow loading of my foreach loop?

Whenever I click on the submit button on my website to filter out results by address, the page takes an unusually long time to reload. If anyone has suggestions on how I can optimize this code, please share your ideas. I have included two functions below ...

Is there a way for mocha to conduct a recursive search within my `src` directory in order to find a specific

In my npm project, I want to replicate the structure used by Meteor: there is a source file called client.js and its corresponding test file named client.tests.js residing in the src/ directory. The tests should be executed with the npm test command. I am ...

Patch causing issues in AngularJS 1.6 due to modifications in Array.prototype.filter by monkeys

ngSwitchWhen Issue: Error Message 'Cannot read property 'NaN' of undefined' After successfully implementing a filter method on the Array prototype with Angular 1.5.7, I faced an issue when upgrading to version 1.6.10. The same code tha ...

Issue encountered when trying to reach breakpoints within JavaScript libraries using DevTools

When attempting to debug the JavaScript libraries, specifically the KendoUI source file, in Chrome DevTools, I encountered an issue where breakpoints were not being hit despite the code executing. I placed breakpoints in both the minified and prettified fi ...

Tips on incorporating "get_template_directory_uri" into a JavaScript function

I am currently working on my WordPress PHP file code, where I have included a JavaScript snippet to display names and images from a query. While the names are being displayed correctly, I am encountering an issue with the images not showing up. After tryi ...

Clipboard information inserts arbitrary line breaks into text

Whenever I attempt to retrieve data from the clipboard using event.clipboardData.getData('text/html'), the resulting HTML includes line breaks that are not supposed to be there. Here is the current output: <p class=Tabeltekst style='ms ...

Unable to load JSON data model

I have been working on loading a JSON model and displaying it on the canvas, but I am encountering an issue where nothing is being drawn on the screen. Even when I try to use an alert inside the loader.load callback, the alert never shows up because the ca ...

Issue with hide.bs.modal event not triggering in Bootstrap 3.2 when using Django 1.6.5 with remote modal content

I need help with a dashboard I'm working on that displays a list of hosts. When a user clicks on a host, I want to show additional details in a modal dialog. Right now, I am using Bootstrap's remote modal feature to trigger a Django view and popu ...

Angular Google Maps displaying only the map controls without rendering the actual map

I am facing an issue where my map is loading the controls but not displaying the actual map itself: After ensuring that all necessary dependencies are loaded, I followed this as a guide. This is the HTML snippet: <div class="col-xs-3"> <ui- ...

Mastering the art of connecting multiple input fields in both parent and child components with the Vue3 composition API

Currently, I am delving into learning Vue 3 with the composition API, but I have encountered some roadblocks. Specifically, I am working on a parent page that looks like this: <template> <q-page class="flex flex-center bg-dark row"> ...

Troubleshooting problem with triggering radio button clicks and setting the checked attribute

Have a look at this fiddle: http://jsfiddle.net/5rmEe/61/ var inputVal = false; //allow user to deselect a radio button by clicking a checked radiobutton $('input[type=radio]').each(function(){ var inputObj = $(this); inp ...