量角器 - DOM 元素更改时页面对象未更新

Protractor - Page Object is not updating when the DOM elements are changed

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

我写了 2 个页面对象,一个用于上传模式 window,另一个用于附件列表的预览。在我的测试中,我首先获取附件的当前计数,然后单击一个按钮以激活模式 window 并附加文件。然后我在预览页面中再次计算附件并将其比较以增加 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 的工作原理。执行测试队列的代码会产生一堆承诺。它们按照添加到流中的顺序得到解决,同时它们中的每一个都等待前一个完成。

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);
});

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

请记住 expect 也已修补以处理承诺,因此您无需将其放在 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.

中叫做framing