【问题标题】:Protractor - Page Object is not updating when the DOM elements are changed量角器 - 更改 DOM 元素时,页面对象未更新
【发布时间】:2015-09-06 02:33:37
【问题描述】:

我正在测试一个使用 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');
                });
            });
        });

查看MeetingTabPage

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

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

我需要在上传文件后以某种方式更新页面对象。我该怎么做。

【问题讨论】:

    标签: angularjs selenium jasmine protractor angularjs-e2e


    【解决方案1】:

    这就是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);
    });
    

    由于量角器修补 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中称为框架。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-15
      • 1970-01-01
      相关资源
      最近更新 更多