1

有人可以告诉我在框架切换完成后如何继续引用 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');
            });
    });

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

4

1 回答 1

6

您的示例实际上非常接近。主要问题是它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基于 - 的链上的框架,如果您想在回调后返回父框架,则需要重置它。
于 2015-11-22T15:30:30.220 回答