Hi Ashish,
I am getting null pointer exception for follwoing java file. I am pasting my Base class file and Login class file
[b:1z291wdw]LOGIN FILe[/b:1z291wdw]---------------------------------------------------------------
package com.qtpselenium.zoho.project.testcases;
import java.io.IOException;
import java.util.Hashtable;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import com.qtpselenium.zoho.project.base.BaseTest;
import com.qtpselenium.zoho.project.util.DataUtil;
import com.qtpselenium.zoho.project.util.Xls_Reader;
import com.relevantcodes.extentreports.LogStatus;
public class LoginTest extends BaseTest {
String testCaseName="LoginTest";
SoftAssert softAssert;
Xls_Reader xls;
@Test(dataProvider="getData")
public void doLoginTest(Hashtable<String,String> data) throws IOException{
test=rep.startTest("LoginTest");
if(!DataUtil.isRunnable(testCaseName, xls) || data.get("Runmode").equals("N")){
test.log(LogStatus.SKIP, "Skipping as Runmode is No");
throw new SkipException("Skipping as runmode is No");
}
openBrowser("Chrome");
navigate("appurl");
}
@BeforeMethod
public void init(){
softAssert= new SoftAssert();
}
@AfterMethod
public void quit(){
try{
softAssert.assertAll();
}
catch(Exception e){
test.log(LogStatus.FAIL, e.getMessage());
}
rep.endTest(test);
rep.flush();
}
@DataProvider
public Object[][]getData() throws IOException{
super.init();
Xls_Reader xls= new Xls_Reader(prop.getProperty("xlspath"));
return DataUtil.getTestData(xls, testCaseName);
}
}
[b:1z291wdw]Base----------------------------------[/b:1z291wdw]
package com.qtpselenium.zoho.project.base;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.asserts.SoftAssert;
import com.qtpselenium.zoho.project.util.ExtentManager;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class BaseTest {
public WebDriver driver;
public Properties prop;
public ExtentReports rep= ExtentManager.getInstance();
public ExtentTest test;
public SoftAssert softAssert= new SoftAssert();
public void init() throws IOException{
if(prop==null){
prop= new Properties();
try {
FileInputStream fs= new FileInputStream(System.getProperty("user.dir")+"//src//test//resources//projectconfig.properties");
prop.load(fs);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void openBrowser(String bType) throws IOException {
if(prop==null){
prop= new Properties();
try {
FileInputStream fs= new FileInputStream(System.getProperty("user.dir")+"//src//test//resources//projectconfig.properties");
prop.load(fs);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(bType.equals("Mozilla")){
System.setProperty("webdriver.gecko.driver",prop.getProperty("mozilladriver_exe"));
driver= new FirefoxDriver();
}
else if(bType.equals("Chrome")){
System.setProperty("webdriver.chrome.driver",prop.getProperty("chromedriver_exe"));
driver= new ChromeDriver();
}
else if(bType.equals("IE")){
System.setProperty("webdriver.ie.driver",prop.getProperty("iedriver_exe"));
driver= new InternetExplorerDriver();
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
public void navigate (String urlKey){
test.log(LogStatus.INFO, "Navigating to" +prop.getProperty(urlKey));
driver.get(prop.getProperty(urlKey));
}
public void click(String locatorKey){
test.log(LogStatus.INFO, "clicking on" +locatorKey);
getElement(locatorKey).click();
test.log(LogStatus.INFO, "clicked successfully on" +locatorKey);
}
public void type(String locatorKey,String data){
test.log(LogStatus.INFO, "typing in" +locatorKey +"..data.."+ data);
getElement(locatorKey).sendKeys(data);
test.log(LogStatus.INFO, "typed successfully in" +locatorKey );
}
public WebElement getElement(String locatorKey){
WebElement e= null;
try {
if(locatorKey.endsWith("_id"))
e=driver.findElement(By.id(prop.getProperty(locatorKey)));
else if(locatorKey.endsWith("_name"))
e=driver.findElement(By.name(prop.getProperty(locatorKey)));
else if(locatorKey.endsWith("_xpath"))
e=driver.findElement(By.xpath(prop.getProperty(locatorKey)));
else{
reportFailure("Locator not correct--"+ locatorKey);
Assert.fail("locator not correct"+locatorKey);
}
}
catch(Exception ex){
reportFailure(ex.getMessage());
ex.printStackTrace();
Assert.fail("failed the test--" + ex.getMessage());
}
return e;
}
/********************validations*************************************/
public boolean verifyTitle(){
return false;
}
public boolean isElementPresent(String locatorKey){
List<WebElement> elementList=null;
if(locatorKey.endsWith("_id"))
elementList=driver.findElements(By.id(prop.getProperty(locatorKey)));
else if(locatorKey.endsWith("_name"))
elementList=driver.findElements(By.name(prop.getProperty(locatorKey)));
else if(locatorKey.endsWith("_xpath"))
elementList=driver.findElements(By.xpath(prop.getProperty(locatorKey)));
else{
reportFailure("Locator not correct--"+ locatorKey);
Assert.fail("locator not correct"+locatorKey);
}
if(elementList.size()==0)
return false;
else
return true;
}
public boolean verifyText(String locatorKey,String expectedTextKey){
String actualText=getElement(locatorKey).getText().trim();
String expectedText=prop.getProperty(expectedTextKey);
if(actualText.equals(expectedText))
return true;
else
return false;
}
/***********************Reporting***************************************/
public void reportPass(String msg){
test.log(LogStatus.PASS, msg);
}
public void reportFailure(String msg){
test.log(LogStatus.FAIL,msg );
takeScreenshot();
Assert.fail(msg);
}
public void takeScreenshot(){
Date d=new Date();
String screenshotFile=d.toString().replace(":", "_").replace(" ", "_")+".png";
File scrFile=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try{
FileUtils.copyFile(scrFile, new File(System.getProperty("user.dir")+"//screenshots//"+screenshotFile));
}
catch (IOException e){
e.printStackTrace();
}
test.log(LogStatus.INFO,"Screenshot"+ test.addScreenCapture(System.getProperty("user.dir")+"//screenshots//"+screenshotFile));
}
}