简体   繁体   English

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

[英]Protractor - How to store the value of browser.execute script in varriable?

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. 我试图将browser.executeScript的值存储在我的块中的局部变量中但是在所有情况下我都无法将其显示为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? 真的会告诉我有人可以告诉我如何正确地做到这一点吗?

You need to have a return from the script: 您需要从脚本return

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

Note that you can also solve the same problem via getCssValue() : 请注意,您还可以通过getCssValue()解决相同的问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM