簡體   English   中英

如何使用JXL覆蓋以前的Excel文件?

[英]How to overwrite in my previous excel file using JXL?

我想設置執行測試的狀態,但是無論何時執行代碼,它都會使用以下行創建新工作簿:

wwbCopy = Workbook.createWorkbook(new File(testSuitePath));

現在任何人都可以讓我知道如何設置測試用例的狀態。

為此,我正在使用JXL lib。 並在關鍵驅動的框架中。

以下是在excel中編寫的代碼:

 public class MainClass {
    private static final String BROWSER_PATH = "D:\\softs\\FF installed\\FF18\\firefox.exe";
    private static final String TEST_SUITE_PATH = "D:\\GmailTestSuite.xls";
    private static final String OBJECT_REPOSITORY_PATH = "D:\\objectrepository.xls";
    private static final String ADDRESS_TO_TEST = "https://www.gmail.com";

    private WebDriver driver;
    private Properties properties;

    public MainClass() {
        File file = new File(BROWSER_PATH);
        FirefoxBinary fb = new FirefoxBinary(file);
        driver = new FirefoxDriver(fb, new FirefoxProfile());
        driver.get(ADDRESS_TO_TEST);
    }

    public static void main(String[] args) throws Exception {
        MainClass main = new MainClass();

        main.handleTestSuite();
    }

    private void handleTestSuite() throws Exception {
        ReadPropertyFile readConfigFile = new ReadPropertyFile();
        properties = readConfigFile.loadPropertiess();
        ExcelHandler testSuite = new ExcelHandler(TEST_SUITE_PATH, "Suite");
        testSuite.columnData();

        int rowCount = testSuite.rowCount();
        System.out.println("Total Rows=" + rowCount);

        for (int i = 1; i < rowCount; i++) {
            String executable = testSuite.readCell(
                    testSuite.getCell("Executable"), i);
            System.out.println("Executable=" + executable);

            if (executable.equalsIgnoreCase("y")) {
                // exe. the process
                String scenarioName = testSuite.readCell(
                        testSuite.getCell("TestScenario"), i);
                System.out.println("Scenario Name=" + scenarioName);
                handleScenario(scenarioName);
            }
        }
        WritableData writableData= new WritableData(TEST_SUITE_PATH,"Login");
        writableData.shSheet("Login", 5, 1, "Pass");
        writableData.shSheet("Login", 5, 2, "Fail");
        writableData.shSheet("Login", 5, 3, "N/A");
    }

    private void handleScenario(String scenarioName) throws Exception {
        ExcelHandler testScenarios = new ExcelHandler(TEST_SUITE_PATH);
        testScenarios.setSheetName("Login");
        testScenarios.columnData();
        int rowWorkBook1 = testScenarios.rowCount();
        for (int j = 1; j < rowWorkBook1; j++) {
            String framWork = testScenarios.readCell(
                    testScenarios.getCell("FrameworkName"), j);
            String operation = testScenarios.readCell(
                    testScenarios.getCell("Operation"), j); // SendKey
            String value = testScenarios.readCell(
                    testScenarios.getCell("Value"), j);
            System.out.println("FRMNameKK=" + framWork + ",Operation="
                    + operation + ",Value=" + value);

            handleObjects(operation, value, framWork);
        }
    }

    private void handleObjects(String operation, String value, String framWork)
            throws Exception {
        System.out.println("HandleObject--> " + framWork);
        ExcelHandler objectRepository = new ExcelHandler(
                OBJECT_REPOSITORY_PATH, "OR");
        objectRepository.columnData();
        int rowCount = objectRepository.rowCount();
        System.out.println("Total Rows in hadleObject=" + rowCount);

        for (int k = 1; k < rowCount; k++) {
            String frameWorkName = objectRepository.readCell(
                    objectRepository.getCell("FrameworkName"), k);
            String ObjectName = objectRepository.readCell(
                    objectRepository.getCell("ObjectName"), k);
            String Locator = objectRepository.readCell(
                    objectRepository.getCell("Locator"), k); // SendKey

            System.out.println("FrameWorkNameV=" + frameWorkName
                    + ",ObjectName=" + ObjectName + ",Locator=" + Locator);

            if (framWork.equalsIgnoreCase(frameWorkName)) {
                operateWebDriver(operation, Locator, value, ObjectName);

            }
        }
    }

    private void operateWebDriver(String operation, String Locator,
            String value, String objectName) throws Exception {
        System.out.println("Operation execution in progress");
        WebElement temp = getElement(Locator, objectName);
        if (operation.equalsIgnoreCase("SendKey")) {
            temp.sendKeys(value);
        }

        Thread.sleep(1000);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        if (operation.equalsIgnoreCase("Click")) {
            temp.click();
        }

        if (operation.equalsIgnoreCase("Verify")) {
            System.out.println("Verify--->" +temp);
            temp.isDisplayed();
        }
    }

    public WebElement getElement(String locator, String objectName)
            throws Exception {
        WebElement temp = null;
        System.out.println("Locator-->" + locator);
        if (locator.equalsIgnoreCase("id")) {
            temp = driver.findElement(By.id(objectName));

        } else if (locator.equalsIgnoreCase("xpath")) {
            temp = driver.findElement(By.xpath(objectName));
            System.out.println("xpath temp ----->" + temp);
        } else if (locator.equalsIgnoreCase("name")) {
            temp = driver.findElement(By.name(objectName));
        }
        return temp;
    }
}

公共類WritableData {

Workbook wbook;
WritableWorkbook wwbCopy;
String ExecutedTestCasesSheet;
WritableSheet shSheet;

public WritableData(String testSuitePath, String OBJECT_REPOSITORY_PATH) throws RowsExceededException, WriteException {
    // TODO Auto-generated constructor stub


     try { wbook = Workbook.getWorkbook(new File(testSuitePath)); 
     wwbCopy= Workbook.createWorkbook(new File(testSuitePath),wbook);
     System.out.println("writable workbookC-->" +wwbCopy);
     shSheet=wwbCopy.getSheet("Login");
     System.out.println("writable sheetC-->" +shSheet); 
     //shSheet = wwbCopy.createSheet("Login", 1); 
     } catch (Exception e) { 
         // TODO: handle exception 
         System.out.println("Exception message" + e.getMessage()); e.printStackTrace(); } }

      public void shSheet(String strSheetName, int iColumnNumber, int
      iRowNumber, String strData) throws WriteException, IOException { 
          // TODO Auto-generated method stub

      WritableSheet wshTemp = wwbCopy.getSheet(strSheetName);
      shSheet=wwbCopy.getSheet("Login"); 

      WritableFont cellFont = null;
      WritableCellFormat cellFormat = null;

      if (strData.equalsIgnoreCase("PASS")) { cellFont = new
      WritableFont(WritableFont.TIMES, 12);
      cellFont.setColour(Colour.GREEN);
      cellFont.setBoldStyle(WritableFont.BOLD);

      cellFormat = new WritableCellFormat(cellFont);
      cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); }

      else if (strData.equalsIgnoreCase("FAIL")) { cellFont = new
      WritableFont(WritableFont.TIMES, 12); cellFont.setColour(Colour.RED);
      cellFont.setBoldStyle(WritableFont.BOLD);

      cellFormat = new WritableCellFormat(cellFont);
      cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN); }

      else { cellFont = new WritableFont(WritableFont.TIMES, 12);
      cellFont.setColour(Colour.BLACK);

      cellFormat = new WritableCellFormat(cellFont);
      cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
      cellFormat.setWrap(true); }

      Label labTemp = new Label(iColumnNumber, iRowNumber, strData,
      cellFormat);
      shSheet.addCell(labTemp); 
      System.out.println("writableSheet---->"+shSheet);

      wwbCopy.write(); 
      wwbCopy.close();
      wbook.close();

}}

您可以通過復制現有的Excel,然后根據需要進行修改來創建Excel。

    Workbook existingWorkbook = Workbook.getWorkbook(new File(Constant.wBook));
    WritableWorkbook workbookCopy = Workbook.createWorkbook(new File(Constant.wBook), existingWorkbook);
    WritableSheet sheetToEdit = workbookCopy.getSheet("report");

    int step_num = col + 1;


    Label lb = new Label(0, col, Integer.toString(step_num));
    Label lb1 = new Label(1, col, text);
    Label lb2 = new Label(2, col, result);


    System.out.println(text + "" + result + "");

    sheetToEdit.addCell(lb);
    sheetToEdit.addCell(lb1);
    sheetToEdit.addCell(lb2);


    workbookCopy.write();
    workbookCopy.close();
    existingWorkbook.close();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM