軟體測試 實驗02 MAC環境下Firefox配置selenium java讀取xlsx檔案

來源:互聯網
上載者:User

標籤:ade   科學   mave   read   column   print   ber   下載連結   過程   

安裝環境

我的環境是mac + firefox42 + selenium 2.9.1
Firefox曆史版本的下載連結:http://ftp.mozilla.org/pub/firefox/releases/
本次實驗需要下載大量jar包,如所示

需要下載連結的同學可以私信我- -

實驗過程
  1. 開啟selenium,錄製一系列操作,我的操作是訪問https://psych.liebes.top/st這個網址,輸入自己的帳號和密碼,然後點一下出來的github連結。將錄製好的操作匯出為java檔案。
  2. 開啟eclipse,建立一個java maven項目(mac新版的的eclipse內建了maven),然後將錄製好操作的java代碼粘貼進去。
  3. 接下來我們需要讀取xlsx的檔案了,首先我們要引入一些包
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.concurrent.TimeUnit;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.DateUtil;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.xssf.usermodel.XSSFWorkbook;

讀取xlsx的代碼如下所示(將這些代碼放入到主函數裡即可):

Workbook wb =null;      Sheet sheet = null;      Row row = null;      List<Map<String,String>> list = null;      String cellData = null;      String filePath = "/Users/izayoi/Desktop/input.xlsx";      String columns[] = {"name","git"};      wb = readExcel(filePath);      if(wb != null){          //用來存放表中資料          list = new ArrayList<Map<String,String>>();          //擷取第一個sheet          sheet = wb.getSheetAt(0);          //擷取最大行數          int rownum = sheet.getPhysicalNumberOfRows();          //擷取第一行          row = sheet.getRow(0);          //擷取最大列數          int colnum = row.getPhysicalNumberOfCells();          for (int i = 1; i<rownum; i++) {              Map<String,String> map = new LinkedHashMap<String,String>();              row = sheet.getRow(i);              if(row !=null){                  for (int j=0;j<colnum;j++){                      cellData = getStringVal(row.getCell(j));                      //System.out.println(cellData.trim());                      map.put(columns[j], cellData.trim());                  }              }else{                  break;              }              list.add(map);          }      }      //遍曆解析出來的list      for (Map<String,String> map : list) {          String username = "";          String giturl = "";          String password = "";          int flag = 0;          for (Entry<String,String> entry : map.entrySet()) {              if(flag == 0)              {                  username = entry.getValue();                  flag++;              }              else              {                  giturl = entry.getValue();                  System.out.println(giturl);                  flag--;              }                        }          try {              assertEquals(giturl, driver.findElement(By.xpath("//p")).getText());              System.out.println(giturl);            } catch (Error e) {              verificationErrors.append(e.toString());            }           System.out.println();      }

其中,讀取xlsx的函數如下所示:

public static Workbook readExcel(String filePath){      Workbook wb = null;      if(filePath==null){          return null;      }      String extString = filePath.substring(filePath.lastIndexOf("."));      InputStream is = null;      try {          is = new FileInputStream(filePath);          if(".xls".equals(extString)){              return wb = new HSSFWorkbook(is);          }else if(".xlsx".equals(extString)){              return wb = new XSSFWorkbook(is);          }else{              return wb = null;          }                } catch (FileNotFoundException e) {          e.printStackTrace();      } catch (IOException e) {          e.printStackTrace();      }      return wb;  }  public static Object getCellFormatValue(Cell cell){      Object cellValue = null;      if(cell!=null){          //判斷cell類型          switch(cell.getCellType()){          case Cell.CELL_TYPE_NUMERIC:{              cellValue = String.valueOf(cell.getNumericCellValue());              break;          }          case Cell.CELL_TYPE_FORMULA:{              //判斷cell是否為日期格式              if(DateUtil.isCellDateFormatted(cell)){                  //轉換為日期格式YYYY-mm-dd                  cellValue = cell.getDateCellValue();              }else{                  //數字                  cellValue = String.valueOf(cell.getNumericCellValue());              }              break;          }          case Cell.CELL_TYPE_STRING:{              cellValue = cell.getRichStringCellValue().getString();              break;          }          default:              cellValue = "";          }      }else{          cellValue = "";      }      return cellValue;  }

因為有些表格的數字是用科學計數法表示的,所以需要下面這個函數來將科學計數法的數字轉化為字串

  private static String getStringVal(Cell cell) {      switch (cell.getCellType()) {          case Cell.CELL_TYPE_BOOLEAN:              return cell.getBooleanCellValue() ? "true" : "false";          case Cell.CELL_TYPE_FORMULA:              return cell.getCellFormula();          case Cell.CELL_TYPE_NUMERIC:              cell.setCellType(Cell.CELL_TYPE_STRING);              return cell.getStringCellValue();          case Cell.CELL_TYPE_STRING:              return cell.getStringCellValue();          default:              return "";      }  }

這個表格裡有不少坑,比如有一個學生沒有github連結,這裡我直接判斷他匹配失敗,還有一些學生的github連結前有空格,我用trim()消除了這些空格
最後的運行結果如所示:

附一個github的網址:
https://github.com/IzayoiNamida/tjuscs-_software_testing

軟體測試 實驗02 MAC環境下Firefox配置selenium java讀取xlsx檔案

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.