JAVA+Selenium 练习(1): 使用POI对excel的读写

3 篇文章 0 订阅
1 篇文章 0 订阅

建了3个包:一个用来写测试数据文件(dataDriver);一个用来写页面对象(pageObjects),还有一个写执行脚本(testScripts)

直接上个代码

一、dataDriver包下两个class:GetTestData.java、writeTestResult.java

    1、GetTestData.java代码:

package dataDriver;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
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;

public class GetTestData {
    
    public Object[][] getExcelData(String path,String fileName,String sheetName) throws IOException{
        File file =  new File(path+"\\"+fileName);
        FileInputStream inputStream = new FileInputStream(file);
        
        Workbook wb = null;
        String fileExtensionName = fileName.substring(fileName.indexOf("."));
        if(fileExtensionName.equals(".xlsx")) {
            wb = new XSSFWorkbook(inputStream);
        }
        else if(fileExtensionName.equals(".xls")) {
            wb = new HSSFWorkbook(inputStream);
        }
        
        Sheet sheet = wb.getSheet(sheetName);
        
        int rowCount = sheet.getLastRowNum()-sheet.getFirstRowNum();
        List<Object[]> records = new ArrayList<Object[]>();
        for(int i = 1;i<=rowCount;i++) {
            Row row = sheet.getRow(i);
            String fields[] = new String[row.getLastCellNum()];
            for(int j = 0;j<row.getLastCellNum();j++) {
                Cell cell = row.getCell(j);
                cell.setCellType(CellType.STRING);
                String cellValue = row.getCell(j).getStringCellValue();
                fields[j] = cellValue;
            }
            records.add(fields);
        }
        
        Object[][] results = new Object[records.size()][];
        for(int i = 0;i<records.size();i++) {
            results[i] = records.get(i);
        }
        inputStream.close();
        return results;
    }
}
 

 2、writeTestResult.java的代码

package dataDriver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class writeTestResult {
    
    public void writeExcelData(String path,String fileName,String sheetName,List<String> resultObj) throws IOException{
        File file =  new File(path+"\\"+fileName);
        FileInputStream inputStream = new FileInputStream(file);
        
        
        
        Workbook wb = null;
        String fileExtensionName = fileName.substring(fileName.indexOf("."));
        if(fileExtensionName.equals(".xlsx")) {
            wb = new XSSFWorkbook(inputStream);
        }
        else if(fileExtensionName.equals(".xls")) {
            wb = new HSSFWorkbook(inputStream);
        }
        
        Sheet sheet = wb.getSheet(sheetName);
        for(int i = 1;i<=resultObj.size();i++) {
            int cellNum = sheet.getRow(i).getLastCellNum();
            sheet.getRow(i).createCell(cellNum).setCellValue(resultObj.get(i-1).toString());
        }
        FileOutputStream fout = new FileOutputStream(new File("E:\\file\\testResult.xls"));
        wb.write(fout);
        fout.close();
        
        inputStream.close();
    }
}

二、pageObjects包下一个class:LoginPage.java

package pageObjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
    
    @FindBy(xpath="//*[@type='text']")
    public WebElement userName;
    
    @FindBy(xpath="//*[@type='password']")
    public WebElement passWord;
    
    @FindBy(xpath="//*[@type='button']")
    public WebElement loginBut;
    
    public LoginPage(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }

}
 

三、testScripts包下一个class:TestLogin.java

package testScripts;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import dataDriver.GetTestData;
import dataDriver.writeTestResult;
import pageObjects.LoginPage;

public class TestLogin {
    
    private WebDriver driver;
    
    private String baseUrl = "http://supplier.pms.com/supplier/login";
    List<String> resultList = new ArrayList<String>();
    
    @DataProvider(name="loginData")
    private static Object[][] getLoginData() throws IOException {
        GetTestData testData = new GetTestData();
        return testData.getExcelData("E:\\", "file\\login1.xls", "sheet1");
    }
    
    @Test(dataProvider="loginData")
    public void testLogin(String rowNum,String loginName,String passWord) throws InterruptedException {
        driver.get(baseUrl);
        LoginPage loginPage = new LoginPage(driver);
        
        loginPage.userName.sendKeys(loginName);
        loginPage.passWord.sendKeys(passWord);
        loginPage.loginBut.click();
        Thread.sleep(1000);
        if(driver.getPageSource().contains("登录信息")) {
            System.out.println(rowNum+":-----------Success------------");
            resultList.add("success");
        }else {
            System.out.println(rowNum+":Password or LoginName is error");
            resultList.add("Fail");
        }
    }
    
    @BeforeMethod
    public void beforeMethod() {
        System.setProperty("webdriver.gecko.driver", "D:\\selenium-java-3.4.0\\geckodriver.exe");
          //指定浏览器安装路径
        System.setProperty("webdriver.firefox.bin","D:\\firefox-52.0.win64.sdk\\firefox-sdk\\bin\\firefox.exe");
        driver = new FirefoxDriver();
    }
    
    @AfterMethod
    public void afterMethod() throws IOException {
        driver.quit();
    }
    
    @AfterClass
    public void after() throws IOException {
        writeTestResult writeResult = new writeTestResult();
        writeResult.writeExcelData("E:\\", "file\\login1.xls", "sheet1", resultList);
    }   

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值