package excellogin;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFRow;
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.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class excellogin {
#Test(dataProvider = "data")
public static void login(String username, String password) throws Exception {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.synctag.com/");
driver.manage().window().maximize();
driver.findElement(By.id("login_web")).click();
driver.findElement(By.id("emailid")).sendKeys("username");
driver.findElement(By.id("password")).sendKeys("password");
}
#DataProvider (name = "data")
public Object[][] datasupply() throws Exception{
File f = new File("D:\\DataProvider_Login\\Book1.xlsx");
FileInputStream fis = new FileInputStream(f);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow Row = sheet.getRow(0);
int row = sheet.getLastRowNum();
int col = Row.getLastCellNum();
Object[][] testdata1 = new Object[row][col];
for(int i=0;i<=sheet.getLastRowNum();i++){
for(int j=0;j<=col;j++){
testdata1[i][j] = sheet.getRow(i).getCell(j).toString();
}
}
wb.close();
return testdata1;
}
}
I am getting the below error in my code
[RemoteTestNG] detected TestNG version 6.14.2
[Utils] [ERROR] [Error] java.lang.NullPointerException at excellogin.excellogin.datasupply(excellogin.java:43)
Help me resolve this error.
SKIPPED: login
java.lang.RuntimeException: java.lang.NullPointerException
at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:49)
at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:131)
at org.testng.internal.Parameters.handleParameters(Parameters.java:706)
Related
The Selenium Webdriver, TestNG version & used libraries are given below.
Selenium version : selenium-server-standalone-3.41.59.jar
TestNG version : 7.4.0
Tried to login with Excel sheet data provider.But no class found error getting.
Added Apache POI excel related dependenices
Added Libraries(Please see the screenshot)
Here is my code.
The Selenium Webdriver, TestNG version & used libraries are given below.
Selenium version : selenium-server-standalone-3.41.59.jar TestNG version : 7.4.0
package ui;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class MultiUserLogin {
public static WebDriver driver;
public WebDriverWait wait;
String appURL1 = "https://www.google.com";
#BeforeClass
public void testSetup() {
System.setProperty("webdriver.gecko.driver","E:/Geckdriver/geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().window().maximize();
wait = new WebDriverWait(driver, 5);
}
#Test(dataProvider="LoginData")
public void VerifyValidLogin(String Username, String Password,String Company) {
driver.navigate().to(appURL1);
driver.findElement(By.id("userId")).sendKeys(Username);
driver.findElement(By.id("password")).sendKeys(Password);
driver.findElement(By.id("company")).sendKeys(Company);
driver.findElement(By.linkText("Login")).click();
}
#DataProvider(name="LoginData")
public Object[][] LoginData() throws Exception {
Object[][] testObjArray = ExcelUtils.getTableArray("E:\\TestNG\\TestData\\TestData.xlsx","Sheet1");
return (testObjArray);
}
}
package ui;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static XSSFSheet ExcelWSheet;
private static XSSFWorkbook ExcelWBook;
private static XSSFCell Cell;
private static XSSFRow Row;
public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {
String[][] tabArray = null;
try {
FileInputStream ExcelFile = new FileInputStream(FilePath);
// Access the required test data sheet
ExcelWBook = new XSSFWorkbook(ExcelFile);
ExcelWSheet = ExcelWBook.getSheet(SheetName);
int startRow = 1;
int startCol = 1;
int ci,cj;
int totalRows = ExcelWSheet.getLastRowNum();
// function as well to get Column count
int totalCols = 2;
tabArray=new String[totalRows][totalCols];
ci=0;
for (int i=startRow;i<=totalRows;i++, ci++) {
cj=0;
for (int j=startCol;j<=totalCols;j++, cj++){
tabArray[ci][cj]=getCellData(i,j);
System.out.println(tabArray[ci][cj]);
}
}
}
catch (FileNotFoundException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
catch (IOException e){
System.out.println("Could not read the Excel sheet");
e.printStackTrace();
}
return(tabArray);
}
public static String getCellData(int RowNum, int ColNum) throws Exception {
try{
Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
CellType dataType= Cell.getCellType();
if (dataType == CellType.STRING) {
return "";
}else{
String CellData = Cell.getStringCellValue();
return CellData;
}
}catch (Exception e){
System.out.println(e.getMessage());
throw (e);
}
}
}
I am trying to do data driven testing and I am not able to figure it out why I am getting data mismatch. I am trying to automate just usernames and that is in form of string. Can you please review my code and tell me what I am doing wrong?
Below are two files. one is my main method file and one is my excel config file. I have also attached screenshot of my error. Any help would be appreciated. Thank you.
File1:
`
package loginAdmin;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class PersonateUser {
#Test(dataProvider="testdata")
public void login(String username) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver",
C:\\Users\\abc\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(200, TimeUnit.SECONDS);
driver.get("www.abc.com/AdminHome.aspx");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtLoginName")).
sendKeys("admin");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_txtPassword")).
sendKeys("Password");
driver.findElement(By.id("M_layout_content_PCDZ_MW2NO7V_ctl00_webInputForm_cmdContinue")).
click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_lblUserName")).
click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_txtUserName")).
sendKeys(username);
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_btnSearch")).click();
driver.findElement(By.id("M_layout_content_PCDZ_M5QH8YG_ctl00_resultsGrid_ctl02_LogInAsUser")).
click();
System.out.println("User is able to login successfully");
driver.findElement(By.xpath("/html/body/form/div[3]/div[3]/div[1]/div[1]/div/div[5]/ul/li[6]/a")).
click();
#DataProvider(name="testdata")
public Object[][] TestDataFeed()
{
ReadExcelFile config = new
ReadExcelFile("C:\\Users\\abc\\eclipseworkspace\\Login\\testdata\\testdata.xlsx");
int rows = config.getRowCount(0);
Object[][] credentials = new Object[rows][2];
for(int i = 0; i < rows; i++)
{
credentials[i][0] = config.getData(0, i, 0);
}
return credentials;
}
}
`
File 2:
` package loginAdmin;
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcelFile
{
XSSFWorkbook wb;
XSSFSheet sheet;
public ReadExcelFile(String excelPath)
{
try
{
File src = new File(excelPath);
FileInputStream fis = new FileInputStream(src);
wb =new XSSFWorkbook(fis);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public String getData(int sheetnumber, int row, int column)
{
sheet= wb.getSheetAt(sheetnumber);
String data = sheet.getRow(row).getCell(column).getStringCellValue();
return data;
}
public int getRowCount(int sheetIndex)
{
int row = wb.getSheetAt(sheetIndex).getLastRowNum();
row = row + 1;
return row;
}
}
`
Error Screenshot:
Excel File:
You are declaring Object[][] credentials = new Object[rows][2]; but you fill only one column keeping other column empty (null). Hence the dimension of your array does not match the number of arguments your method accepts.
Fix it with changing that line to:
Object[][] credentials = new Object[rows][1];
I am trying to write title and price into excel file. I am creating columns but java game me error 'NULL POINTER EXCEPTION' at line number 48, Please help me what is main reason.But if i write at line no 48 ,sheet1.getRow(0).createCell(0).getStringCellValue('Naqash');Then no Null pointer error is showing.
package codeclasses;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
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.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.Test;
public class ReadExcel {
List<WebElement> title, prices;
#Test
public void test() throws IOException {
System.setProperty("webdriver.chrome.driver", "h:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://themeforest.net/search/education?referrer=homepage&utf8=%E2%9C%93");
title = driver.findElements(By.xpath("//h3[#class = 'product-list__heading']/a"));
prices = driver.findElements(By.xpath("//p[#class='product-list__price-desktop']"));
File src = new File("./file/Book1.xlsx");
FileInputStream file = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet1 = wb.getSheetAt(0);
for (int i = 0; i < 30; i++) {
int j = 0;
if(sheet1.getRow(i+1)==null){
(48) sheet1.getRow(i+2).createCell(j).setCellValue("Naqash");
sheet1.getRow(i+2).createCell(j+1).setCellValue("Zafar");
}
else{
System.out.println("Cant find the scene");
}
FileOutputStream fileout = new FileOutputStream(src);
wb.write(fileout);
}
}
}
Try to create the row and cell before you insert data into it. For example:
int rowIndex = 0;
int columnIndex = 0;
Row row = sheet1.createRow(rowIndex);
Cell cell = row.createCell(columnIndex);
cell.setCellValue("Naqash");
You can get row by getRow(index) if you have already created it before
The issue is that you never test if the cell is null!
if (cell == null)
{
System.out.println("Cell is Empty in Column:" + cols);
}
else if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING)
{
//code
}
As a general matter, you should be careful while handling Cell.getCellType() function, since an empty cell could be either null or be a CELL_TYPE_BLANK.
I hope it helped.
The below code is fetching the emailId and password value from the source file and printing it on the console, but only pasting the emailId value in the web page, but not the password.
I am getting an error message as
FAILED: doLogin("gaurav_thantry#yahoo.com", "password1")
org.openqa.selenium.StaleElementReferenceException: stale element
reference: element is not attached to the page document
This is my code :
package excelSelenium;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
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.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class seleniumIntg {
XSSFWorkbook workbook = null;
XSSFSheet sheet = null;
XSSFRow row = null;
XSSFCell cell = null;
WebDriver driver = null;
#Test(dataProvider = "getData")
public void doLogin(String username, String password)
{
System.setProperty("webdriver.chrome.driver","C://testing/chromedriver_win32/chromedriver.exe" );
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://login.yahoo.com/config/login?.src=fpctx&.intl=in&.lang=en-IN&.done=https://in.yahoo.com/%3fp=us");
driver.findElement(By.xpath("//input[#id='login-username']")).sendKeys(username);
driver.findElement(By.xpath("//*[#id='login-signin']")).click();
driver.findElement(By.xpath("//*[#id='login-passwd']")).sendKeys(password);
}
#DataProvider
public Object[][] getData() throws IOException
{
FileInputStream fis = new FileInputStream("C://Users/Gaurav/Documents/testid.xlsx");
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheet("sheet1");
int rowCount = sheet.getFirstRowNum()+sheet.getLastRowNum()+1;
int colCount = sheet.getRow(0).getLastCellNum();
System.out.println("Row count is:" +rowCount+ "Col count is:" +colCount);
Object[][] data = new Object[rowCount-1][colCount];
for(int rNum = 2; rNum<=rowCount; rNum++)
for(int cNum = 0; cNum<colCount; cNum++)
{
System.out.println(getCellData("sheet1",cNum,rNum));
data[rNum-2][cNum]=getCellData("sheet1",cNum,rNum);
}
return data;
}
public String getCellData(String sheetName, int colNum, int rowNum)
{
try{
if(rowNum<=0)
return "";
int index = workbook.getSheetIndex(sheetName);
if(index == -1)
return "";
sheet =workbook.getSheetAt(index);
row = sheet.getRow(rowNum-1);
if(row==null)
return "";
cell = row.getCell(colNum);
if(cell==null)
return "";
else if(cell.getCellTypeEnum()==CellType.STRING)
return cell.getStringCellValue();
else if(cell.getCellTypeEnum()==CellType.NUMERIC||cell.getCellTypeEnum()==CellType.FORMULA)
{String CellText = String.valueOf(cell.getNumericCellValue());
return CellText;}
else if(cell.getCellTypeEnum()==CellType.BLANK)
return "";
else return String.valueOf(cell.getBooleanCellValue());
}
catch(Exception e)
{
e.printStackTrace();
return "row"+rowNum+"col"+colNum+"Does not exist";
}
}
}
Add Explicit wait before entering password.
Try like below.
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://login.yahoo.com/");
driver.findElement(By.xpath("//input[#id='login-username']")).sendKeys("Username");
driver.findElement(By.xpath("//*[#id='login-signin']")).click();
WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='login-passwd']")));
driver.findElement(By.xpath("//*[#id='login-passwd']")).sendKeys("Password");
It was working in my machine.Let me know if you have any queries
package poi;
import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
class Demo
{
public static void main(String a[]) throws IOException
{
String temp ="C:/santosh_chikne/Selenium Script/test.xlsx";
FileInputStream fs = new FileInputStream(temp);
Workbook wb = Workbook.getWorkbook(fs);
}
}
try as following:
String temp ="C:/santosh_chikne/Selenium Script/test.xlsx";
FileInputStream fs = new FileInputStream(temp);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sh = wb.getSheet("Sheet1");
Row row = sh.getRow(1);
Cell cell = row.getCell(1);
String valueFromXl = cell.getStringCellValue();