最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Mar 21, 2023 at 23:31 pizzaisdavid 4693 silver badges14 bronze badges asked Jun 29, 2016 at 13:19 SandyRocksSandyRocks 2975 silver badges15 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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);
});
发布评论

评论列表(0)

  1. 暂无评论