2015-07-09 44 views
0

我有一個頁面加載後請求憑據的html頁面。一旦提交了正確的信用卡,就會執行異步功能。異步函數返回一個承諾。一旦這個承諾解決了,一個節點就會被插入到dom中,並帶有響應文本。如何在實習生js中編寫具有異步同步功能的應用程序的功能測試?

var executeRequest = Request(req); 
    executeRequest.then(function(response) { 
    var node = domConstruct.toDom("<div id='text'></div>"); 
    domConstruct.place(node, "title", "after"); 
    node.innerHTML = JSON.stringify(response); 
    }); 

但是測試沒有完全執行,因爲它沒有等待承諾解決。

var dfd = this.async(15000); 
    return this.remote 
    .get(require.toUrl(url)) 
    .setFindTimeout(5000) 
    .elementById('dijit_form_ValidationTextBox_0') 
    .click() 
    .type('user1') 
    .end() 
    .elementById('dijit_form_ValidationTextBox_1') 
    .click() 
    .type('user1') 
    .end() 
    .elementById('dijit_form_Button_0') 
    .click() 
    .end() 
    .waitForElementById('text') 
    .text() 
    .then(dfd.rejectOnError(function(result) { 
     assert.equal(result.length, 2, 'When form is submitted, operation should complete successfully'); 
     dfd.resolve(); 
    }), dfd.reject); 

我在做什麼錯?

回答

0

我想應該是這樣的:

//ready is dojo/tests/support/ready 
return ready(this.get('remote'), require.toUrl(url)) 
    .setFindTimeout(5000) 
    .elementById('dijit_form_ValidationTextBox_0') 
    .click() 
    .type('user1') 
    .end() 
    .elementById('dijit_form_ValidationTextBox_1') 
    .click() 
    .type('user1') 
    .end() 
    .elementById('dijit_form_Button_0') 
    .click() 
    .end() 
    .waitForElementById('text') 
    .text() 
    .then(function (result) { 
     assert.equal(result, 'loaded'); 
    }); 
+0

沒有本,它沒有工作。當異步'請求'方法返回promise時,它跳到下一個測試。我想,我正在以錯誤的方式處理那部分。 –

0

this.remote.get(...)被調用時,目標網頁的同步部分(圖片,腳本標記等)的服務器的webdriver等待加載,然後移動上。如果您的頁面包含異步啓動代碼,那麼您必須在測試中添加一些內容以等待它完成。既然您知道將添加到頁面的元素的ID,只需使用findById等待它:

return this.remote 
    // Load the page 
    .get(require.toUrl(url)) 

    // Make the find timeout long enough that your Promise should have 
    // completed 
    .setFindTimeout(5000) 

    // Start looking for the element that should be added; Intern will keep 
    // looking for up to findtimeout ms 
    .findElementById('text') 
    .end() 

    // At this point the element is in place, so the promise resolved 
    // rest of test