我不熟悉使用 Intern JS 编写测试,并且一直在关注他们的文档以使用 Object interface和 Page objects , 特别。根据文档,页面对象背后的想法是封装特定页面的 DOM,以防标记发生变化。
在尝试遵循页面对象模式时,我的目标是在 myPage
上有一个方法,它使用 Promise.all()
来查找几个 DOM 元素,然后将这些返回到实际的测试函数。
我大致基于 documentation for leadfood/Command 顶部附近的 Promise.all()
示例.我怀疑我在调用 testOneStuff()
时将我想要返回的内容返回到 test 1
函数的链接错误......但是因为我看不到在链中包含 Promise.all()
的方法,我发现自己不知所措。
当我尝试运行这个测试时,它似乎可以启动 function (unitWrapper)
(我有一个 console.log 在那里),然后几秒钟后它失败并显示 CancelError:达到超时...
。
这个想法有可能吗?我也意识到我可能会以一种不寻常的方式来处理这个问题,因为除了他们文档中的一些基本示例之外,我不熟悉 Intern JS 中的典型模式。
以下是我正在做的相关部分:
在定义测试的文件中:
define([
'intern!object',
'intern/chai!assert',
'../support/pages/MyPage'
], function (registerSuite, assert, MyPage) {
registerSuite(function () {
var myPage;
return {
name: 'my_test',
setup: function () {
myPage = new MyPage(this.remote);
},
'test 1': function () {
return myPage
.testOneStuff()
.then(function (elements) {
// here I would like 'elements' to be the object I'm
// returning in function passed into Promise.all().then().
});
}
};
});
});
在定义 MyPage 的文件中:
define(function (require) {
// Constructor to exec at runtime
function MyPage(remote) {
this.remote = remote;
}
MyPage.prototype = {
constructor: MyPage,
testOneStuff: function () {
return this.remote
.findByCssSelector('.unit-question')
.then(function (unitWrapper) {
// Note that 'this' is the command object.
return Promise.all([
// Note we've left the 'find' context with .unit-question as parent
this.findByCssSelector('ul.question-numbers li.current').getVisibleText(),
this.findAllByCssSelector('ul.answers li a.answer'),
this.findByCssSelector('a.action-submit-answer'),
this.findByCssSelector('a.action-advance-assessment')
]).then(function (results) {
return {
currentQuestionNumber: results[0],
answerLinks: results[1],
btnSubmit: results[2],
btnNext: results[3]
};
});
});
}
};
return MyPage;
});
最佳答案
挂起可能是由于使用 this
在 then
回调中启动命令链。从命令 then
回调返回 this
或从 this
开始的命令链将使链死锁。相反,在回调中使用 this.parent
,例如:
return Promise.all([
this.parent.findByCssSelector('...'),
this.parent.findAllByCssSelector('...'),
this.parent.findByCssSelector('...'),
this.parent.findByCssSelector('...')
]).then(//...
关于javascript - Intern JS - 如何在链式命令方法中使用 Promise.all()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34187230/