简体   繁体   English

量角器用getText()进行诺言解析

[英]Protractor promise resolution with getText()

I call the getText() function like so and then try to resolve the promise but cant get the string value later. 我这样调用getText()函数,然后尝试解析Promise,但以后无法获取字符串值。

var textFromElement = someElement.getText().then(function(text){return text})
var splittedText = textFromElement.split(" ")

How can I get the text for later use? 如何获取文本以备后用?

What you don't understand is how the javascript compiler deals with promises. 您不了解的是javascript编译器如何处理promise。

This is how the compiler looks at your code; 这就是编译器查看代码的方式。

var textFromElement = someElement.getText().then(function(text){return text})
var splittedText = textFromElement.split(" ")

1 - All variable are created at the top of the function scope regardless of where you assign it. 1-所有变量都在函数作用域的顶部创建,无论您在何处分配变量。

var textFromElement; (= undefined)
var splittedText; (= undefined)

2 - Does the minimum amount of work it can get away with for each line and moves to the next line. 2-对每一行进行最少的工作,然后移至下一行。

testFromElement = {promise element object};
splittedText = {promise element object}.split(" "); (= undefined)//This what you don't want.

3 - Starts at the top and does more minimal work on unresolved lines. 3-从顶部开始,在未解析的行上执行最少的工作。

testFromElement = {promise getText object};

4 - Starts at the top and does more minimal work on unresolved lines. 4-从顶部开始,在未解决的行上执行最少的工作。

testFromElement = "text text";

In short it assigns splittedText three step before you want it to. 简而言之,它在您需要之前将splittedText分配了三步。

Good example: 好例子:

var splittedText;
it("should get split text", function(done) {
  someElement.getText().then(function(textFromElement){
    splittedText = textFromElement.split(" ");
    done();
  })
})

1 - All variable are created at the top of the function scope regardless of where you assign it. 1-所有变量都在函数作用域的顶部创建,无论您在何处分配变量。

var splittedText; (= undefined)

2 - Only work is done inside this function until done() is called 2-仅在此函数内完成工作,直到调用done()

it("should get split text", function(done) {

3 - Does the minimum amount of work it can get away with for each line and moves to the next line. 3-对每一行进行最少的工作,然后移至下一行。

someElement = {promise element object};

4 - Starts at the top of the function and does more minimal work on unresolved lines. 4-从函数的顶部开始,在未解析的行上执行的工作量最少。

someElement.getText() = {promise getText object};

5 - Starts at the top of the function and does more minimal work on unresolved lines. 5-从函数的顶部开始,在未解析的行上执行的工作量最少。

textFromElement = "text text";
splittedText = textFromElement.split(" "); (["text","text"]);
done();  //the compiler can now to work outside this function

Your problem is that you are assigning the resolution of .then() method to your variable, not callback which you are providing as a parameter to it. 您的问题是,您正在为变量分配.then()方法的分辨率,而不是为其提供参数的回调。

All in all protractor/jasmine comes with mechanism for asyncronous tests. 总而言之,量角器/茉莉都带有异步测试机制。

Here you got ES6 example. 在这里,您获得了ES6示例。

it('some description', (done) => {
    someElement.getText().then(text => {
        var splittedText = text.split(" ");
        done();
    });
});

And ES5: 和ES5:

it('some description', function(done) => {
    someElement.getText().then(function(text){
        var splittedText = text.split(" ");
    }).finally(done);
});

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

相关问题 量角器getText永不解析 - Protractor getText never resolves 在 Protractor 中调用 getText() 时,ElementFinder 正在记录 - ElementFinder is logging when calling getText() in Protractor 量角器在承诺后找不到相同的元素 - Protractor not finding the same element after a promise Angular 2(cli)量角器jasmine期望不能解决承诺 - Angular 2 (cli) protractor jasmine expect is not resolving promise ngOnInit() 中承诺解析的 Angular fakeAsync 测试 - Angular fakeAsync test of promise resolution in ngOnInit() e2e Angular 2,带有量角器,element(by.id('count'))。getText()显示为空 - e2e Angular 2 with protractor, element(by.id('count')).getText() showing null 当我解决它并改变页面(量角器测试)时,保证继续工作 - promise keep working when i resolve it and change page (protractor test) 量角器无法从executescript中获取价值,它会返回承诺 - Protractor can't get value from executescript it returns a promise Angular 6 量角器为字符串返回未定义,即使它返回承诺 - Angular 6 protractor returns undefined for string even it return promise 为什么我不能在量角器中使用:“await .getAttribute()”,即使它返回一个承诺? - Why can't I use: "await .getAttribute()" in Protractor, even though it returns a promise?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM