New to Telerik Test Studio Dev Edition? Start a free 30-day trial
Random Row from Excel Data Source
Is there a way to data drive a step in a test script so that each time the script is run, the test step will select a random row from the Excel data source?
Solution
In code, generate a random number based on the number of rows. Then use the corresponding text from that row's cell in the applicable test step.
1.Create a basic test against Bing.com to enter a value in the Search field and click on the Search button. 2.Convert any of the recorded steps into code. 3.Enter the below code into the coded step. The searchQuery.xlsx Excel file contains five rows of data.
* **Note:** Ensure you <a href="/advanced-topics/coded-steps/add-assembly-reference" target="_blank">Add an Assembly Reference</a> to *Microsoft.Office.Interop.Excel*. You can download a version of that file on Microsoft's website that matches your version of MS Office.
C#
Random random = new Random();
int num = random.Next(1, 6);
string input = @"C:\Data\searchQuery.xlsx";
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook inputBook = app.Workbooks.Open(input, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Worksheet inputSheet = (Microsoft.Office.Interop.Excel.Worksheet)((inputBook.Worksheets).get_Item(1));
string value = ((Microsoft.Office.Interop.Excel.Range)inputSheet.Cells[num, 1]).Text as string;
app.Quit();
app = null;
Pages.Bing.SbFormQText.Text = value;