我需要幫助讀取Excel表格中的所有行作爲我的硒測試用例中的測試數據。如何讀取Excel表格的所有行並使用C#在Selenium測試腳本中運行?
我只能用下面的代碼讀取Excel表格的一行。硒
//設置測試
public void SetupTest()
{
selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com/");
selenium.Start();
verificationErrors = new StringBuilder();
}
//連接我到我的excel表格中這是SampleTestData.xls
public void ConnectExcel()
{
excel.Application excelApp = new excel.Application();
excelApp.Visible = true;
string ExcelDataPath = @"C:\SampleTestData.xls";
excel.Workbook excelWorkBook = excelApp.Workbooks.Open(ExcelDataPath, 0, false, 5, "", "", false, excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
excel.Sheets excelSheets = excelWorkBook.Worksheets;
excel.Worksheet DataworkSheet = (excel.Worksheet)excelSheets.get_Item("Sheet1");
System.String Search1 = ((excel.Range)DataworkSheet.get_Range("A1", Type.Missing)).Value2 as string;
}
//硒代碼來運行測試
public void StartTest()
{
selenium.open("");
selenium.WaitForPageToLoad("100000");
selenium.Type("gbqfif", search1);
selenium.Click("btnG");
selenium.WaitForPageToLoad("100000");
}
此代碼讓我只選擇一個包含search1值的行。但是,我需要迭代它,以便它將繼續運行StartTest()方法,直到輸入Excel表中的所有行(50)。
任何幫助,將不勝感激。
考慮使用一個foreach或For循環..做谷歌搜索以及.. – MethodMan 2012-08-01 17:04:21
看看這個鏈接並學習如何使用基於這個例子的Interop它將是一個很好的學習經驗.. http://stackoverflow.com/questions/11593628/reading-data-from-excel-spreadsheet-cannot-perform-runtime-binding- on-a-null-r – MethodMan 2012-08-01 17:07:38
感謝DJ KRAZE,我看了一下這個帖子,但它太複雜了。我只想循環使用我無法做到的excel行。 – seleniumlover 2012-08-01 17:09:48