如何在InternetJS中切换帧后继续

How to proceed AFTER switching frames in InternJS

本文关键字:继续 InternetJS      更新时间:2023-09-26

有人能告诉我在帧切换完成后如何继续引用iframe中的元素吗?我已经看过如何切换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');
            });
    });

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

您的示例实际上非常接近。主要的问题是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的链上更改帧,如果您想在回调后返回到父帧,则需要重置它