Selenium Interview Questions (Part 3)
Some Important Codes
To Use .properties File:
Properties p = new Properties();
InputStream stream = new FileInputStream(new File("\\filepath.properties"));
p.load(stream);
driver.findElement(By.id(p.getProperty("EmailAlt")).click();To Use DB:
Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, first, last, age FROM Employees");
while (rs.next()) {
//Retrieve by column name
int id = rs.getInt("id");
int age = rs.getInt("age");
String first = rs.getString("first");
String last = rs.getString("last");
}To Use Excel File:
String FilePath = "d://filepath.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = new XSSFWorkbook(fs);
Sheet sh = wb.getSheet(0); // this is to get the access to Sheet1.
Sheet sh = wb.getSheet(1);
Sheet sh = wb.getSheet("sheet1");
// Using XSSF for xlsx format, for xls use HSSF
int totalNoOfRows = sh.getRows();
int totalNoOfCols = sh.getColumns();
for (int row = 0; row < totalNoOfRows; row++) {
for (int col = 0; col < totalNoOfCols; col++) {
System.out.print(sh.getCell(col, row).getContents() + "\t");
}
System.out.println();
}
//Additional -
String CellGetContent = sh.getCell(0,0).getContents();
System.out.println(CellGetContent);
//OR
Cell Row0Col0 = sheet.getCell(0,0);
Cell Row1Col1 = sheet.getCell(1,1);
String FirstRowFirstColumn = Row0Col0.getContents();
String SecondRowSecondColumn = Row1Col1.getcontents();