首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Intern/Leadfoot迭代元素

使用Intern/Leadfoot迭代元素
EN

Stack Overflow用户
提问于 2015-11-24 01:01:50
回答 1查看 400关注 0票数 0

下面有两个地方,代码(在Javascript中)永远不会执行,尽管它看起来应该执行。请看下面的代码,注释是“这个代码永远不会被执行”。

我碰巧在一个动态生成in的环境中。我创建了两个文件'a.html‘和'b.html’,最终作为iframe添加到页面(index.html)。我想遍历iframe,获取每个iframe的ID,然后使用该ID获取每个iframe的内容以进行测试。

如何使用Javascript测试框架实习生完成此操作?

文件: a.html

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>A Title</title>
</head>
<body>
Got A here
</body>
</html>

文件: b.html

代码语言:javascript
复制
<!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

代码语言:javascript
复制
<!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

代码语言:javascript
复制
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;
        }
    });
});
EN

回答 1

Stack Overflow用户

发布于 2015-12-12 06:14:58

当我创建了对该命令的引用或实例化了一个新命令时,我有点惊讶,输出崩溃了--请看下面显示"// * it new work here *“的地方。当你看到这样的评论:"//一切都能正常工作“,我觉得我很幸运能偶然发现这个解决方案。

代码语言:javascript
复制
/**
 * 
 */
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();
    }
    });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33876535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档