【问题标题】:how to write functional tests in intern js on an app that has async fynctions?如何在具有异步功能的应用程序上用实习生 js 编写功能测试?
【发布时间】:2015-07-09 21:49:13
【问题描述】:

我有一个 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);

我做错了什么?

【问题讨论】:

    标签: javascript asynchronous dojo intern


    【解决方案1】:

    我认为应该是这样的:

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

    【讨论】:

    • 不,它没用。当异步request 方法返回承诺时,它会跳到下一个测试。我想,我处理那部分的方式不对。
    【解决方案2】:

    当调用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-30
      • 2014-05-21
      • 1970-01-01
      • 2015-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多