2015-06-20 51 views
3

我正在测试使用页面对象模式编写测试的angular.js和im构建的SPA。在应用程序中,我们有许多将被更新的列表。例如,有附件列表会在附件添加/删除时更新。要添加附件,我们有一个模式窗口,当我们上传文件并点击确定。文件上传和列表更新。当DOM元素发生变化时,量角器 - 页面对象未更新

我写了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(); 
    }; 

我需要什么已经是莫名其妙地更新页面我上传文件后的对象。我怎样才能做到这一点。

回答

2

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

由于量角器补丁茉莉到测试块用于控制流之间的等待清空,这可能会奏效。

记住expect也被修补以处理承诺,因此您不需要将其放置在then中。

UPDATE:

其实,你不应该需要高于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

相关问题