Protractor - selecting a button within a table

Following the HTML code provided below, there is a table containing a list of test site links with a delete button next to each test site.

 /* Element locators in the table */
var testSiteLinks = element.all(by.css('a[ui-sref^="testpages"]'));
var deleteBtnCssStart = "body > div.container > div > div > div > div > div > div:nth-child(2) > table > tbody > tr:nth-child(";
var deteletBtnCssEnd  = ") > td > button";

var testSite = {
deleteSite: function(siteName){
        
        testSiteLinks.each(function(element,index){
                
              
              var temp = index;
              element.getText().then(function(text) {

                  
                 temp++;
                 var patt = new RegExp(siteName);
                 if(patt.test(text)){
                   
                    // CSS locator for delete button corresponding to the test site
                    var test = deleteBtnCssStart + temp + deteletBtnCssEnd;
                   
                    // Click on the delete button, but this step is failing with the error "TypeError: object is not a function"
                    element(by.css(test)).click();  
                 } 

 


            });   
 }); 
}

I have attempted to add a function as depicted above to search for a test site in the table and delete the site if it exists in the table. However, the test is failing when trying to click on the delete button, displaying the error "TypeError: object is not a function". Could you please provide guidance on any corrections needed?

Please see the HTML code for the table along with the test site links below.

<table class="table card">
    <thead></thead>
          <tbody>
          <!-- ngRepeat: site in Sites --><tr ng-repeat="site in Sites" class="ng-scope">
            <td>
              <button ng-hide="role.DeletingSite===true" ng-click="role.DeletingSite=true" class="delSite pull-right btn btn-sm btn-danger" tabindex="0" aria-hidden="false">Delete</button>

              <div ng-show="role.DeletingSite===true" class="pull-right ng-hide" aria-hidden="true">
               <a ng-click="role.DeletingSite=false" class="btn btn-sm btn-default pull-right" tabindex="0">Cancel</a>
               <a ng-click="deleteSite(site)" class="reallyDelete btn btn-sm btn-danger pull-right " tabindex="0">Delete</a>
              </div>

             <a ui-sref="testpages({id:site.id, name:site.name, host:site.host, httpsSupported:site.httpsSupported})" class="ng-binding" href="#/testsite/pages/40288a884cdaa49a014cdbfb08270003/testsite/testsite/true   ">testsite (testsite)</a>
                        </td>
                    </tr><!-- end ngRepeat: site in Sites --><tr ng-repeat="site in Sites" class="ng-scope">
                        <td>
                            <button ng-hide="role.DeletingSite===true" ng-click="role.DeletingSite=true" class="delSite pull-right btn btn-sm btn-danger" tabindex="0" aria-hidden="false">Delete</button>

                            <div ng-show="role.DeletingSite===true" class="pull-right ng-hide" aria-hidden="true">
                                <a ng-click="role.DeletingSite=false" class="btn btn-sm btn-default pull-right" tabindex="0">Cancel</a>
                                <a ng-click="deleteSite(site)" class="reallyDelete btn btn-sm btn-danger pull-right " tabindex="0">Delete</a>
                            </div>

                            <a ui-sref="testsitepages({id:site.id, name:site.name, host:site.host, httpsSupported:site.httpsSupported})" class="ng-binding" href="#/test/pages/40288a884cd57e14014cd60701890001/TestSite.org/testsite.org/false   ">Testsite.org (testsite.org)</a>
                        </td>
                    </tr><!-- end ngRepeat: site in Sites -->
                </tbody>
            </table>

Answer №1

Initially, the element(by.css(test)) function would utilize the element variable within the internal scope derived from the each() function.

My alternative approach involves utilizing filter() and evaluate():

element.all(by.repeater("site in Sites")).filter(function (site) {
    return site.evaluate("site.name").then(function (name) {
        return name == siteName;
    });
}).then(function (sites) {
    if (sites) {  // a match is found - locate and click the Delete button
        sites[0].element(by.linkText("Delete")).click();
    }
});

The concept here is to iterate through all the sites in the repeater, evaluate the site.name, and confirm if it matches the desired one. Upon finding a match, the script proceeds to click the Delete button located within the repeater segment.

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

Issues with Ionic's ion-nav-title not refreshing properly

Despite my efforts to utilize ion-nav-title, I am facing an issue where the nav bar title fails to update when transitioning from a child state using ui-sref. Interestingly, the nav bar updates correctly when moving from a parent state. I have diligently ...

SelectBoxIt jquery plugin can be applied after the completion of populating options in the select element with AngularJS

Utilizing AngularJS, I am dynamically populating a select box with the code below: <select ng-model="department" ng-options="dept as dept.name for dept in departmentList" class="fancy"> <option value="">-- select an optio ...

How do I activate the <li> tag using jQuery?

I am currently implementing pagination on my webpage using the following JavaScript code: var pagingList = $('<ul>', {class: 'pagination list-unstyled list-inline'}); ...

Utilize a special JavaScript function to submit a concealed PayPal form

Looking to integrate a call to PayPal within a JavaScript function. The goal is for the PayPal call to occur prior to running myStep1 and myStep2. Check out the function below: function() { // insert PayPal call here, before myStep1 and myStep2 ...

Error message: "Attempting to assign a value to the 'size' property of an undefined object, despite the fact that

I recently started delving into NodeJS development and have configured Visual Studio Code for JavaScript development in node applications. During a Pluralsight lecture on Objects, the instructor demonstrated creating two scripts, dice.js and program.js, a ...

concealing the upper header while scrolling and shifting the primary header upwards

Is there a way to use CSS to move the main header navigation, including the logo and links, on my website up when scrolling down in order to hide the top black bar header that contains contact information? The website in question is atm.truenorthmediasol ...

Issue with applying CSS properties of 'top' and 'left' to a dynamically created div using

After an extensive search on both the site and the internet, I have not had any luck so far. I am dealing with a series of dynamically-generated divs using JQuery, cloned from the original HTML source. While I can manipulate the positioning properties of ...

Issue with Angular 13 Bootstrap5 Navbar's functionality

Angular 13 is my framework of choice, and I recently integrated Bootstrap 5 into my project using the command below: ng add @ng-bootstrap/ng-bootstrap As for the index.js file content, it looks like this: <!doctype html> <html lang="en" ...

Enable hyperlink to open in a new browser tab after user clicks on button embedded inside an iFrame

<head> </head> <div> <iframe src="https://....."></iframe> </div> https://i.sstatic.net/dk21i.png I'm looking for a way to open the link in a new tab when clicking on this button. Currently, the page loads wi ...

My goal is to implement a confirmation modal prior to any route changes within Next.js framework

Below is the code block that I have: const onRouteChangeStart = React.useCallback(() => { if (formState.isDirty) { if (window.confirm('Confirmation message')) { return true; } NProgress.done(); throw "A ...

Unexpected behavior: Angular post request does not include the expected request body

Embarking on my initial solo Angular project... I am endeavoring to make a post request to my freshly created web service and have implemented the following code: headers = new HttpHeaders( {'Content-Type':'text/plain'} ); l ...

There are additional elements that can be toggled, but I prefer to only toggle the one that I have selected

Every time I click on a table a, intending to toggle the .info inside the same div, it also toggles the .info in other divs. Can someone provide assistance with this issue? function info() { $('.table a').click(function() { $('.info ...

Experimenting with TypeScript code using namespaces through jest (ts-jest) testing framework

Whenever I attempt to test TypeScript code: namespace MainNamespace { export class MainClass { public sum(a: number, b: number) : number { return a + b; } } } The test scenario is as follows: describe("main test", () ...

JavaScript method overloading involves defining multiple functions with the same name

In my JavaScript code, I have implemented method overloading using the following approach: function somefunction() { //1st function } function somefunction(a) { //2nd function } function somefunction(a,b) { //3rd function } somefunction(); // ...

Adjusting canvas context position after resizing

I've been experimenting with using a canvas as a texture in three.js. Since three.js requires textures to have dimensions that are powers of two, I initially set the canvas width and height to [512, 512]. However, I want the final canvas to have non-p ...

Injecting cascading style sheets across different domains using Javascript

Put simply: Is it possible for an external Javascript code to inject another script that is hosted on a completely different domain into the page's Document Object Model (DOM)? For example: Imagine the website foo.com which has an html script tag w ...

Is there a way to manipulate a website's HTML on my local machine using code?

I am currently working on a project to create a program that can scan through a website and censor inappropriate language. While I have been able to manually edit the text using Chrome Dev tools, I am unsure of how to automate this process with code. I ha ...

Show a component when there is no input value, then conceal it when an input value is present

Recently, I attempted to position a paragraph absolutely in relation to an input element. My goal is to conceal the paragraph when the input has a value, and reveal it when the input is empty. Here is the code snippet I created, however, it doesn't ...

unable to utilize the library three.js

I stumbled upon an interesting animation on Codepen that uses blocks to reconstruct a map image. The necessary JavaScript files are three.min.js and TweenMax.min.js. I copied the links from Codepen and pasted them into my HTML using <script src="">&l ...

Let's explore further - delving into JSON & array manipulation using the foreach loop in Pure JavaScript

Although I have some experience with Java Script, I still consider myself a beginner in certain areas, particularly when it comes to accessing JSON objects and arrays. I've tried various syntax and options for accessing arrays using [], but so far, I ...