Opaldes
Opaldes

Reputation: 193

Protractor Error "not assignable to type "Locator" "

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.

Upvotes: 1

Views: 1393

Answers (2)

alecxe
alecxe

Reputation: 474281

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();

Upvotes: 2

Sudharsan Selvaraj
Sudharsan Selvaraj

Reputation: 4832

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()
  })

Upvotes: 1

Related Questions