下面有 2 个地方的代码(在 Javascript 中)从未被执行,尽管它看起来好像应该执行。请参阅下面的代码,评论说“此代码永远不会执行”。
我碰巧处于动态生成 ID 的环境中。我创建了 2 个文件 'a.html' 和 'b.html' 最终作为 iframe 添加到页面 (index.html)。我想遍历 iframe,获取每个 iframe 的 ID,然后使用 ID 获取每个 iframe 的内容进行测试。
如何使用 Javascript 测试框架 Intern 做到这一点?
文件:a.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>A Title</title>
</head>
<body>
Got A here
</body>
</html>
文件:b.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>B Title</title>
</head>
<body>
Got B here
</body>
</html>
我创建了一个文件 index.html 来加载上面的文件...文件:index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Main</title>
</head>
<body>
<iframe id="a1" width="100" height="100" src="a.html"></iframe>
<iframe id="b1" width="100" height="100" src="b.html"></iframe>
</body>
</html>
...并运行以下实习生测试文件:testFrames.js
define([
'intern!object',
'intern/chai!assert'
], function (registerSuite, assert) {
registerSuite({
name: 'is this working?',
'Iterating through frames': function () {
var command = this.remote;
command = command.get(require.toUrl( "html/index.html"))
.findAllByTagName("iframe")
.then(function(iframes)
{
assert( iframes.length== 2);
for (var i=0;i<iframes.length;i++)
{
var frame = iframes[i];
console.log("got frame id: ", frame.elementId);
frame.getAttribute("id").then(function(id)
{
// an elementId is not the same as an attribute id of an element --- ARGH!!!
console.log("got id: ", id);
command = command
.findById(id)
.switchToFrame(id)
.getPageTitle()
.then(function(text)
{
// THIS CODE NEVER GETS EXECUTED
console.log("something should be here: ");
console.log("text value: ", text);
// make assertions here to verify all is well.
})
.end();
return command;
})
}
})
.end()
;
return command;
}
});
});