下面有两个地方,代码(在Javascript中)永远不会执行,尽管它看起来应该执行。请看下面的代码,注释是“这个代码永远不会被执行”。
我碰巧在一个动态生成in的环境中。我创建了两个文件'a.html‘和'b.html’,最终作为iframe添加到页面(index.html)。我想遍历iframe,获取每个iframe的ID,然后使用该ID获取每个iframe的内容以进行测试。
如何使用Javascript测试框架实习生完成此操作?
文件: 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;
}
});
});发布于 2015-12-12 06:14:58
当我创建了对该命令的引用或实例化了一个新命令时,我有点惊讶,输出崩溃了--请看下面显示"// * it new work here *“的地方。当你看到这样的评论:"//一切都能正常工作“,我觉得我很幸运能偶然发现这个解决方案。
/**
*
*/
define([
'intern!object',
'intern/chai!assert'
], function (registerSuite, assert) {
registerSuite({
name: 'is this working?',
'Check Page body': function()
{
var command = this.remote;
return this.remote.get(require.toUrl("http://localhost:63342/InternFrames/html/index.html"))
.findAllByTagName("iframe")
.then(function(iframes, context1)
{
console.log("iframes size: ", iframes.length);
return Promise.all(iframes.map(function(element, index, array)
{
// everything works correctly
command = command
// if you skip the executing line below
// the second iteration through
// the loop won't work.
.switchToParentFrame()
.switchToFrame(element)
.findByTagName('body')
.getVisibleText()
.then(function(bodyText, context)
{
console.log("this is the -->body: ", bodyText );
console.log("it's working");
})
.end();
return command;
}));
})
.end();
},
'can\'t reassign to var': function()
{
var command = this.remote;
return this.remote.get(require.toUrl("http://localhost:63342/InternFrames/html/index.html"))
.findAllByTagName("iframe")
.then(function(iframes, context1)
{
console.log("iframes size: ", iframes.length);
return Promise.all(iframes.map(function(element, index, array)
{
// ***** it doesn't work here ***********
var commandReponse = command
.switchToParentFrame()
.switchToFrame(element)
.findByTagName('body')
.getVisibleText()
.then(function(bodyText, context)
{
console.log("this is the -->body: ", bodyText );
console.log("it's working");
})
.end();
return commandReponse;
}));
})
.end();
},
'can\'t create new instance': function()
{
var command = this.remote;
return this.remote.get(require.toUrl("http://localhost:63342/InternFrames/html/index.html"))
.findAllByTagName("iframe")
.then(function(iframes, context1)
{
console.log("iframes size: ", iframes.length);
return Promise.all(iframes.map(function(element, index, array)
{
// ***** it doesn't work here ***********
command =new command.constructor(command.session)
.switchToParentFrame()
.switchToFrame(element)
.findByTagName('body')
.getVisibleText()
.then(function(bodyText, context)
{
console.log("this is the -->body: ", bodyText );
console.log("it's working");
})
.end();
return command;
}));
})
.end();
},
'simply returning won\'t work either': function()
{
var command = this.remote;
return this.remote.get(require.toUrl("http://localhost:63342/InternFrames/html/index.html"))
.findAllByTagName("iframe")
.then(function(iframes, context1)
{
console.log("iframes size: ", iframes.length);
return Promise.all(iframes.map(function(element, index, array)
{
// ***** it doesn't work here ***********
return command
// if you skip the executing line below
// the second iteration through
// the loop won't work.
.switchToParentFrame()
.switchToFrame(element)
.findByTagName('body')
.getVisibleText()
.then(function(bodyText, context)
{
console.log("this is the -->body: ", bodyText );
console.log("it's working");
})
.end();
return command;
}));
})
.end();
}
});
});https://stackoverflow.com/questions/33876535
复制相似问题