量子器的基本行为,其功能在块外部

时间:2017-09-29 14:12:49

标签: javascript angularjs protractor

我试图在量角器中输出一个函数,以便它在it-block之外。在"外包之前"它工作得很好。

这是我的代码:

it('should delete a private linked clone', function () {
            let countPrivateLinkedClonesBefore;
            let countPrivateLinkedClonesAfter;
            vmPortal.navigationbar.navPersonalClients.click();

            countPrivateLinkedClonesBefore = countPrivateLinkedClones();

            console.log(Count of private linked Clones before elimination: ', countPrivateLinkedClonesBefore);

            vmPortal.personalClients.firstLinkedCloneRow.element(by.css('button.btn-danger')).click();
            vmPortal.modalCreateMachine.btnModalDeleteMachine.click();

            countPrivateLinkedClonesAfter = countPrivateLinkedClones();
            console.log('Count of private linked Clones after elimination: ', countPrivateLinkedClonesBefore);
            expect(countPrivateLinkedClonesAfter === countPrivateLinkedClonesBefore - 1);
 });

所以我的功能是 countPrivateLinkedClones()

    countPrivateLinkedClones =  function () {
                let countPLC;

                vmPortal.personalClients.linkedCloneList.count().then(function (count) {
                    console.log("Count of private linked Clones:", count)
                    countPLC = count;
                });
                return countPLC;
}

如果我运行此结果是:

。消除前私有链接克隆的数量:未定义

消除后私有链​​接克隆的计数:未定义

私人链接克隆的数量:3

私人链接克隆的数量:2

这种行为的原因是什么?

如何修复它,以便在最后执行该功能而不是我想要执行的功能?

提前致谢!

2 个答案:

答案 0 :(得分:1)

看起来vmPortal.personalClients.linkedCloneList.count()是一个返回Promise的异步函数。因此,当.then()解析时,调用传递给Promise的回调函数,这保证在代码的其余部分完成之前不会发生。

特别是这意味着您无法从countPLC返回countPrivateLinkedClones,因为该值保证不会在该点设置。相反,您必须返回Promise,然后调用测试可以在Promise结算时获取值。

反过来,这意味着必须将测试编写为异步测试。

因此,让第一个函数返回一个Promise,当它可用时将解析为count值:

function countPrivateLinkedClones () {
    let countPLC;

    return vmPortal.personalClients.linkedCloneList.count().then(function (count) {
        console.log("Count of private linked Clones:", count)
        return count;
     });
}

使用传递给测试函数的done参数使测试异步,并使用Promises:

it('should delete a private linked clone', function (done) {
    let countPrivateLinkedClonesBefore;
    let countPrivateLinkedClonesAfter;
    vmPortal.navigationbar.navPersonalClients.click();

    countPrivateLinkedClones().then(count => {
        countPrivateLinkedClonesBefore = count;
        console.log('Count of private linked Clones before elimination: ', countPrivateLinkedClonesBefore);

        vmPortal.personalClients.firstLinkedCloneRow.element(by.css('button.btn-danger')).click();
        vmPortal.modalCreateMachine.btnModalDeleteMachine.click();

        return countPrivateLinkedClones();
    }).then(count => {
        couuntPrivateLinkedClonesAfter = count;
        console.log('Count of private linked Clones after elimination: ', countPrivateLinkedClonesBefore);
        expect(countPrivateLinkedClonesAfter === countPrivateLinkedClonesBefore - 1);
        done();
    });
});

答案 1 :(得分:0)

我可以看到的countPrivateLinkedClones是一个异步函数,是一个ajax请求,这意味着将在ajax返回时执行,或者调用一个promise,这意味着它将在promise被解析时执行。 。在功能结束时.. 由于您使用的是ES6,我建议使用generators,这些函数会生成一系列值,但不会像标准函数那样同时生成,但是基于每个请求。

function* generatorPrivateLink() {
            let countPrivateLinkedClonesBefore;
            let countPrivateLinkedClonesAfter;
            vmPortal.navigationbar.navPersonalClients.click();

            countPrivateLinkedClonesBefore = yield countPrivateLinkedClones();

            console.log(Count of private linked Clones before elimination: ', countPrivateLinkedClonesBefore);

            vmPortal.personalClients.firstLinkedCloneRow.element(by.css('button.btn-danger')).click();
            vmPortal.modalCreateMachine.btnModalDeleteMachine.click();

            countPrivateLinkedClonesAfter = yield ountPrivateLinkedClones();
            console.log('Count of private linked Clones after elimination: ', countPrivateLinkedClonesBefore);
            expect(countPrivateLinkedClonesAfter === countPrivateLinkedClonesBefore - 1);
 }

你可以定义一个新的生成器函数,每次调用countPrivateLinkClones()时,都可以使用'yield'关键字..