I am completely new to selenium as well as Java coding and I am trying to execute excel based login script -- Selenium webdriver java. I am having 2 problems :
- I am not able to iterate for loop to read all the records, only 1st row data is correctly processed and then gives nullpointerexception
- And my password for all the account is 123456 but selenium is passing it as 123456.0 and I am getting error due to this.
If any one has better scripts which actually run that is also acceptable since I have searched many code but I am not able to run then.
Below is my code :
public class Excel_Read_Write {
public static void main(String[] args)throws Exception
{
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// To Maximize browser screen
// FileInputStream fs = new FileInputStream("C:\\LoginDetails.xls");
// driver.manage().window().maximize();
String baseUrl = "http://openspace.website/vpms/public/";
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// String filePath = System.getProperty("user.dir")+"\\src\\excel_read_write";
String filePath = System.getProperty("user.dir")+"\\src";
// FileInputStream fs = new FileInputStream(filePath);
String FilePath = "F:\\\\LoginDetails.xlsx";
XSSFWorkbook wb = new XSSFWorkbook(FilePath);
XSSFSheet s1 = wb.getSheetAt(0);
int totalNoOfRows = s1.getLastRowNum();
int noOfColumns = s1.getRow(0).getPhysicalNumberOfCells();
System.out.println(noOfColumns);
// int totalNoOfCols = s1.getLastCellNum();
System.out.println(noOfColumns);
XSSFCell username,password;
for(int i = 1; i < totalNoOfRows ; i++) {
for (int j=0; j < noOfColumns - 1; j++) {
username= s1.getRow(1).getCell(j);
password = s1.getRow(1).getCell(i);
System.out.println("Username"+username);
System.out.println("Password"+password);
driver.findElement(By.id("email")).clear();
driver.findElement(By.id("password")).clear();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.id("email")).sendKeys(username.toString());
driver.findElement(By.id("password")).sendKeys(password.toString());
driver.findElement(By.xpath("/html/body/div/div/div[3]/section/form/div[3]/button")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
if ((driver.getPageSource().contains("Check In"))){
System.out.println("Successful Login verified for " +username.toString());
driver.findElement(By.xpath("//*[@id=\"bs-example-navbar-collapse-1\"]/ul/li[4]/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
} else {
System.out.println("Successful Login not verified for " +username.toString());
}
}
}
Error message displayed :
Starting ChromeDriver 2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1) on port 29074
Only local connections are allowed.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Jan 09, 2018 12:28:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
3
3
UsernameQA Master
Password1.0
Successful Login not verified for QA Master
Username1.0
Password1.0
Successful Login not verified for 1.0
UsernameQA Master
Passwordnull
Exception in thread "main" java.lang.NullPointerException
at excel_read_write.Excel_Read_Write.main(Excel_Read_Write.java:69)