2015-11-19 15 views
1

切換幀誰能告訴我如何與iframe中引用的元素進行幀的切換完成後?我已經看了看How to switch iframes InternJS呈現無濟於事的解決方案,並在intern Functional Testing with Frames的信息是不適用下面的腳本返回Cannot read property 'apply' of undefined type: TypeError錯誤(但):如何進行後InternJS

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'); 
      }); 
    }); 

唯一的原因,我可以看到腳本失敗的原因是框架位置不正確。網頁上有兩個,我需要第一個。一旦完成,我真的很想把這個框架的引用放在一個頁面對象中(這是我感覺它屬於的地方),但我必須能夠成功找到它,所以不要把它放在馬前。建議和幫助非常感謝。

回答

5

你舉的例子其實是相當接近。主要問題是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基於鏈框架,你需要重新設置,如果你想在回調後返回到父框架。
+0

OMG,你只是完全讓我很快樂!明天早上我會給你賞金的第一件事。你有我不朽的感謝! – MBielski

相關問題