量角器中for循环的异步工作 [英] Asynchronously working of for loop in protractor

查看:25
本文介绍了量角器中for循环的异步工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用量角器执行以下代码时,它可以工作.我将嵌套的 json 传递给 for 循环.由于 for 循环的异步工作,它打印变量 i 的所有值并达到最后一个值,因此它总是访问最后一对用户名和密码.我该如何解决这个问题?

when i execute the following code using protractor it works. I am passing nested json to for loop. Because of asynchronously working of for loop it print all values of variable i and reaches to last value because of this it always access last pair of username and password. How can i solve this issue?

var data = require('.../testdata.json');

describe('homepage Test', function() {

  it('candidate login', function() {
    browser.driver.get('https://abcxyz.com');
    for (i in data.testdata) {
      element(by.id('tool_btn3')).click();
      console.log(i);
      browser.getTitle().then(function(title) {
        console.log("Title: " + title)
        if (title === "<page title>") {
          browser.driver.sleep(3000);
          element(by.id('email_input')).sendKeys(data.testdata[i].username);
          element(by.id('pwd_input')).sendKeys(data.testdata[i].password);
          element(by.xpath('//*[@id="signIn_btn"]/div[2]')).click();
          browser.sleep(3000);
          element(by.id('setting_img')).click();
          browser.sleep(2000);
          element(by.id('logout_div')).click().then(function() {
            console.log('success');
          });
        } else {
          console.log("problem");
        }
      });
    }
  });
});

推荐答案

你需要记住,你不能使用带有 Promise 的 for 循环.一切都是异步的,所以在最后它会咬你的屁股,这意味着 it 已经准备好但测试还没有.

You need to keep in mind that you can't use a for-loop with promises. All is async so in the end it will bite you in the ass, meaning that the it is ready but the test isn't.

根据您的示例,建议创建一个名为 logon 的方法(将其放在页面对象或其他东西中).它会为你做登录和东西.添加一个空的 Promise 容器(数组)并将 Promise 推入其中.

Based on you example it would suggest to make a method called for example logon (place it in a Page Object or something). It will do the logon and stuff for you. Add an empty promise-container (array) and push the promises in there.

for 循环完成后,您可以立即解析完整的 Promise 容器,它将依次执行所有 Promise 1.它看起来像这样.

When the for-loop is done you can resolve the complete promise-container at once and it will execute all the promises 1 after each other. It will look something like this.

var data = require('.../testdata.json');

describe('homepage Test', function() {

  it('candidate login', function() {
    var promises = [];
    browser.driver.get('https://abcxyz.com');
    for (i in data.testdata) {
      promises.push(expect(logon(data.testdata[i].username, data.testdata[i].password)).to.equal(true));
      promises.push(console.log(i));
    }
    Promise.all(promises);
  });
});

/**
 * Logon
 * @params {string} username
 * @params {string} password
 * @return {boolean}
 */
function logon(username, password) {
  element(by.id('tool_btn3')).click();
  return browser.getTitle().then(function(title) {
    console.log("Title: " + title)
    if (title === "<page title>") {
      browser.driver.sleep(3000);
      element(by.id('email_input')).sendKeys(username);
      element(by.id('pwd_input')).sendKeys(password);
      element(by.xpath('//*[@id="signIn_btn"]/div[2]')).click();
      browser.sleep(3000);
      element(by.id('setting_img')).click();
      browser.sleep(2000);
      return element(by.id('logout_div')).click()
        .then(function() {
          return Promise.resolve(true);
        });
    } else {
      return Promise.resolve(false);
    }
  });
}

如果您使用的是 Node 7,您可以使用 async/await,或者使用 Babel 转译代码.如果您可以编写 TypeScript,您还将获得 async/await

If you are using for example Node 7 you can use async/await, or use Babel to transpile the code. If you can write TypeScript you also get the async/await

这篇关于量角器中for循环的异步工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆