Get the specific element at the given index from an array of elements using

I'm currently tackling a challenge with my angular e2e test project. The task at hand involves selecting the second element within an ng-repeat that has the heading "Topic - xyz" and then clicking on the button that is a sibling of this element. How should I approach this?

<div class="row ng-scope" ng-repeat="post in posts">
    <div class="col-md-7">
        <h4 class="ng-binding">Topic - ABC</h4>
        <div class="text-right">
            <button class="btn btn-none btn-sm" ng-click="posts.newPost()">
                Create Post
            </button>
        </div>
    </div>
 </div> 
<div class="row ng-scope" ng-repeat="post in posts">
    <div class="col-md-7">
        <h4 class="ng-binding">Topic - XYZ</h4>
        <div class="text-right">
            <button class="btn btn-none btn-sm" ng-click="posts.newPost()">
                Create Post
            </button>
       </div>
    </div>
 </div>  
<div class="row ng-scope" ng-repeat="post in posts">
    <div class="col-md-7">
        <h4 class="ng-binding">Topic - EFG</h4>     
        <div class="text-right">
            <button class="btn btn-none btn-sm" ng-click="posts.newPost()">
                Create Post
            </button>
        </div>
     </div>
</div>

My attempts to achieve this have been unsuccessful so far.

var button = $$(by.repeater('post in posts')).get(1).$(by.css('[ng-click="posts.newPost()"]'))

button.click(); // click is not showing up

Answer №1

$(by.repeater('post in posts')) and

$by.css('[ng-click="posts.newPost()"]')
- these examples do not show the correct syntax for using the by.repeater() or by.css() locators. The use of $$ is actually a shortcut for element.all(by.css()) and should not be used with the "repeater" locator. When using $(), there is no need to wrap the selector in by.css():

var button = element.all(by.repeater('post in posts')).get(1).$('[ng-click*=newPost]');
button.click();

To narrow down the repeater element based on the topic name, you can utilize the .filter() function:

var button = element.all(by.repeater('post in posts')).filter(function (post) {
    return post.$("h4").getText().then(function (postTitle) {
        return postTitle === "Topic - XYZ";
    });
}).get(1).$('[ng-click*=newPost]');
button.click();

You may also consider trying out the by.buttonText locator which could provide a cleaner solution:

var post = element.all(by.repeater('post in posts')).get(1);

var button = post.element(by.buttonText("Create Post"));
button.click();

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

Angular: Assign a value to ng-model using a variable from the scope

I am trying to set a default value in my ng-model from the scope, but for some reason it's not recognizing the value. Here is what I have in my controller: $scope.defaultRole = "Admin"; This is how my view looks like: <input type="text" class= ...

Exploring AngularJS $compile and the concept of scoping within JavaScript windows

I've encountered a scoping issue with the use of this inside an angular-ui bootstrap modal. The code below functions perfectly outside of a modal, but encounters problems when run within one: var GlobalVariable = GlobalVariable || {}; (fun ...

Connecting to deeply nested attributes within an object using specified criteria

I apologize if the title of my query is not very descriptive, I couldn't come up with a better one. Please feel free to suggest improvements. I am currently working on developing a reusable "property grid" in Angular. My goal is to create a grid wher ...

The ng-view directive within AngularJS appears to be malfunctioning

I am facing an issue with my simple Angular app. I have two links that are supposed to change the URL and display the correct view within the same single page application. However, when I include the controllers' module in the main module, it stops wo ...

The .val() and focus() methods are not functioning correctly

I am having an issue with a simple script: when I input a link to an image in the form's INPUT field, it should automatically display a preview of the image: https://example.com/image.jpg However, when I input the same link not by using ctrl+C/ctr ...

Retrieve the value of an unmatched ng-pattern field

<div> <input type="text" name="rate" ng-model="rate" ng-pattern="/^[0-9]+(\.[0-9]{0,2})?$/"/> </div> <span class="text-warning control-label"> {{rate}} ...

Angular handling multiple query parameters

I am looking to implement multiple path routes for a component named Component2. I want the functionality to be similar to GitHub's file navigation within repositories, but I do not want to hardcode a path like 'source/:filePath/:filePath/:filePa ...

Chained module incorporating a specialized Angular form validation directive

I've been working on incorporating an angular form validation "plugin," but I've hit a roadblock trying to utilize the directive in a child module. As a test, I'm using the HighlightDirective example. @Directive({ selector: '[highligh ...

Traversing through nested arrays within nested objects using AngularJS

In my project, I am utilizing angularjs 1 and dealing with a complex json object that has multiple levels of nesting. My goal is to use ng-repeat in order to access a nested array within the json structure. [{ "information": { "name": "simdi jinki ...

Having trouble getting ng-repeat to work properly alongside Bootstrap collapse?

Using a bootstrap 4 card to create a blog post. When the View Comments link is clicked, a collapsed div within the card-footer should open to display all comments. The collapse functioned correctly with hard coded html and dynamic data {{blog.title}} until ...

Lamenting the Perils of Losing AngularJS Rootscope Data upon Refresh

Currently, I am facing an issue in AngularJS 1.x. When I save a value in the $rootScope and pass to the next page or router, unfortunately, the $rootScope value gets lost upon refreshing the page (F5/Window Reload). I need a solution that doesn't inv ...

What could be causing the AngularStrap modal to flicker when I attempt to close it?

Currently, I am utilizing angularjs version 1.4.4 and angularstrap version 2.3.7. Following the guidelines in the angularstrap documentation, I have incorporated angularmotion from its [github][1]. However, while using a modal, I noticed that there is a ...

Error encountered in MEAN stack when making an AJAX POST request: TypeError occurs when attempting to access property 'userName' of an undefined object

Currently, I am involved in the development of a MEAN stack application for my school project. The main objective is to allow users to view and submit highscores for Galaga and Dig Dug games to a MongoDB database. An issue that I am facing is: POST http ...

Sending an object and receiving it in an Angular modal window

I am struggling to pass Objects into a modal. I am unsure how to pass an argument into a modal, so I am attempting the following: vm.viewGroupDetail = function(userDetails) { var scope = $scope.$new(); scope.userDetails = userDetails; vm.mod ...

Bower consistently installs the most up-to-date version instead of the version that was explicitly specified

Despite the version specified in the "bower.json" file, bower (v1.8.0) doesn't adhere to it and instead downloads the most recent version of the library available. It seems like it's not taking the version into account at all. Even downgrading to ...

Anticipating outcome: row 1 column 1 (character 0) in Python

As a newcomer to the world of Python, I am currently attempting to parse data in my application using the following lines of code: json_str = request.body.decode('utf-8') py_str = json.loads(json_str) Unfortunately, when I reach this line (jso ...

Return user to the previous webpage upon successful login using Node

I am looking for a way to redirect users who are not logged in from an individual post's page to the login screen, and then back to the post after they have successfully logged in. Here is my login route: router.get('/login', function(req, ...

Guide to uploading multiple images to an Amazon AWS S3 bucket using Node.js, Angular, and JavaScript

Can anyone share insights on where and how E-commerce websites typically upload their product images? Has anyone had experience using Amazon AWS S3 bucket for this purpose? Additionally, does anyone know how to upload or retrieve multiple images at once ...

Error: The 'in' operator cannot be utilized to search for 'length' in the Kendo UI framework

I am encountering an error while using Angular 1.4, jQuery 1.11.3, and the latest Kendo UI. I'm wondering if this issue is a known bug to Telerik. The only Kendo controls present on the page are kendo-multi-select and kendo-date-picker. TypeError: C ...

The AngularJS component materializes almost instantaneously

Using AngularJS, I have developed an application where a certain section of code is displayed after the page loads for a split second: <div class="col-sm-4" data-ng-repeat="user in users"> <div class="card"> ...