How to assign count of rows or getText to a variab

2019-02-19 03:27发布

问题:

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? 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.

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.....

回答1:

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

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.



回答2:

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