【问题标题】:How to proceed AFTER switching frames in InternJS如何在 InternJS 中切换帧后继续
【发布时间】:2015-11-19 00:04:38
【问题描述】:

有人能告诉我在框架切换完成后如何继续引用 iframe 中的元素吗?我已经查看了How to switch iframes InternJS 中提出的解决方案,但无济于事,intern Functional Testing with Frames 中的信息还不适用。以下脚本返回Cannot read property 'apply' of undefined type: TypeError 的错误:

return Remote
    .findAllByTagName('iframe')
    .then(function (frames) {
        return new Remote.constructor(Remote.session)
            .switchToFrame(frames[0])
            .getProperty('title')
            .then(function (result) {
                expect(result).to.equal('Rich text editor, rtDescAttach');
            });
    });

我可以看到脚本失败的唯一原因是框架的位置不正确。页面上有两个,我需要第一个。完成此操作后,我真的很想将对框架的引用放在页面对象中(这是我认为它所属的地方),但我必须能够首先成功找到它,所以不要本末倒置。非常感谢您的建议和帮助。

【问题讨论】:

    标签: javascript iframe functional-testing intern leadfoot


    【解决方案1】:

    您的示例实际上非常接近。主要问题是getProperty('title') 无法以它的使用方式工作。 getProperty 是一个元素方法,在您调用它时,您在上下文堆栈中没有有效元素。假设您正在尝试获取 iframe 页面的标题,则需要使用 execute 回调,例如:

    .switchToFrame(frames[0])
    .execute(function () {
        return document.title;
    })
    .then(function (title) {
        // assert
    })
    

    Leadfoot 有一个getPageTitle 回调,但它总是返回顶级文档的标题(标题在浏览器标题栏或标签中的那个)。

    另一个小问题是,在回调中访问远程的更规范方法是通过parent 属性,例如:

    .then(function (frames) {
        return this.parent
            .switchToFrame(frames[0])
            // ...
    })
    

    如果你想访问 iframe 中的元素,你需要切换框架,重置搜索上下文,然后找到元素,例如:

    .findAllByTagName('iframe')
    .then(function (frames) {
        return this.parent
            // clear the search context in this callback
            .end(Infinity)
            // switch to the first frame
            .switchToFrame(frames[0])
            // find an element in the frame, examine its text content
            .findById('foo')
            .getVisibleText()
            .then(function (text) {
                assert.equal(text, 'expected content');
            })
            // switch back to the parent frame when finished
            .switchToParentFrame()
    })
    // continue testing in parent frame
    

    需要注意的几点:

    1. 搜索上下文在命令链中是本地的,因此基于this.parent 的命令链上的更改不会保留在父命令链上。基本上,不需要在回调的命令链末尾调用.end()
    2. 活动框架不是命令链本地的,因此如果您更改基于 this.parent 的链上的框架,如果您想返回到回调后的父框架。

    【讨论】:

    • 天啊,你简直让我开心!明天早上,当它允许我的时候,我会首先奖励你赏金。你有我永远的感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多