简体   繁体   English

如何在Protractor中将行数或getText分配给变量

[英]How to assign count of rows or getText to a variable in Protractor

Here is my code: 这是我的代码:

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

Is there a way to use timeTocheck and counttim outside of this then structure? 有没有办法使用的方式timeTocheckcounttim这个以外then结构呢? 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();

and in myTime to have a string value that I can use later... I want to do the same for the number of rows. 并在myTime中有一个我可以在以后使用的字符串值...我想对行数做同样的事情。

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..... 我不想比较它们我想用它们请帮忙.....

Actually, this is a real problem with asynchronous code like Protractor, and one that I encounter a lot. 实际上,这是像Protractor这样的异步代码的一个真正的问题,也是我经常遇到的问题。 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." 问题是所有命令在执行之前都放在命令队列中,因此尝试将gettext()放入变量并稍后使用(例如,检查页面之间的一致性)需要深入理解“之间的差异”。解析时间“和”运行时间。“

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

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). 如果你以后需要获取更多的变量,只需结束当前的then()并坚持另一个(使用then()语句多次嵌套是不好的做法)。

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. 我不是特别喜欢这个解决方案,所以我正在寻找另一个(即使我必须自己编写代码),但是现在这是我发现的最好的。

to get Row count: 得到行数:

var count=element.all('Here css locator').count();

to get text of a variable: 获取变量的文本:

var row=element.all('here css locator').get(INDEX of Row);
var text=row.element('here variable css locator').getText();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM