Finding and clicking an ng-click button within a ng-repeat directive

I want to conduct a test on a web page built with AngularJS using Selenium WebDriver and Protractor.

Here is the HTML snippet that needs to be tested:

<tr ng-repeat="item in items" class="ng-scope">
    <td class="ng-binding">
        Item1
    </td>
    <td class="ng-binding">
        description
    </td>
    <td class="ng-binding">
        1234
    </td>
    <td>
        <div ng-click="AddItem(item.id)" class="btn">Add Item</div>
    </td>
</tr>

<tr ng-repeat="item in items" class="ng-scope">
    <td class="ng-binding">
        Item2
    </td>
    <td class="ng-binding">
        description
    </td>
    <td class="ng-binding">
        1235
    </td>
    <td>
        <div ng-click="AddItem(item.id)" class="btn">Add Item</div>
    </td>
</tr>

This is how it appears:

screenshot of the html angular page

Despite going through documentation, tutorials on youtube, and searching online, I am still unsure how to find and click "Add Item" for a specific item with a particular item id.

Could you please clarify the process for me?

Answer №1

If you don't find the specific id in the DOM, it means you won't be able to locate an element using that method.

Here is a code snippet that I personally utilize:

element.all(by.repeater('item in items')).filter(function(row){
 return row.all(by.tagName('td')).first().getText().then(function(val){
     return val === "Item1" //you can customize this as needed
  })
}).first().$('[ng-click="AddItem(item.id)"]').click();

Answer №2

Aside from @Danny's response, you can also retrieve the item.id directly from the DOM by utilizing the evaluate() method. Give this code snippet a try:

element.all(by.repeater('item in items')).filter(function(row){
   return row.evaluate('item.id').then(function(id){
      return val === "1234";
    })
}).first().$('.btn').click();

Ref: http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.evaluate

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 is the best way to extract a message as an object from a JSON data in discord.js?

Currently in the process of developing a discord bot with an election feature for my server's moderator. All the necessary election data is being saved in an external JSON file to ensure that if the bot crashes during an election, it can seamlessly re ...

jquery ajax function that returns an object when successful

Below is a brief example of an AJAX call wrapped in a function. MyNS.GetStrings = function (successCallback, errorCallback) { var url = serverUrl + "/GetStrings"; $.ajax({ type: "GET", contentType: "application/json; charset=utf-8", dataType: ...

jquery ajax call not returning data

I'm new to asp.net and currently exploring how ajax responses work. Here's the code I am working with: $(document).ready(function () { $('#<%=cbx_pep.ClientID%>').change(function () { var mSis = $('#<%=cbx_pep ...

Can you switch between displaying and concealing a dropdown in the same location based on chosen values?

I have four dropdowns listed, but I only want to display one at a time. Depending on the selected values, when I try to show a dropdown, it does not replace the current one; instead, it just appears next to the existing dropdown. Is there a way to keep th ...

Experiencing excessive delay in loading data into the DOM following a successful response from a service in AngularJS

I am facing a challenge in loading a large set of data into an HTML table. To begin, you need to click on a button: <a ng-click="get_product()">load data</a> This button triggers a function called get_product: $scope.get_product = func ...

Incorporate a FontAwesome Icon into your jsPdf Document

I recently updated to the latest version of jspdf.debug.js. Unfortunately, when I try to render FontAwesome icons in a PDF document, they are not displaying properly. I have added a user icon using FontAwesome on my webpage and included an image for refer ...

Tips for linking real-time information to an array using AngularJS

Exploring Angular for the first time, I've embarked on creating a shopping cart web application. I came across a cart plugin online at https://www.codeproject.com/Articles/576246/A-Shopping-Cart-Application-Built-with-AngularJS, but it only supports s ...

Element not being located by Selenium post-click operation

Currently, I am working with Selenium using Robot Framework to test a web application built in React. Unfortunately, I have encountered an issue where my Selenium test fails to locate an element after clicking on a link. I attempted to address this proble ...

VueJS .filter unexpectedly alters array data

My goal is to filter an array based on the contents of another array, and here is the code I have written for it. private get filteredArray() { const filteredArray = this.unfilteredArray; console.log(this.unfilteredArray); filteredArray = this.unfil ...

Using CSS and Vue, you can customize the appearance of inactive thumbnails by displaying them

My goal is for a clicked thumbnail to display in color while the others show as grey. The active thumbnail will be in color, and I want inactive thumbnails to appear in grey. Here is what I am trying to achieve: Vue.component('carousel', { ...

Guide to configuring a background image from the public folder in a React application using Create React App

How do I properly set a background image in a .css file located in the src folder? src/Components/Component/Comp.css .services { background-image : url("./images/img-2.jpg"); background-position: center; background-size : cover; b ...

Click on a selenium python button

https://i.sstatic.net/Qkm0B.png I'm looking for the bot to select the option for male However, I've attempted using the following code without success: driver.find_element_by_xpath("//input[@name='sex']").click() Can someone ...

Decoding JSON objects using JavaScript/jQuery

I am working with a Json object that looks like this. { "firstName":"John", "lastName":"Doe" "123":"456" } My goal is to access the keys and values in order to populate fields. However, for some reason it is not working as expected even though I have ch ...

Testing the display of an Angular-UI modal through unit testing

Lately, I have been diving deep into testing controllers with modals, modal controllers, and directives. It feels like almost everything in my app that can be tested has been tested, and the results are incredibly positive. However, I have encountered a n ...

Retrieve a specific nested key using its name

I am working with the following structure: const config = { modules: [ { debug: true }, { test: false } ] } My goal is to create a function that can provide the status of a specific module. For example: getStatus("debug") While I can access the array ...

Achieving the retrieval of all data can be done simply by utilizing the `echo json_encode($data);

I am trying to automatically update the user wall with new data from the database using a script that checks for updates every 15 seconds. Everything works fine when I only use one piece of data like $data = $row['description']; in the server.ph ...

Issues with the "required" functionality in Angular Schema Form on select and checkbox fields are not being resolved

I am encountering difficulties with the required validation of select and checkbox fields in Angular Schema Form as a beginner. Within $scope.schema, I have a select field called designation: "designation": { "title": "Designation", "type": "sele ...

Splitting JavaScript Arrays: Exploring Efficient Division

I'm attempting to develop a recursive function that divides an array in half until it only consists of lengths of 3 and 2. After dividing, I want to neatly organize all the new arrays into another array. My idea is to find a way to determine the numb ...

The specified type `Observable<Pet>&Observable<HttpResponse<Pet>>&Observable<HttpEvent<Pet>>` is not compatible with `Observable<HttpResponse<Pet>>`

I'm currently attempting to integrate the Angular code generated by openapi-generator with the JHipster CRUD views. While working on customizing them for the Pet entity, I encountered the following error: "Argument of type 'Observable & ...

My React application came to an unexpected halt with an error message stating: "TypeError: Cannot read properties of undefined (reading 'prototype')"

Everything was running smoothly with my app until I encountered this error without making any significant changes: TypeError: Cannot read properties of undefined (reading 'prototype') (anonymous function) .../client/node_modules/express/lib/resp ...