I'm writing Selenium code in Java. And below is one of the methods.
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();
}
Basically there are 2 values used in this program: dBName
and pubName
.
Here what the program does.
- enter
dBName
in a text area and get the result and store it in variabledbPaceNumber
. - hit on search and again do the same but this time enter
pubName
and store the value in variablepubPaceNumber
- Compare these two variables and see if they are same, if so, enter the result in Excel Cell else concatenate these two values and store it in Excel Cell.
What needs work
The below block of code is repeated once for pubName
and once for dBName
. I want to know if I can make a common block for both cases and reuse it. Is there a way that I can make a single block to check once for pubName
and once for 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"); }