Javascript 使用promise通过外部函数从DOM检索属性

Javascript 使用promise通过外部函数从DOM检索属性,javascript,es6-promise,Javascript,Es6 Promise,我对Gragrator/jasmine,尤其是javascript,在编写前端测试自动化方面有些陌生。 我在DOM中有一个对象,我正试图检索和计算它。它本质上是一个列高度值。我需要在一个独立于测试脚本的类中的函数中对此进行求值 测试脚本调用如下所示(“选择器”是预定义的且有效的): html DOM如下所示: class="bar" fill-opacity="0.000001" x="123.02739726027396" width="6" height="10.8" y="15.8"

我对Gragrator/jasmine,尤其是javascript,在编写前端测试自动化方面有些陌生。 我在DOM中有一个对象,我正试图检索和计算它。它本质上是一个列高度值。我需要在一个独立于测试脚本的类中的函数中对此进行求值

测试脚本调用如下所示(“选择器”是预定义的且有效的):

html DOM如下所示:

class="bar" fill-opacity="0.000001" x="123.02739726027396" width="6" height="10.8" y="15.8" 
我已经设置了功能,但我认为我在承诺概念上绊倒了;我不断收到未定义的返回。 我只想返回高度值。
下面是我的函数:

this.getColumnHeight = function(selector) {

var tmpSelector = element(by.css(selector));

  var found = new Promise(function(resolve, reject) {
    var look = function() {
      tmpSelector.getAttribute("height").then(function(text) { 
        var h = text;
        console.log("h = " + h);
        return h; 
      })
    }
  var value = look();
  return value;
  })
}
该行:

console.log("h = " + h);
正确返回值。
我不知道如何从look()函数中得到“h”并返回到我的原始测试脚本中。 我在哪里跌倒?非常感谢任何指点

编辑(2016年5月9日):

不清楚如何从then()块将值传递回测试脚本。我当前的代码为承诺返回一个{pending}。 非常感谢

调用(tests.js):

函数(lib.js):


根据您的评论,getAttribue似乎是一个承诺,您希望从承诺中获得高度值

//Pretend getAttribute function
function getAttribute(selector) {

  return new Promise( function(resolve,reject) {

    var height = 114.5;

    setTimeout( function(){  resolve(height);  }, 1000 );
    //After 1 second resolve promise and pass height to .then() function
  })
}

var A = "I get printed before .then()"

getAttribute("#myTable").then( function(valueFromResolve) {
   A = valueFromResolve;
   console.log(A);
})
console.log(A); //This will run before console.log(A) inside .then()
                // even if there is no time delay in setTimeout()
.then()
中的任何函数都将处于挂起状态,直到运行所有其他同步代码

在您的代码中,
returnh
将在
返回值之后返回。在执行
返回值时,h未定义

console.log(“h=”+h)
之所以有效,是因为它是在运行var
var h=text
时(在运行所有同步代码之后)在
.then()
函数中调用的

编辑:实际上不是在运行所有同步代码之后,而是在函数堆栈为空之前。函数
look
将占用堆栈,而
中的任何内容都将被占用。然后()
将等待堆栈为空


关键是要记住,
中的代码。然后()
在承诺解决(或被拒绝)时执行,而不是按照我们习惯的自上而下的顺序执行。

根据您的评论,getAttribue似乎是一个承诺,您希望从承诺中获得高度值

//Pretend getAttribute function
function getAttribute(selector) {

  return new Promise( function(resolve,reject) {

    var height = 114.5;

    setTimeout( function(){  resolve(height);  }, 1000 );
    //After 1 second resolve promise and pass height to .then() function
  })
}

var A = "I get printed before .then()"

getAttribute("#myTable").then( function(valueFromResolve) {
   A = valueFromResolve;
   console.log(A);
})
console.log(A); //This will run before console.log(A) inside .then()
                // even if there is no time delay in setTimeout()
.then()
中的任何函数都将处于挂起状态,直到运行所有其他同步代码

在您的代码中,
returnh
将在
返回值之后返回。在执行
返回值时,h未定义

console.log(“h=”+h)
之所以有效,是因为它是在运行var
var h=text
时(在运行所有同步代码之后)在
.then()
函数中调用的

编辑:实际上不是在运行所有同步代码之后,而是在函数堆栈为空之前。函数
look
将占用堆栈,而
中的任何内容都将被占用。然后()
将等待堆栈为空


关键是要记住,
.then()
中的代码是在承诺解决(或被拒绝)时执行的,而不是像我们习惯的那样,按照从上到下的顺序执行。

不清楚为什么需要使用承诺。我非常愿意接受建议。getAttribute函数是基于承诺的。如果有更好的解决方案,我很乐意听到。不清楚你为什么需要使用承诺。我非常愿意接受建议。getAttribute函数是基于承诺的。如果有更好的解决办法,我很乐意听到。谢谢,约翰。亚伯拉罕。我仍然无法理解如何将值从then()块传递回测试脚本。我编辑了上面的帖子,以显示我当前的代码。提前感谢。@bmagstadt您想知道新值的任何代码都必须从.then()内部调用,或者从承诺链的下一个.then()调用。在.then()之前运行的代码不会知道.then()返回什么。谢谢,john.abraham。我仍然无法理解如何将值从then()块传递回测试脚本。我编辑了上面的帖子,以显示我当前的代码。提前感谢。@bmagstadt您想知道新值的任何代码都必须从.then()内部调用,或者从承诺链的下一个.then()调用。在.then()之前运行的代码不会知道.then()返回什么。
this.getColumnHeight = function(selector) {
    var tmpSelector = element(by.css(selector));

    return new Promise(
        function(resolve,reject) {
        var height = 0;
        setTimeout( function(){  resolve(height);  }, 2000 );
    })

    tmpSelector.getAttribute("height").then( function(valueFromResolve) {
    A = valueFromResolve;
    console.log(A);
    return A;
    })
}
//Pretend getAttribute function
function getAttribute(selector) {

  return new Promise( function(resolve,reject) {

    var height = 114.5;

    setTimeout( function(){  resolve(height);  }, 1000 );
    //After 1 second resolve promise and pass height to .then() function
  })
}

var A = "I get printed before .then()"

getAttribute("#myTable").then( function(valueFromResolve) {
   A = valueFromResolve;
   console.log(A);
})
console.log(A); //This will run before console.log(A) inside .then()
                // even if there is no time delay in setTimeout()