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

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

问题描述

当我使用量角器执行下面的代码它的作品。我正在传递嵌套的JSON到循环。由于for循环的异步工作,它打印变量i的所有值并达到上一个值,因为它总是访问最后一对用户名和密码。如何解决这个问题?



var data = require('.. ./testdata.json');describe(homepage Test),function(){it('candidate login',function(){browser.driver.get('https://abcxyz.com'); for(i在data.testdata)元素(by.id('tool_btn3'))。click(); console.log(i); browser.getTitle()。然后(函数(标题){console.log(标题:如果(title ===< page title>){browser.driver.sleep(3000); element(by.id('email_input'))。sendKeys(data.testdata [i] .username) ;元素(by.id('pwd_input'))。sendKeys(data.testdata [i] .password);元素(by.xpath('// * [@ id =signIn_btn] / div [2]') ).click(); browser.sleep(3000); element(by.id('setting_img'))。click(); browser.sl eep(2000); element(by.id('logout_div'))。click()。then(function(){console.log('success'); }); }其他{console.log(问题); }}); }});});


解决方案

您需要记住,您不能对 -loop使用和promise。所有的都是异步的,所以在结尾它会咬你的屁股,这意味着 it 已经准备好了,但是测试不是。 p>

根据你的例子,它会提出一个叫做 logon 的方法(把它放在页面对象中)。它会为你做登录和东西。添加一个空的承诺容器(数组),并在那里推承诺。


$ b for -loop是你可以立即解决完整的承诺容器,它会执行所有的承诺一个接一个。它会看起来像这样。


$ b

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)); promise。 push(console.log(i));} Promise.all(promises);});}); / ** *登录* @params {字符串}用户名* @params {字符串}密码* @return {布尔} / function logon(username,password){element(by.id('tool_btn3'))。click(); \ return browser.getTitle()。then(function(title){console.log(Title:+标题)if(title ===< page title>){browser.driver.sleep(3000); element(by.id('emai l_input))的SendKeys(用户名)。元件(by.id( pwd_input))的SendKeys(密码)。元件(by.xpath( // * [@ ID = signIn_btn] / DIV [2]))点击(); browser.sleep(3000);元件(by.id( setting_img))点击(); browser.sleep(2000);返回元素(by.id('logout_div'))。click().then(function(){return Promise.resolve(true);}); } else {return Promise.resolve(false);如果您正在使用节点7(如节点7),则可以使用节点7您可以使用 async / await ,或者使用Babel来编译代码。如果你可以编写TypeScript,你也可以得到 async / await


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

解决方案

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.

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.

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

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天全站免登陆