簡體   English   中英

如何在具有異步功能的應用程序上用js編寫功能測試?

[英]how to write functional tests in intern js on an app that has async fynctions?

我有一個HTML頁面,頁面加載后會要求提供憑據。 提交正確的憑據后,將執行異步功能。 異步函數返回一個Promise。 兌現諾言后,會將一個帶有響應文本的節點插入dom。

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

我究竟做錯了什么?

我認為應該是這樣的:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM