如何在InternJS中切换帧后继续

MBi*_*ski 1 javascript iframe functional-testing intern leadfoot

有人能告诉我如何在完成帧切换后继续引用iframe中的元素吗?我已经看过如何切换iframes InternJS中提供的解决方案无效,而实用的功能测试框架中的信息是不适用的(还是.)以下脚本返回错误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');
            });
    });
Run Code Online (Sandbox Code Playgroud)

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

jas*_*x43 6

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

.switchToFrame(frames[0])
.execute(function () {
    return document.title;
})
.then(function (title) {
    // assert
})
Run Code Online (Sandbox Code Playgroud)

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

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

.then(function (frames) {
    return this.parent
        .switchToFrame(frames[0])
        // ...
})
Run Code Online (Sandbox Code Playgroud)

如果要访问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
Run Code Online (Sandbox Code Playgroud)

有几点需要注意:

  1. 搜索上下文是命令链的本地,因此this.parent基于命令链的更改不会在父命令链上保留.基本上,没有必要.end()在回调中的命令链末尾调用.
  2. 活动帧不是命令链的本地帧,因此如果更改this.parent基于链的帧,如果要在回调后返回父帧,则需要重置它.