java+selenium 자동화 테스트 데이터 구동 실현

15997 단어 데이터 구동
Excel에서 데이터 읽기
public class ExcelDataProvider {
public Object[][] getTestDataByExcel(String fileName, String sheetName)throws IOException {
  File file = new File(fileName);
  FileInputStream inputstream = new FileInputStream(file);
  Workbook wbook = null;
  String fileExtensionName = fileName.substring(fileName.indexOf("."));
  System.out.println(fileExtensionName);
  if (fileExtensionName.equals(".xlsx")) {
   wbook = new XSSFWorkbook(inputstream);
  
  } else if (fileExtensionName.equals(".xls")) {
   wbook = new HSSFWorkbook(inputstream);
  }
  Sheet sheet = wbook.getSheet(sheetName);
  //  sheetName Sheet 
  int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
  //  sheet , 0 
  List<Object[]> records = new ArrayList<Object[]>();
  //  excel ( )
  for (int i = 1; i < rowCount + 1; i++) {
   Row row = sheet.getRow(i);
   String fields[] = new String[row.getLastCellNum()];
   for (int j = 0; j < row.getLastCellNum(); j++) {
    //  
    fields[j] = row.getCell(j).getStringCellValue();
   }
   records.add(fields);
  }
  Object[][] results = new Object[records.size()][];
  for (int i = 0; i < records.size(); i++) {
   results[i] = records.get(i);
  }
  return results;
 }
 }
// 
@Listeners(ApiListener.class)
public class login extends BaseTest{
 // 
 @DataProvider(name="user_name")
 public Object[][] data()throws IOException {
  ExcelDataProvider excel=new ExcelDataProvider();
  return excel.getTestDataByExcel("F:// //data.xlsx","Sheet1");
 }
 //   12 
 @Test(dataProvider="user_name",description=" ")
 public void testLogin(String u_name,String p_word) throws InterruptedException, IOException {
  // 
  String opensns_url =ReadProperties.getPropertyValue("base_url");
  // 
  webtest.open(opensns_url);
  // 
  webtest.click("link= ");
  webtest.type("name=username", u_name);
  webtest.type("name=password", p_word);
  Thread.sleep(2000);
  webtest.click("xpath=//button[@type='submit']");
  Thread.sleep(2000);
  assertTrue(webtest.isTextPresent(" "));
  webtest.click("xpath=//span[@class='user-name text-ellipsis']");
  webtest.click("xpath=//div[@event-node='logout']");
 } 
}

좋은 웹페이지 즐겨찾기