Javascript 在InternJS中切换帧后如何继续

Javascript 在InternJS中切换帧后如何继续,javascript,iframe,functional-testing,intern,leadfoot,Javascript,Iframe,Functional Testing,Intern,Leadfoot,有人能告诉我在帧切换完成后如何继续引用iframe中的元素吗?我已经查看了中提供的解决方案,但没有效果,中的信息只是不适用(尚未)。以下脚本返回一个错误无法读取未定义类型的属性“apply”:TypeError: return Remote .findAllByTagName('iframe') .then(function (frames) { return new Remote.constructor(Remote.session) .

有人能告诉我在帧切换完成后如何继续引用iframe中的元素吗?我已经查看了中提供的解决方案,但没有效果,中的信息只是不适用(尚未)。以下脚本返回一个错误
无法读取未定义类型的属性“apply”: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
有几件事需要注意:

  • 搜索上下文是命令链的本地上下文,因此基于父级的命令链上的更改不会在父级命令链上持久存在。基本上,不需要在回调中的命令链末尾调用
    .end()
  • 活动帧不是命令链的本地帧,因此如果在基于
    此.parent
    的链上更改帧,则如果要在回调后返回到父帧,则需要将其重置

  • 你的例子实际上非常接近。主要问题是
    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
    
    有几件事需要注意:

  • 搜索上下文是命令链的本地上下文,因此基于父级的命令链上的更改不会在父级命令链上持久存在。基本上,不需要在回调中的命令链末尾调用
    .end()
  • 活动帧不是命令链的本地帧,因此如果在基于
    此.parent
    的链上更改帧,则如果要在回调后返回到父帧,则需要将其重置

  • 天哪,你真让我开心!明天一早我会给你赏金,等它允许我的时候。我永远感谢你!天哪,你真让我开心!明天一早我会给你赏金,等它允许我的时候。我永远感谢你!