How to assign row count or getText to a variable in Protractor

Here is my code:

describe('SuperCalculator Page', function() { beforeEach(function(){ browser.get('http://juliemr.imtqy.com/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 this then structure? I want to save the value and use it elsewhere. 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 myTime to have a string value that I can use later ... I want to do the same for the number of lines.

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

I do not want to compare them, I want to use them, please help .....

+3
javascript protractor
source share
2 answers

Actually, this is a real problem with asynchronous code like Protractor and the one I am facing. The problem is that all of your commands are placed in the command queue before they are executed, so trying to put gettext () in a variable and use it later (for example, to check consistency between pages) requires a deep understanding of the difference between "parsing time" and "runtime "

Your best option is to do something like this:

 describe('SuperCalculator Page', function() { beforeEach(function(){ browser.get('http://juliemr.imtqy.com/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 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 something because it adds a layer of nested brackets / brackets, but it works great. If you need to capture more variables later, just end the current then () and stick with the other (multiple nesting with then () statements is bad practice).

I don’t really like this solution, so I'm looking for another (even if I need to encode it myself), but so far this is the best I have found.

+1
source share

to get the number of rows:

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

to get the text of the variable:

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

All Articles