2

Hi I am new to protractor, I have below two functions, the first function is returning a promise, which I want to use in 2nd function to retrieve value from it and use it.

getColumnNumber(columnName) {
        this.eh.waitForElement(this.GridsEleMap.get("HeaderTableHeaderRow"));
        var colEle = this.GridsEleMap.get("HeaderTableHeaderRow").all(by.xpath(".//td//div[contains(@class,'text-content')]"));
        return colEle.getText().then(function (text) {
            var actIndex = text.indexOf(columnName) + 1;
            logger.info("Column Index:" + actIndex);
        });
    }

clickRowElementUsingRowTextAndColumnName(rowText, columnName) {
    var ele = this.GridsEleMap.get("BodyTable");
    return this.getColumnNumber(columnName).then(function (result) {
        logger.info("Text:" + result);
        var cellEle = ele.all(by.xpath(".//tr//td[" + result + "]//div[@class='virtualLink']"));
        logger.info("Result:" + cellEle);
        return cellEle.filter(function (elem) {
            browser.actions().mouseMove(elem).perform();
            browser.sleep(50);
            return elem.getText().then(function (text) {
                return text.trim() === rowText.trim();
            });
        }).each(function (element) {
            browser.actions().mouseMove(element).perform();
            element.click();
            browser.sleep(10*1000);
        });
    });

Whenever I am trying to use "result" object of then function applied on first function in clickRowElementUsingRowTextAndColumnName, its value is coming as undefined. Please help me with it.

I have to pass this result value to form a xpath of particular column index and perform operation on it.

1
  • You should return the value return (text.indexOf(columnName) + 1) from colEle.getText() promise. Commented Dec 28, 2017 at 11:22

1 Answer 1

2

You should return properly the value from the first function. You can try the following code for example:

getColumnNumber(columnName) {
        ...
        return colEle.getText().then(function (text) {
            return text.indexOf(columnName) + 1;
        });
}

If you see, it returns the actIndex.

Pay also attention that you have few code not chained properly, all protractor methods return promises which need to be chained in order to be sure to keep the flow sync.

Then, just as suggestion, try to avoid the use of xpath locators. They are unreadable and they bring to a decrease of performances.

Sign up to request clarification or add additional context in comments.

Comments

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.