问题
cellsData
have array like below in global variable
[ { id: 2,
cellId: 'R2C3',
row: '2',
col: '3',
value: '202',
rawValue: '202.0',
numValue: '$202' },
{ id: 3,
cellId: 'R2C4',
row: '2',
col: '4',
value: '2034',
rawValue: '2034.0',
numValue: '$2,034' }]
I have a soda script in which I want to verify the data after reading from this array
problem is it gives error on cellsData[startRowId].numValue
as, 27 undefined
I tried global.cellsData
also but no working
error
.keyYears table tbody tr:nth-child('+i+') td:nth-child(3)',''+ cellsData[startRowI ^
TypeError: Cannot read property '27' of undefined
var browser = soda.createClient({
.....
});
// Retrieving data from Excel sheet
testsheet.setAuth( 'user', 'pass', function(err){
if (err) console.log(err);
testsheet.getCells( 1, function(err, cellsObj){
if (err) console.log(err);
cellsData = cellsObj.cells;
rowcount = cellsObj.RowCount;
});
})
browser
.chain
.session()
.setSpeed(speed)
.setTimeout(2000)
.open('/')
.and(login('dev@dev.com', 'x1212GQsdtpS'))
.and(verifyData())
.end(function(err){
console.log('error');
});
function login(user, pass) {
return function(browser) {
browser
.click('css=a#loginButton')
.type('css=input.input-medium.email',user)
.type('css=input.input.pwd',pass)
.clickAndWait('css=a.btn.login')
.assertTextPresent('Clients',function(){ console.log('logged in ok')})
}
}
function verifyData() {
var startRowId=27
console.log('inside TestingSpreaddsheet')
var totalLoop=(rowcount-5)+1
return function(browser) {
browser
for (var i = 1; i <= 4; i++) {
browser.assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(3)',''+ cellsData[startRowId].numValue +'',function(){ console.log('looks good')})
.assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(5)',''+ cellsData[startRowId].numValue +'')
.assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(6)',''+ cellsData[startRowId].numValue +'')
.assertText('css=div.keyYears table tbody tr:nth-child('+i+') td:nth-child(7)',''+ cellsData[startRowId].numValue +'')
}
}
}
UPDATE Soda is running the chain before fetching my ExcelSheet Data. How to control chain ?
回答1:
in node, most things are async, not sync. the data hasn't arrived yet hence rest process breaking.
the only place in code where I have the data is inside testsheet function so i moved my test code there and it fixed this issue.
来源:https://stackoverflow.com/questions/16261010/nodejs-retrieving-value-from-array-show-as-un