2017-07-05 37 views
0

我使用下面的代碼將Excel文件中的屬性值設置爲我的Property Step,然後將其傳遞給測試用例。使用Groovy腳本進行數據驅動測試

當我運行Groovy代碼時,來自excel最後一個單元格的值進入屬性步驟。

請幫助我正確設置屬性。

import jxl.* 

Workbook wb = Workbook.getWorkbook(new File("C:\\Users\\naraysa1\\Desktop\\Address_Doctor\\DataSource_Address_Validate\\Data_AddressValidate.xls")) 
Sheet sh = wb.getSheet(0) 
RowCount = sh.getRows(); 
ColumnCount = sh.getColumns(); 
for (i=1;i<RowCount;i++) 
{ 
    for (j=0;j<ColumnCount;j++) 
    { 
     def c = sh.getCell(j, i).getContents(); 
     log.info c 

     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("ID", c) 
     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("AddressLine1", c) 
     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("AddressLine2", c) 
     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("AddressLine3", c) 
     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("City", c) 
     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("State ", c) 
     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("PostalCode", c) 
     testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue("Country", c) 

     def TestStep = testRunner.runTestStepByName("Search_Address") 
    } 
}  

回答

0

在腳本中,您將excel的每個單元格的值分配給所有屬性,併爲每個單元格執行teststep。

您可能想要將單元格值從一行映射到不同的屬性並對每行執行測試。

定義下列地圖

def header=[ 
    0:'ID', //means put cell0 into property `ID` 
    1:'AddressLine1', 
    2:'AddressLine2', 
    3:'AddressLine3', 
    4:'City', 
    ... 
] 

(你可以從零排您的Excel表格的初始化這個地圖),現在你的週期將是這樣的:

for (i=1;i<RowCount;i++) 
{ 
    for (j=0;j<ColumnCount;j++) 
    { 
     def pname = header[j] 
     if(pname){ 
      def c = sh.getCell(j, i).getContents(); 
      testRunner.testCase.getTestStepByName("Properties_Address").setPropertyValue(pname, c) 
     } 
    } 
    def TestStep = testRunner.runTestStepByName("Search_Address") 
} 
相關問題