这是我的代码:
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 );
});
});
});
有没有办法在timeTocheck
结构之外使用counttim
和then
?我想保存价值并在其他地方使用它。
我只想做点什么:
var myTime = element(by.css('table[class=\'table\'] > tbody > tr:nth-child(1) > td:nth-child(1)')).getText();
并在myTime
中有一个我以后可以使用的字符串值...
我想对行数做同样的事情。
var rowNumbers = element(by.css('table[class=\'table\']')).all(by.css('tr')).count()
我不想比较它们我想用它们请帮助.....
答案 0 :(得分:1)
实际上,这是像Protractor这样的异步代码的一个真正问题,也是我经常遇到的问题。问题是所有命令在执行之前都放在命令队列中,因此尝试将gettext()放入变量并稍后使用(例如,检查页面之间的一致性)需要深入理解&之间的区别。 #34;解析时间"和"运行时间。"
你最好的选择是做这样的事情:
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()语句进行多次嵌套是不好的做法)。
我不是特别喜欢这个解决方案,所以我正在寻找另一个(即使我必须自己编写代码),但是现在这是我发现的最好的。
答案 1 :(得分:-1)
获取行数:
var count=element.all('Here css locator').count();
获取变量的文本:
var row=element.all('here css locator').get(INDEX of Row);
var text=row.element('here variable css locator').getText();