量角器 - 如何在变量中存储browser.execute脚本的值?

San*_*cks 4 javascript css testing angularjs protractor

我试图将browser.executeScript的值存储在我的块中的局部变量中但是在所有情况下我都无法将其显示为null.

到目前为止,我已尝试过很多方法

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

还有这个

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;
}
Run Code Online (Sandbox Code Playgroud)

我在我的规范中使用这个

   iit('', function() {        

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

            returnColor();


        });
Run Code Online (Sandbox Code Playgroud)

真的会告诉我有人可以告诉我如何正确地做到这一点吗?

ale*_*cxe 9

你需要return从脚本中获得一个:

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

请注意,您还可以通过getCssValue()以下方法解决相同的问题:

var elm = element(by.id("txtName"));
elm.getCssValue("border-left-color").then(function (color) {
    console.log(color);
});
Run Code Online (Sandbox Code Playgroud)