无法使用Selenium Webdriver写入Excel

时间:2019-01-09 11:47:00

标签: java excel eclipse selenium-webdriver automated-tests

我创建了一个测试,可以从excel读取并遍历工作表以处理Web门户中的应用程序。一切正常。

但是,我现在试图将网页的结果写到同一Excel上的另一张表中。我正在运行的测试用例在Eclipse中通过,但是没有数据写入指定的工作表中。 (我还希望在结果表上进行迭代以捕获多个应用程序记录,但尚未涉及到该部分)。

请在下面查看我的测试脚本以及在ExcelConfig实用程序表中创建的方法。希望有人能告诉我我要去哪里错了,

史蒂夫

测试用例

package com.htb.puma.uatTests;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import com.htb.puma.pages.SMcaseHeader;
import com.htb.puma.pages.SMhome;
import com.htb.puma.pages.SMloanDetails;
import com.htb.puma.pages.SMlogin;
import com.htb.puma.pages.SetUpConfig;
import com.htb.puma.util.ExcelConfig;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoAlertPresentException;
import java.io.File;
import java.io.IOException;

public class THAM_WRITE_Test {

    WebDriver driver;

    @Test
    public void specMortHome() throws NoAlertPresentException, InterruptedException, IOException {

        // calling drivers from the SetUpConfig page

        driver = SetUpConfig.getChromeDriver();
        // driver = SetUpConfig.getFirefoxDriver();
        // driver = SetUpConfig.getIEDriver();

        String path = new File("src/test/resources/TestData.xlsx").getAbsolutePath();

        ExcelConfig excel = new ExcelConfig(path);

        int row = 1;
        while (excel.getData("PDFRollUp", row, 0) != "") {

            String loanAmReq = excel.getNumericData("PDFRollUp", row, 6);

            // LOGIN

            SMlogin specMortLogin = new SMlogin(driver);
            specMortLogin.openSMlogin();
            specMortLogin.maximiseWindow();

            specMortLogin.enterUsername("OpsAdminAuto");
            specMortLogin.enterPassword("AutoOps123!");
            specMortLogin.clickSignInBtn();

            Thread.sleep(2000);

            SMhome specMortHome = new SMhome(driver);

            specMortHome.clickTopMC();

            Thread.sleep(2000);

            SMcaseHeader specMortCaseHeader = new SMcaseHeader(driver);

            specMortCaseHeader.clickLoanDetailsTab();

            SMloanDetails specMortLoanDetails = new SMloanDetails(driver);

            Thread.sleep(2000);

            specMortLoanDetails.enterLoanAmReq(Keys.CONTROL + "a"); // PDF
            specMortLoanDetails.enterLoanAmReq(loanAmReq); // PDF

            String erc = specMortLoanDetails.getERC();
            String ltv = specMortLoanDetails.getLTV();

            excel.createFile("src/test/resources/TestData.xlsx");
            excel.writeStringData("Results", 1, 1, erc);
            excel.writeStringData("Results", 1, 2, ltv);

            specMortHome.clickUserActionsHomeLM();
            specMortHome.clickLogoutHomeLM();

            row++;

        }
        driver.quit();
    }

}

Excel Config

package com.htb.puma.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;

public class ExcelConfig {

    public XSSFWorkbook wb;
    XSSFSheet sheet1;

    public ExcelConfig(String Excelpath) {

        File src = new File(Excelpath);
        try {
            FileInputStream fis = new FileInputStream(src);
            wb = new XSSFWorkbook(fis);

        } catch (Exception e) {
            System.out.println("Excel file not loaded");
        }

    }

    // reads the string in the excel file
    public String getData(String sheetName, int row, int column) {
        sheet1 = wb.getSheet(sheetName);
        String data = "";

        try {
            data = sheet1.getRow(row).getCell(column).getStringCellValue();
        } catch (NullPointerException e) {
            data = "";
        }
        return data;
    }

    // reads the number in the excel file
    public String getNumericData(String sheetName, int row, int column) {

        sheet1 = wb.getSheet(sheetName);
        String data = "";

        try {
            // data = sheet.getRow(row).getCell(column).getRawValue();
            DataFormatter dataFormatter = new DataFormatter();
            Cell cell = sheet1.getRow(row).getCell(column);
            data = dataFormatter.formatCellValue(cell);
        } catch (NullPointerException e) {
            data = "";
        }
        return data;
    }

//write string data into excel
public void writeStringData(String sheetName, int row, int column, String data) {
    sheet1 = wb.getSheet(sheetName);

    Row valueRow = sheet1.getRow(row);
    Cell valueCell = valueRow.createCell(column);

    if (data.equals("FAILED")) {
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        //font.setColor(HSSFColor.RED.index);
        style.setFont(font);
        valueCell.setCellValue(data);
        valueCell.setCellStyle(style);
    }

    valueCell.setCellValue(data);

}

//creates an excel file
public void createFile(String path) {
    File src = new File(path);
    try {
        FileOutputStream outputStream = new FileOutputStream(src);
        try {
            wb.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

}

1 个答案:

答案 0 :(得分:1)

write(java.io.OutputStream流)用于将数据写入excel文件

spring.datasource.hikari.maximum-pool-size

}