This is my code. After view the code, Please give some input to enhance myself.Is this code correct .Give some procedural way to create automation script.Is this code is Ok?
package mytestpackage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
//import com.google.common.base.Verify;
//import com.google.common.base.Function;
import org.testng.asserts.SoftAssert;
public class Login {
WebDriver driver;
WebDriverWait wait;
XSSFWorkbook workbook;
SoftAssert softAssert = new SoftAssert();
XSSFSheet sheet;
XSSFCell cell;
@BeforeTest
public void TestSetup() {
// Set the path of the Firefox driver.
//System.setProperty("webdriver.gecko.driver", "D:\\BALA\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
wait = new WebDriverWait(driver, 25);
}
private XSSFCell getCellValue(int row, int column) {
XSSFCell cell = sheet.getRow(row).getCell(column, Row.CREATE_NULL_AS_BLANK);
return cell;
}
public void ExcelRead() throws IOException {
File src = new File("D:\\BALA\\TestData1.xlsx");
FileInputStream finput = new FileInputStream(src);
workbook = new XSSFWorkbook(finput);
}
@Test(priority = 1)
public void Signin() throws IOException, InterruptedException {
ExcelRead();
sheet = workbook.getSheetAt(0);
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
try {
cell = getCellValue(i, 1);
String cellValue = getCellValueAsString(cell);
wait.until(ExpectedConditions.elementToBeClickable(By.id("LG")));
driver.findElement(By.id("LG")).clear();
driver.findElement(By.id("LG")).sendKeys(cellValue);
cell = getCellValue(i, 2);
String cellValue1 = getCellValueAsString(cell);
// String rawValue = cell.getRawValue();
// driver.findElement(By.id("PWD")).clear();
driver.findElement(By.id("PWD")).sendKeys(cellValue1);
driver.findElement(By.id("btnSubmit")).click();
String actualTitle = driver.getTitle();
String expectedTitle = "IntouchOnDemand: Menu";
softAssert.assertEquals(actualTitle, expectedTitle);
} catch (Exception e) {
softAssert.assertTrue(false);
}
}
}
public static String getCellValueAsString(Cell cell) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
return cell.getRichStringCellValue().getString();
case Cell.CELL_TYPE_NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
return cell.getDateCellValue().toString();
// return cell.getDateCellValue().toString();
} else {
return cell.getNumericCellValue() + "";
// return cell.getNumericCellValue();
}
case Cell.CELL_TYPE_BOOLEAN:
return ((Boolean) cell.getBooleanCellValue()).toString();
case Cell.CELL_TYPE_FORMULA:
return cell.getCellFormula().toString();
}
return null;
}
/*@Test(priority = 2)
public void Logout() throws IOException, InterruptedException {
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("a.dropdown-toggle.ng-binding")));
driver.findElement(By.cssSelector("a.dropdown-toggle.ng-binding")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.glyphicon.glyphicon-off")));
driver.findElement(By.cssSelector("span.glyphicon.glyphicon-off")).click(); }*/
@AfterTest
public void tearDown() throws Exception {
driver.close();
softAssert.assertAll();
}
}