request : read data from excel and feed into website.

113 views
Skip to first unread message

sapsdkar...@gmail.com

unread,
Apr 12, 2017, 1:40:43 AM4/12/17
to Selenium Users
Dear All,

I have a requirement to fetch data (user name , password) from the excel and feed into the website. I'm getting the below java null point exception, and im unable to sort out the issue.

Your response/feedback would be greatly appreciated as I need to close this reqmt in next couple of days.

Please help!


Code below:

package webExcel;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testng.Assert;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import Lib.ExcelDataConfig;

public class exceltosite {
WebDriver driver;

@Test(dataProvider = "loginAuthen")
public void Logsite(String sUsername, String sPassword) throws InterruptedException
{
driver = new FirefoxDriver();
System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Users\\karajagopalan\\AppData\\Local\\Mozilla Firefox\\firefox.exe"); //This is the location where you have installed Firefox on your machine
// FirefoxDriver driver = new FirefoxDriver(options);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
driver.findElement(By.name("email")).clear();
driver.findElement(By.id("email")).sendKeys(sUsername);;
driver.findElement(By.name("passwd")).clear();
driver.findElement(By.id("passwd")).sendKeys(sPassword);;
driver.findElement(By.id("SubmitLogin")).click();
Thread.sleep(5000);
Assert.assertTrue(driver.getTitle().contains("MY ACCOUNT"), "User is not able to login succesfully");
System.out.println("Page verified - User is able to login");
driver.close();

}

@DataProvider(name = "loginAuthen")
public Object[][] passData()
{
ExcelDataConfig config=new ExcelDataConfig("C:\\Users\\Karthik\\Desktop\\log.xls");
int rows=config.getRowCount(0);
Object [][] data=new Object[rows][2];

for (int i=0; i<rows; i++)
data[i][0]=config.getData(0, i, 0);
data[i][1]=config.getData(0, i, 1);
}
return data;


@AfterSuite
public void closeBrowser(){
driver.quit();
}
}

--

package Lib;

import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExcelDataConfig{
HSSFWorkbook wb;
HSSFSheet sheet1;
public ExcelDataConfig (String excelPath)
{
try {
File src = new File(excelPath);
FileInputStream fis = new FileInputStream(src);
wb=new HSSFWorkbook(fis);
sheet1=wb.getSheetAt(0);
}  
catch (Exception e) {
System.out.println(e.getMessage());
}

}


public String getData(int sheetNumber, int row, int column)

{
sheet1=wb.getSheetAt(sheetNumber);
String data = sheet1.getRow(row).getCell(column).getStringCellValue();
return data;
}


/* public Object getData(int i, int i2, int column) {
// TODO Auto-generated method stub
return null;
}*/


public int getRowCount(int sheetIndex) {

    int row = wb.getSheetAt(sheetIndex).getLastRowNum();
    row = row + 1;
    return row;
}
}

Exception:

[Utils] [ERROR] [Error] java.lang.NullPointerException
at Lib.ExcelDataConfig.getData(ExcelDataConfig.java:34)
at webExcel.exceltosite.passData(exceltosite.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:55)
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:115)
at org.testng.internal.Parameters.handleParameters(Parameters.java:509)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1308)
at org.testng.internal.Invoker.createParameters(Invoker.java:1036)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1126)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

SKIPPED: Logsite
java.lang.RuntimeException: java.lang.NullPointerException
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:49)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:115)
at org.testng.internal.Parameters.handleParameters(Parameters.java:509)
at org.testng.internal.Invoker.handleParameters(Invoker.java:1308)
at org.testng.internal.Invoker.createParameters(Invoker.java:1036)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1126)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:744)
at org.testng.TestRunner.run(TestRunner.java:602)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
at org.testng.SuiteRunner.run(SuiteRunner.java:289)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
at org.testng.TestNG.runSuites(TestNG.java:1144)
at org.testng.TestNG.run(TestNG.java:1115)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)
Caused by: java.lang.NullPointerException
at Lib.ExcelDataConfig.getData(ExcelDataConfig.java:34)
at webExcel.exceltosite.passData(exceltosite.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:55)
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
... 22 more

Kind regards,
Karthik

praveen kumar cherukuri

unread,
Apr 12, 2017, 2:09:03 AM4/12/17
to seleniu...@googlegroups.com
Hi Karthik, 

Could you please share the excel sheet as well, so it gives an idea what could have gone wrong. 
Please make sure you change the actual credentials to some dummy data before sharing the excel sheet. 

I guess there might be a very small mistake you are encountering while extracting the data. 

Please share 1) Excel sheet used 2) java code in .java file 



photo 
Praveen Kumar Cherukuri
Test Automation ExpertQA-Masters 
Trai...@qa-masters.com | www.qa-masters.com | Skype: QA-masters |
Experts in QA Training | Technical Project Support | Trainers Outsourcing | LinkedIn | Facebook |

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-users+unsubscribe@googlegroups.com.
To post to this group, send email to selenium-users@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/selenium-users/11583fbf-07bd-4000-858c-eaacf9721951%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sapsdkar...@gmail.com

unread,
Apr 12, 2017, 11:59:16 PM4/12/17
to Selenium Users
Hi Praveen,

First of all, thanks for your prompt response. 

I was able to resolve the issue.  There was an issue with unwanted object being declared, and it has been sorted out.

However, I have posted a new 'WebDriver unable to read numeric value 'Cannot get a text value from a numeric cell'' topic in the forum.  Would you be able to shed some light on this please?

Kind regards,
Karthik
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages