Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor - How to store the value of browser.executeScript in variable?

I am trying to store the value of browser.executeScript inside a local variable in my it block but I am not able to do so in all the cases it displays null.

I have tried many ways so far

     browser.executeScript('$("#txtName").css("border-left-color");').then(function (color) {
        console.log("This is color" + color);
    });

Also this

function returnColor()
{
     var  a = browser.executeScript('$("#txtName").css("border-left-color");');
     return a;
}

function getColorCode()
{
       var a = returnColor().then(function(list){
           console.log("Output is ***************" + list);
             return list;
      });

        return a;
}

I am using this inside my spec as

   iit('', function() {        

             browser.executeScript('$("#txtName").css("border-left-color");').then(function (color) {
                console.log("This is color" + color);
            });

            returnColor();


        });

Will really appreaciate it someone can tell me how to do it properly?

like image 874
SandyRocks Avatar asked Jan 06 '23 19:01

SandyRocks


1 Answers

You need to have a return from the script:

function returnColor()
{
    return browser.executeScript('return $("#txtName").css("border-left-color");');
}

Note that you can also solve the same problem via getCssValue():

var elm = element(by.id("txtName"));
elm.getCssValue("border-left-color").then(function (color) {
    console.log(color);
});
like image 75
alecxe Avatar answered Jan 30 '23 11:01

alecxe