How To Assign Count Of Rows Or Gettext To A Variable In Protractor
Solution 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 scopesvar 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.
Solution 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 ofRow);
var text=row.element('here variable css locator').getText();
Post a Comment for "How To Assign Count Of Rows Or Gettext To A Variable In Protractor"