我正在测试使用angular.js构建的SPA,并使用Page Objects模式来编写我的测试。在应用程序中,我们有许多将更新的列表。例如,有一个附件列表将在添加/删除附件时更新。要添加附件,我们有一个模态窗口,当我们上传文件并单击确定时。文件上传和列表更新。
我写了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();
};
我需要的是在上传文件后以某种方式更新页面对象。我怎么能这样做。
答案 0 :(得分:2)
这就是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中称为框架。