简体   繁体   English

使用Webdriver从嵌套的promise中返回值

[英]Return value from nested promise with Webdriver

In my test code I wish to achieve the following: 在我的测试代码中,我希望实现以下目标:

it('Updates label text', function(done) {
   page.testLabelText();
   assert.equal(page.testLabelText().pageLabel, page.testLabelText().iFrameLabel);
   done();
});

In my page object, here is testLabelText(); 在我的页面对象中,这里是testLabelText(); :

page.testLabelText = function () {
    var pageLabel = function () {
        return driver.findElement(By.css('#element')).getText().then(function(text) {
            return text;
        });
    };
    var iFrameLabel = function () {
        return driver.findElement(By.css('#element')).getText().then(function(text) {
            return text;
        });
    };
    return {
        pageLabel: pageLabel(),
        iFrameLabel: iFrameLabel()
    };
};

But this returns 'Undefined' when printed to the console...I'm a newbie at javascript so though I've managed this in regular javascript, everything I've tried has failed with selenium WebdriverJS promises... 但是当打印到控制台时,这会返回'未定义'...我是javascript的新手,所以虽然我已经在常规javascript中管理了这个,但我尝试过的所有内容都因selenium WebdriverJS承诺而失败...

Your assert.equal() is comparing two distinct promise objects so that will never be true. 你的assert.equal()正在比较两个不同的promise对象,所以永远不会是真的。 To understand why, here's the step by step. 为了理解原因,这是一步一步的。 What you need to do is to get the values out of both the promises after they are resolved and then compare the values. 您需要做的是在解析后从promises中获取值,然后比较值。

page.testLabelText(); by itself just returns an object so calling it by itself with no assignment of the return value or referencing the return value does nothing. 它本身只返回一个对象,因此它本身调用它没有返回值的赋值或引用返回值什么都不做。

page.testLabelText().pageLabel by itself is a promise. page.testLabelText().pageLabel本身就是一个承诺。

page.testLabelText().iFrameLabel by itself is a promise. page.testLabelText().iFrameLabel本身就是一个承诺。

And, they are different promise objects so your assert.equal() will not be true. 并且,它们是不同的promise对象,因此你的assert.equal()将不是真的。

If you wanted to compare the two values from the promises, you'd have to do something like this: 如果你想比较promises中的两个值,你必须做这样的事情:

var obj = page.testLabelText();
Promise.all(obj.pageLabel, obj.iFrameLabel).then(function(results) {
    assert.equal(results[0], results[1]);
    done();
});

The solution was to use an assertion library that could resolve promises in the tests, as this would be impossible using regular async asserts. 解决方案是使用可以解决测试中的promise的断言库,因为使用常规异步断言是不可能的。 In this case I used Chai as Promised. 在这种情况下,我使用Chai作为承诺。

Requiring the following: 要求如下:

chai = require('chai'),
chaiAsPromised = require("chai-as-promised"),
should = chai.should();

and including chai.use(chaiAsPromised); 包括chai.use(chaiAsPromised); in mocha's before hook, I could then write 在钩子before mocha中,我可以写

it('Updates label text', function() {
  var label = FormsPage.testLabelText();
  label.labelHeading.should.eventually.contain(label.userInput);
});

I found a blog post on this here 我在这里发现了一篇博文

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

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