2016-03-27 46 views
1

以下代碼是Selenium Webdriver中的Java代碼。代碼從excel表格中讀取一個長列表。它將每個Excel單元格中的值存儲在變量LastNameFirstName中。在導航到SQL Server管理工作室數據庫之後,我需要在查詢中使用這些變量。這是我遇到問題的地方。當我使用命令'screen.type(LastName);'時,變量LastName拋出「無法解析爲變量」錯誤。在Selenium-Webdriver中使用Sikuli的Java變量

如何使用Sikuli中Java中定義的變量LastNameFirstName

File src = new File ("C:\\EmployeeList.xlsx"); 
FileInputStream fis = new FileInputStream(src); 
XSSFWorkbook wb = new XSSFWorkbook(fis); 
XSSFSheet sheet1 = wb.getSheetAt(0); 
int rowcount=sheet1.getLastRowNum(); 
System.out.println("Total Row is :" + rowcount); 

for (int i=0; i<rowcount;i++) { 
    String LastName=sheet1.getRow(i).getCell(0).getStringCellValue(); 
    String FirstName=sheet1.getRow(i).getCell(1).getStringCellValue(); 
    System.out.println("Data Employee List is " +i+ " "+"is "+ LastName+ ", "+FirstName+"); 
} 

wb.close(); 

//Navigated into SQL Server management studio database 
    screen.type(LastName); 

回答

0

LastNamefor環路局部範圍內聲明,因此它不for之外存在。你需要聲明它之外

String lastName, firstName; 
for (int i = 0 ; i < rowcount ; i++) { 
    lastName = sheet1.getRow(i).getCell(0).getStringCellValue(); 
    firstName = sheet1.getRow(i).getCell(1).getStringCellValue(); 
    System.out.println("Data Employee List is " + i + " " + "is " + lastName + ", " + firstName); 
} 

screen.type(lastName); 

我不知道這是否是故意的,但screen.type(lastName);將只使用lastName最後值。如果你想使用它們全部插入到循環中。在這種情況下,你可以離開lastName聲明for

for (int i = 0 ; i < rowcount ; i++) { 
    String lastName = sheet1.getRow(i).getCell(0).getStringCellValue(); 
    String firstName = sheet1.getRow(i).getCell(1).getStringCellValue(); 
    System.out.println("Data Employee List is " + i + " " + "is " + lastName + ", " + firstName); 
    screen.type(lastName); 
} 

裏面順便說一句,在Java中的變量應小寫開頭。

+0

謝謝!感謝幫助。其目的是從Excel表格中每行的每個單元格中獲取值。使用SQL查詢中的值,然後返回Excel表格中的下一行。 – sama