我正在测试一个用 angular.js 构建的 SPA,我使用页面对象模式来编写我的测试。在应用程序中,我们有许多将更新的列表。例如,有一个附件列表会在添加/删除附件时更新。要添加附件,我们有一个模态窗口,当我们上传文件并单击确定时。文件上传和列表更新。

我写了 2 个页面对象,一个用于上传模式窗口,另一个用于附件列表的预览。在我的测试中,我首先获取附件的当前计数,然后单击一个按钮以激活模式窗口并附加文件。然后我在预览页面中再次计数附件并将其比较以增加 1。但测试失败。页面对象未更新,但仍将附件计数显示为 2。

测试

it('Should attach a file when a file is selected and OK button is pressed.', function () {
            var currentFileCount = viewMeetingTabPage.getMeetingAttachmentCount();

            viewMeetingPage.clickAddAttachmentButton();
            addAttchmentPage.attachFile();
            addAttchmentPage.clickConfimAttachFileButton();

            currentFileCount.then(function (curCount) {
                viewMeetingTabPage.getMeetingAttachmentCount().then(function (newCount) {
                    expect(newCount).toBe(curCount + 1);
                    //expect(viewMeetingTabPage.getMeetingAttachmentName()).toBe('test-file.pdf');
                });
            });
        });

ViewMeetingTabPage
this.getMeetingAttchments = function () {
        return element.all(by.repeater('attachment in meeting.AttachmentViewModelList track by $index'));
    };

this.getMeetingAttachmentCount = function () {
        return this.getMeetingAttchments().count();
    };

我需要的是在上传文件后以某种方式更新页面对象。我怎样才能做到这一点。

最佳答案

这就是 control-flow 的工作原理。执行测试代码将一堆 promise 排队。它们按照添加到流中的顺序得到解决,而它们中的每一个都在等待前一个完成。

it('Should attach a file when a file is selected and OK button is pressed.', function () {
        # Queues the count promise
        var currentFileCount = viewMeetingTabPage.getMeetingAttachmentCount();

        # Queues some other promises that would start
        # to get executed once the one above finishes
        viewMeetingPage.clickAddAttachmentButton();
        addAttchmentPage.attachFile();
        addAttchmentPage.clickConfimAttachFileButton();

        # This piece of code branches-off the control-flow
        # and gets executed immediately after currentFileCount is resolved
        # i.e. before the clickAddAttachmentButton
        currentFileCount.then(function (curCount) {
            # That's why newCount equals curCount,
            # they are counting the same number of elements
            # since nothing changed in the meantime
            viewMeetingTabPage.getMeetingAttachmentCount().then(function (newCount) {
                expect(newCount).toBe(curCount + 1);
                //expect(viewMeetingTabPage.getMeetingAttachmentName()).toBe('test-file.pdf');
            });
        });
    });
currentFileCount 可以被视为测试的设置阶段,因此您可以将其提取到 beforeEach 块中:
var initialFileCount;
beforeEach(function() {
    viewMeetingTabPage.getMeetingAttachmentCount().then(function(count) {
        initialFileCount = count;
    });
});

it('Should attach a file when a file is selected and OK button is pressed.', function () {
    viewMeetingPage.clickAddAttachmentButton();
    addAttchmentPage.attachFile();
    addAttchmentPage.clickConfimAttachFileButton();
    expect(viewMeetingTabPage.getMeetingAttachmentCount()).toBe(initialFileCount + 1);
});

由于 Protractor 修补 jasmine 以在测试块之间等待控制流清空,因此这可能会起作用。

请记住, expect 也已修补以处理 promise ,因此您无需将其放在 then 中。

更新:

实际上,您不应该需要上面的 beforeEach,它也应该这样工作:
var initialFileCount;

it('Should attach a file when a file is selected and OK button is pressed.', function () {
    viewMeetingTabPage.getMeetingAttachmentCount().then(function(count) {
        initialFileCount = count;
    });
    viewMeetingPage.clickAddAttachmentButton();
    addAttchmentPage.attachFile();
    addAttchmentPage.clickConfimAttachFileButton();
    expect(viewMeetingTabPage.getMeetingAttachmentCount()).toBe(initialFileCount + 1);
});

它在 WebDriverJS User’s Guide 中称为框架。

关于angularjs - Protractor - 更改 DOM 元素时页面对象未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30949492/

10-12 03:04
查看更多