这就是如何控制流 https://github.com/angular/protractor/blob/master/docs/control-flow.md作品。执行测试代码会排队一堆承诺。它们按照添加到流程中的顺序得到解决,同时每个都等待前一个完成。
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
block:
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
还进行了修补以处理承诺,因此您无需将其放在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 用户指南 https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs#framing.