1

I wrote a test in protractor where i want to get a list of DOM elements in a reversed position.

Here is the code i wrote :

var buttons = element.all(by.js(() => {
            var arr = document.querySelectorAll('.md-table-toolbar md-icon');
            return Array.prototype.reverse.apply(arr);
        }));

But typescript throws me the following error

Argument of type '(WebDriver: WebDriver) => Promise' is not assignable to type "Locator"

What is wrong about my code ?

EDIT:

A coworker edited a file called custom.d.ts and inserted the following

declare namespace protractor {
    interface IProtractorLocatorStrategy {
        js: (script: any, ...var_args: any[]) => webdriver.Locator;
    }
}

Im fairly new to TS and programming so i have no clue what he did maybe one of you can form an answer from that information.

2 Answers 2

2

I would extend ElementArrayFinder and add a reverse() reusable method:

// for Protractor 4.0.0:
var elementLib = require('protractor/built/element');
var ElementArrayFinder = elementLib.ElementArrayFinder;

ElementArrayFinder.prototype.reverse = function() {
    var self = this;
    var getWebElements = function() {
        return self.getWebElements().then(function(parentWebElements) {
            parentWebElements.reverse();
            return parentWebElements;
        });
    };
    return new ElementArrayFinder(this.ptor_, getWebElements, this.locator_);
};

Now, you can do:

var elements = $$(".md-table-toolbar md-icon");
var reversedElements = elements.reverse();
Sign up to request clarification or add additional context in comments.

1 Comment

I edited my Question. I'll look into your solution later and mark it as accepted answer if it does solve the problem
1

You need to use only any of the element locator inside the element() method. if you want to reverse the resultant webelement you need to resolve the promise and should reverse the array value. Try the below code

var buttons = element.all(by.css('.md-table-toolbar.md-icon')).then(
  function(arrayOfWebElements)
   { 
     return arrayOfWebElements.reverse()
  })

1 Comment

Ty for the fast response but my typescript says that buttons has a type of promise I need a type of array because i cant use the "get" property on "Promise<ElementFinder[]>"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.