如何通过 PassByReference 传递 webdriver-io 的浏览器对象

How to transfer webdriver-io's browser Object via PassByReference

我将 webdriver-io v3.x 与 Mocha 结合使用。 为了能够在不同站点上测试相同的组件,我想将测试外包给一个额外的功能。 为此,我必须通过某种 PassByReference 将浏览器对象传输到这个额外的函数。我该怎么做?

示例代码:

// [...]

// this function shall be callable from every TestCase.
var testObject = function(browser) {
    return browser
        .getText('.InfoText')
            .then(function(txt) {
                console.log('txt: ' + txt);
                txt.should.equal('Information');
            });
});

describe('Sample Test Suite', function() {

    // go to a webpage for testing
    before(function() {
        browser
            .url('http://example.com');
    });

    // refer to the test definitions in the function testObject(browser)
    it('sample test case', function() {
        return testObject(browser);
    });

});

我可以在函数testObject中打印出对象浏览器的所有属性,所以像

这样的函数
for(var attribute in browser) {
    console.log('browsers attributes: ' + attribute);
};

将列出 API 中定义的所有(以及更多)webdriver-io 函数:

$ browsers attributes: defer
$ browsers attributes: promise
$ browsers attributes: lastPromise
$ browsers attributes: desiredCapabilities
$ [...]
$ browsers attributes: getTabIds
$ browsers attributes: getTagName
$ browsers attributes: getText
$ browsers attributes: getTitle
$ browsers attributes: getUrl
$ browsers attributes: getValue
$ browsers attributes: getViewportSize
$ browsers attributes: hold
$ browsers attributes: isEnabled
$ browsers attributes: isExisting
$ browsers attributes: isSelected
$ browsers attributes: isVisible
$ [...]

但不幸的是,我的方法 testObject 中的函数 browser.getText() 似乎永远不会到达 then 块,因为它不会在控制台上打印出任何内容。

所以我的问题是:如何将浏览器对象及其功能正确地转移到另一个方法,以便我可以在那里充分利用它?

@T.J.Crowder 帮我解决了这个问题。非常感谢!

我必须让我的测试异步。 所以解决方案代码是在testCase中加入done函数:

// refer to the test definitions in the function testObject(browser)
it('sample test case', function(done) {
    return testObject(browser)
                .then(done);
});