cancel
Showing results for 
Search instead for 
Did you mean: 

how i give iteration into my script to read from excel sheet multiple times

SOLVED
Occasional Contributor

how i give iteration into my script to read from excel sheet multiple times

I used this syntax for to read data but i try to do multiple set of data to run my scripts(rather than keywords) multiple times.so What syntax I should use in to my scripts  to read different data and run my scripts multiple times. 

 

function testdata(){
var Driver = DDT.ExcelDriver("D:\\MyFile2.xlsx", "Sheet1");

// Iterates through records
while (! Driver.EOF())
{
//Gets a value from the storage and posts it to the log
var test =(Driver.Value('Customer name)');
DDT.CurrentDriver.Next();

}

// Closes the driver
DDT.CloseDriver(Driver.Name);
return test;
}

function testone(){
Log.Message("This is : " +testdata())
}

Urmi

1 ACCEPTED SOLUTION

Accepted Solutions
Community Leader

Re: how i give iteration into my script to read from excel sheet multiple times

var test =(Driver.Value('Customer name)');
DDT.CurrentDriver.Next();

 

This always re-initiates the varable test.  That means it will only ever contain the last single value you were looking for.  Try something like this instead:

var returnValue;
while (! Driver.EOF()) 
{ 
//Gets a value from the storage and posts it to the log
returnValue += "; " + (Driver.Value('Customer name')); 
DDT.CurrentDriver.Next(); 

}
...
return returnValue;

This should return a list of all the values for a given field in a single string seperated by ";"

 

That may not be what you are looking for, but it looks like the intent is to bring back ALL the records, rather than just the last record.  Consider making your function more generic:

 

function testdata(filename,sheetname,fieldname){
var Driver = DDT.ExcelDriver(filename, sheetname);
var returnValue while(!Driver.EOF()){
...
returnValue += "; " + Driver.Value(fieldname);
...
}
... } function testone(){ var functionReturn = testdata("D:\\MyFile2.xlsx","Sheet1","Customer Name"); Log.Message(functionReturn); }

That way it means you can re-use the function

 

 


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
2 REPLIES 2
Community Hero

Re: how i give iteration into my script to read from excel sheet multiple times

Run your test inside the DDT loop (your While loop) so it executes once for each time through the loop.   Here are the details:

https://support.smartbear.com/testcomplete/docs/testing-with/data-driven/using-scripts.html

Community Leader

Re: how i give iteration into my script to read from excel sheet multiple times

var test =(Driver.Value('Customer name)');
DDT.CurrentDriver.Next();

 

This always re-initiates the varable test.  That means it will only ever contain the last single value you were looking for.  Try something like this instead:

var returnValue;
while (! Driver.EOF()) 
{ 
//Gets a value from the storage and posts it to the log
returnValue += "; " + (Driver.Value('Customer name')); 
DDT.CurrentDriver.Next(); 

}
...
return returnValue;

This should return a list of all the values for a given field in a single string seperated by ";"

 

That may not be what you are looking for, but it looks like the intent is to bring back ALL the records, rather than just the last record.  Consider making your function more generic:

 

function testdata(filename,sheetname,fieldname){
var Driver = DDT.ExcelDriver(filename, sheetname);
var returnValue while(!Driver.EOF()){
...
returnValue += "; " + Driver.Value(fieldname);
...
}
... } function testone(){ var functionReturn = testdata("D:\\MyFile2.xlsx","Sheet1","Customer Name"); Log.Message(functionReturn); }

That way it means you can re-use the function

 

 


-------------------------------------------------
Standard syntax disclaimers apply
Regards,
New Here?
Join us and watch the welcome video:
API Testing Mistake #2
APITestingMistake#2
Top Kudoed Authors