优化方法块

时间:2015-11-10 09:28:44

标签: java selenium optimization

我正在用Java编写Selenium代码。以下是方法块之一。

private static void getPaceNumber(WebDriver chromeDriver, String dBName, XSSFSheet paceSheet, String pubName, int i,
        XSSFCell cell, XSSFWorkbook workbook) throws Exception {
    System.out.println("DBID is " + dBName + " and fpn is " + pubName);
    CellStyle style = workbook.createCellStyle();
    int defaultHeight = paceSheet.getRow(0).getHeight();
    cell = paceSheet.getRow(i).createCell(1);
    paceSheet.getRow(i).setHeight((short) (defaultHeight * 2));
    if (dBName == "" || dBName.equals("null")) {
        System.out.println("Null Block");
        cell.setCellValue("N/A");
    } else {
        chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[2]/td[2]/textarea"))
                .sendKeys("\"" + dBName + "\"");
        chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[4]/td[2]/input[1]")).click();
        // Thread.sleep(500L);
        System.out.println("entered second block");
        List<WebElement> pace = chromeDriver
                .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"));
        int paceSize = pace.size();
        System.out.println("pace size is " + paceSize);
        int pubPaceNumber = 0;
        int dbPaceNumber;
        if (paceSize >= 1) {
            dbPaceNumber = Integer.parseInt(
                    chromeDriver.findElement(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"))
                            .getAttribute("value"));
            chromeDriver.findElement(By.xpath(".//*[@id='searchPublication']")).click();
            chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[2]/td[2]/textarea"))
                    .sendKeys("\"" + pubName + "\"");
            chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[4]/td[2]/input[1]")).click();

            int paceSizse = chromeDriver
                    .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]")).size();
            if (paceSizse >= 1) {
                pubPaceNumber = Integer.parseInt(
                        chromeDriver.findElement(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"))
                                .getAttribute("value"));
            } else {
                List<WebElement> table = chromeDriver
                        .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[4]/td/b"));
                int tabSize = table.size();
                System.out.println("Tab size is " + tabSize);
                if (tabSize == 1) {
                    chromeDriver.findElement(By.xpath(".//*[@id='searchPublication']")).click();
                    chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[2]/td[2]/textarea"))
                            .sendKeys("\"" + pubName + "\"");
                    chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[4]/td[2]/input[1]"))
                            .click();
                    List<WebElement> paceWithFPN = chromeDriver
                            .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"));
                    int paceWithFPNSize = paceWithFPN.size();
                    if (paceWithFPNSize >= 1) {

                        cell.setCellValue("N/A");
                    } else {
                        cell.setCellValue("N/A");
                    }

                } else {
                    cell.setCellValue("N/A");
                }

            }

            if (dbPaceNumber == pubPaceNumber) {
                cell.setCellValue(dbPaceNumber);
            } else {

                cell.setCellValue(dbPaceNumber + "\n" + pubPaceNumber);
                style.setWrapText(true);
                style.setAlignment(CellStyle.ALIGN_RIGHT);
                cell.setCellStyle(style);
            }

        } else {
            List<WebElement> table = chromeDriver
                    .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[4]/td/b"));
            int tabSize = table.size();
            System.out.println("Tab size is " + tabSize);
            if (tabSize == 1) {
                chromeDriver.findElement(By.xpath(".//*[@id='searchPublication']")).click();
                chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[2]/td[2]/textarea"))
                        .sendKeys("\"" + pubName + "\"");
                chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[4]/td[2]/input[1]"))
                        .click();
                List<WebElement> paceWithFPN = chromeDriver
                        .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"));
                int paceWithFPNSize = paceWithFPN.size();
                if (paceWithFPNSize >= 1) {
                    int paceSubNumber = Integer.parseInt(chromeDriver
                            .findElement(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"))
                            .getAttribute("value"));

                    cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                    cell.setCellValue(paceSubNumber);
                } else {
                    cell.setCellValue("N/A");
                }

            } else {
                cell.setCellValue("N/A");
            }

        }

    }
    chromeDriver.findElement(By.xpath(".//*[@id='searchPublication']")).click();
}

此计划dBNamepubName基本上使用了2个值 这是程序的作用。

  1. 在文本区域输入dBName并获取结果并将其存储在变量`dbPaceNumber中。
  2. 点击搜索并再次执行相同操作,但这一次输入pubName并将值存储在变量pubPaceNumber
  3. 比较这两个变量并查看它们是否相同,如果是,请在Excel中输入结果Cell else连接这两个值并将其存储在Excel Cell中。
  4. 需要什么

    在我的代码中,下面的块重复pubName一次,dBName重复一次,我想知道我是否可以为两者创建一个公共块并使用它。我的意思是,有一种方法可以让一个块为pubName检查一次,为dBName检查一次。

    List<WebElement> table = chromeDriver
                            .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[4]/td/b"));
                    int tabSize = table.size();
                    System.out.println("Tab size is " + tabSize);
                    if (tabSize == 1) {
                        chromeDriver.findElement(By.xpath(".//*[@id='searchPublication']")).click();
                        chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[2]/td[2]/textarea"))
                                .sendKeys("\"" + pubName + "\"");
                        chromeDriver.findElement(By.xpath("html/body/form[2]/b/b/table/tbody/tr[4]/td[2]/input[1]"))
                                .click();
                        List<WebElement> paceWithFPN = chromeDriver
                                .findElements(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"));
                        int paceWithFPNSize = paceWithFPN.size();
                        if (paceWithFPNSize >= 1) {
                            int paceSubNumber = Integer.parseInt(chromeDriver
                                    .findElement(By.xpath("html/body/form[2]/table[1]/tbody/tr[2]/td[2]/input[1]"))
                                    .getAttribute("value"));
    
                            cell.setCellType(Cell.CELL_TYPE_NUMERIC);
                            cell.setCellValue(paceSubNumber);
                        } else {
                            cell.setCellValue("N/A");
                        }
    
                    } else {
                        cell.setCellValue("N/A");
                    }
    

    由于

0 个答案:

没有答案