0

下面有 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;
        }
    });
});
4

1 回答 1

0

当我创建对该命令的引用或实例化一个新命令时,我有点惊讶,输出崩溃了——看看它在哪里说“// ***** 它在这里不起作用 ******* ****“ 以下。在您找到评论“//一切正常”的地方,我觉得我很幸运偶然发现了这个解决方案。

/**
 * 
 */
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();
    }
    });
});
于 2015-12-11T22:14:58.170 回答