如何将行数或getText分配给量角器中的变量 [英] How to assign count of rows or getText to a variable in Protractor

查看:38
本文介绍了如何将行数或getText分配给量角器中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

describe('SuperCalculator Page', function() {

  beforeEach(function(){
      browser.get('http://juliemr.github.io/protractor-demo/');
  });

  it('get rows count and firs column value', function() {

      element(by.model('first')).sendKeys(1);
      element(by.model('second')).sendKeys(2);
      element(by.id('gobutton')).click();

      element(by.css('table[class=\'table\']')).all(by.css('tr')).count().then(function(rowCount) {
          counttim = rowCount;
          console.log(counttim);
      });

      element(by.css('table[class=\'table\'] > tbody > tr:nth-child(1) > td:nth-child(1)')).getText().then(function(text) {
          timeTocheck = text;
          console.log(timeTocheck,counttim );
      });
  });
});

是否可以在此 then 结构之外使用 timeTocheck counttim ?我想保存该值并在其他地方使用它.我只想做类似的事情:

Is there a way to use timeTocheck and counttim outside of this then structure? I want to save the value and use it in other place. I just want to do something like:

var myTime = element(by.css('table[class=\'table\'] > tbody > tr:nth-child(1) > td:nth-child(1)')).getText();

myTime 中有一个字符串值,以后可以使用...我想对行数做同样的事情.

and in myTime to have a string value that I can use later... I want to do the same for the number of rows.

var rowNumbers = element(by.css('table[class=\'table\']')).all(by.css('tr')).count()

我不想比较它们,我想使用它们,请帮助.....

I don't want to compare them I want to use them please help.....

推荐答案

实际上,这是诸如Protractor之类的异步代码的实际问题,而我遇到的很多问题.问题是您的所有命令在执行之前都已放入命令队列中,因此,尝试将gettext()放入变量中并在以后使用它(例如,检查页面之间的一致性)需要深刻理解解析时间"和运行时间".

Actually, this is a real problem with asynchronous code like Protractor, and one that I encounter a lot. The problem is that all your commands are placed in the Command Queue before they execute, so attempting to put gettext() into a variable and use it later (for example, to check consistency across pages) requires a deep understanding of the difference between "parse time" and "run time."

您最好的选择是执行以下操作:

Your best option is to do something like this:

describe('SuperCalculator Page', function() {

  beforeEach(function(){
      browser.get('http://juliemr.github.io/protractor-demo/');
  });

  it('gets row count and first column value', function() {

      // Declare your variables here so they'll be available in lower scopes
      var counttim, timeToCheck;

      element(by.model('first')).sendKeys(1);
      element(by.model('second')).sendKeys(2);
      element(by.id('gobutton')).click();

      element(by.css('table[class=\'table\']')).all(by.css('tr')).count().then(function(rowCount) {
          counttim = rowCount;
          console.log(counttim);
      })
      .then(function() {
          element(by.css('table[class=\'table\'] > tbody > tr:nth-child(1) > td:nth-child(1)')).getText().then(function(text) {
              timeToCheck = text;
              console.log(timeToCheck,counttim );
          });
      })
      .then(function() {
          // The rest of your program can go here (as many statements as
          //    needed), and it can actually use the variables you picked 
          //    up earlier because it's chained to a then() statement, so 
          //    they don't compute until the promises resolve.
          //
          // Just refer to them by name, like this:
          // expect(counttim).toEqual(1);
          // expect(timeToCheck).toEqual(3);
      });
  });
});

这是一种丑陋的处理方式,因为它添加了一层嵌套的括号/括号,但效果很好.如果以后需要获取更多变量,只需结束当前的then()并坚持使用另一个变量(不好的做法是在then()语句中嵌套).

This is an ugly way to do things, because it adds a layer of nested brackets/parentheses, but it works fine. If you need to grab more variables later, just end the current then() and stick on another one (multiple nesting with then() statements is bad practice).

我不是特别喜欢这种解决方案,因此我正在寻找另一种解决方案(即使我必须自己编写),但是目前这是我找到的最好的解决方案.

I don't particularly like this solution, so I am in search of another one (even if I have to code it myself), but for now this is the best I've found.

这篇关于如何将行数或getText分配给量角器中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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