2015-05-04 75 views
0

我需要編寫一段代碼,通過一個Excel文件(.xlsx) 的工作表,然後通過WebDriver逐個使用這些值。如何使用Excel工作表中的單元格值作爲輸入?

更具體地說,一張表擁有搜索引擎鏈接,另一張表持有查詢。

我只需要你幫我弄清楚如何使用Excel文件 中的鏈接,而不是作爲硬編碼值的一種方式來遍歷它們。

這是我到目前爲止有:

package a.utilities; 

import java.awt.List; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.*; 

import a.utilities.ChromeDriverInit; 

import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 
import org.apache.poi.*; 
import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.openqa.selenium.WebDriver; 

public class ExcelUtils { 
    static WebDriver driver; 
    protected static XSSFSheet excelWSheet; 
    protected static XSSFWorkbook excelWBook; 
    private static XSSFCell cell; 
    private static XSSFRow row; 
    // 
    static String web; 
    static String query; 

    // Setting the file to read from 
    public static void setExcelFile() throws FileNotFoundException { 
     FileInputStream file = null; 

     try { 
      file = new FileInputStream("ExcelWorkSheets/SearchEngines.xlsx"); 
      excelWBook = new XSSFWorkbook(file); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 

     } finally { 
      if (file != null) { 
       try { 
        file.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 

    // Counting the used rows in every work sheet and give you the work sheets data 
    public static void getSheetData() { 
     int index = 0; 
     for (int i = 0; i < excelWBook.getNumberOfSheets(); i++) { 
      index++; 
      System.out.println("Sheet Name: " + "[" 
        + excelWBook.getSheetName(i) + "] --> " + "Sheet index: " 
        + "[" + index + "]\n"); 

     } 
     int rowIndex = 0; 
     for (int i = 0; i < excelWBook.getSheetAt(0).getLastRowNum()+1; i++) { 
      excelWSheet = excelWBook.getSheetAt(0); 
      rowIndex++; 

     } 
     System.out.println("Number of rows including the header: --> " + rowIndex); 
     System.out.println("Number of rows not including the header: --> " +excelWSheet.getLastRowNum()); 
     System.out.println(); 


     int rowIndex2 = 0; 
     for (int i = 0; i < excelWBook.getSheetAt(1).getLastRowNum()+1; i++) { 
      excelWSheet = excelWBook.getSheetAt(1); 
      rowIndex2++; 

     } 
     System.out.println("Number of rows including the header: --> " + rowIndex2); 
     System.out.println("Number of rows not including the header: --> " +excelWSheet.getLastRowNum()); 
     System.out.println(); 


    // Going through the SearchEngines work sheet to get the data from every used cell and prints it out 

     Iterator<Row> rowIterator = excelWSheet.iterator(); 
     while(rowIterator.hasNext()) { 
      Row row = rowIterator.next(); 

      Iterator<Cell> cellIterator = row.cellIterator(); 
      while(cellIterator.hasNext()) { 

       Cell cell = cellIterator.next(); 

       switch (cell.getCellType()) { 
       case Cell.CELL_TYPE_BOOLEAN: 
        System.out.println(cell.getBooleanCellValue() + "\t\t"); 
        break; 
       case Cell.CELL_TYPE_NUMERIC: 
        System.out.println(cell.getNumericCellValue() + "\t\t"); 
       case Cell.CELL_TYPE_STRING: 
        web = cell.getStringCellValue(); 
        System.out.println(web + "\t\t"); 

       default: 
        break; 
       } 
      } 
      System.out.println(""); 
     } 
     // Going through the queries work sheet to get the data from every used cell and prints it out 
     Iterator<Row> rowIterator2 = excelWSheet.iterator(); 
      while(rowIterator2.hasNext()) { 
       Row row = rowIterator2.next(); 

       Iterator<Cell> cellIterator = row.cellIterator(); 
       while(cellIterator.hasNext()) { 

        Cell cell = cellIterator.next(); 

        switch (cell.getCellType()) { 
        case Cell.CELL_TYPE_BOOLEAN: 
         System.out.println(cell.getBooleanCellValue() + "\t\t"); 
         break; 
        case Cell.CELL_TYPE_NUMERIC: 
         System.out.println(cell.getNumericCellValue() + "\t\t"); 
         break; 
        case Cell.CELL_TYPE_STRING: 
         System.out.println(cell.getStringCellValue() + "\t\t"); 
         break; 

        default: 
         break; 
        } 
       } 
       System.out.println(""); 
      } 
    }   

這並瀏覽清單,但我怎麼把它傳遞這樣的webdriver將能夠使用這個值?

+0

相反,它打印到'的System.out.println(...)的'值保存到'ArrayList'並將其傳遞到的webdriver。 – JonasCz

+0

對不起,我的java技能不是那麼好,但我需要問:怎麼樣?我該如何做到這一點?我知道一般的想法是使用ArrayList,但我又是一個noob。 – Okowalsky

回答

0

因此,儘量編寫程序,以便從文件中讀取一個值。將該值存儲在一個java變量中,並根據webdriver的需要使用該變量。然後根據需要迭代它。

我認爲流程應該是這樣的。

  • 閱讀與搜索引擎(搜索引擎迭代)
  • 使用去打開URL
  • 迭代查詢
  • 重複用於下一個搜索ENGINS
0

的XLSX文件值可以說,你從Excel文件中獲取的值是名稱和郵件標識。

將這兩個值都存儲在它們的數據類型變量中。

可以說名稱mailid是兩個必需的變量。

現在使用上面創建的參數創建一個方法,將它從Excel文件發送到網頁。

以下只是您可以實現的示例實現。

public static void runTest(String name,String mailid) throws InterruptedException 
    {  
     System.out.println("Inputing name: "+name+" and mailid: "+mailid); 

     driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).clear(); 
     driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).clear(); 

     driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[1]/td[2]/input")).sendKeys(name); 
     driver.findElement(By.xpath("html/body/div[6]/div[1]/div/div[4]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]/input")).sendKeys(mailid); 

     System.out.println("Inputted name: "+name+" and mailid: "+mailid); 

     Thread.sleep(2000); // Sleeping 2 seconds so that each entry is detected. 

    } 
} 
0
  1. 保存價值的數據結構。
  2. 遍歷值。
  3. 創建一個幫助函數,它將逐個傳遞給Web驅動程序。

僞代碼: -

//Instead of printing the values 
    //Save all the site in 
    List<String> sites = new ArrayList<String>(0); 
    //Save all the queries in 
    List<String> queries = new ArrayList<String>(0); 
    for(String site : sites){ 
    System.out.println("Time consumed:- " + runQueriesForSite(site, queries)); 
    } 




    private int runQueriesForSite(site, queries){ 
     int searchTime = 0; 
     for(String query : queries) { 
      searchTime += runQueryForSite(query, site); //Webdriver will use this function to connect and return result 
     } 
     return searchTime; 
    } 


int runQueryForSite(query, site) 
{ 
//Hope you'll find the time calculation algorithm yourself 
WebDriver driver = new HtmlUnitDriver(); 

     // And now use this to visit Google 
     driver.get(site); 

     // Find the text input element by its name 
     WebElement element = driver.findElement(By.name("q")); 

     // Enter something to search for 
     element.sendKeys(query); 

     // Now submit the form. WebDriver will find the form for us from the element 
     element.submit(); 

     // Check the title of the page 
     System.out.println("Page title is: " + driver.getTitle()); 

     driver.quit(); 
} 
相關問題