Module 20 giving nullpointer exception | Selenium Forum
M
Posted on 29/07/2016
hello sir im getting null pointer exception ....in testBase openBrowser method Config.getproperty("browserType") is giving null pointer exception


package com.qtpselenium.base;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;

import com.qtpselenium.util.Xls_Reader;

public class TestBase {
public static Logger App_Logger=null;
public static Properties Config=null;
public static Properties OR=null;
public static Xls_Reader suitexls=null;
public static Xls_Reader suite_shop_xls=null;
public static Xls_Reader suite_cart_xls=null;
public static Xls_Reader suite_productdisplay_xls=null;
public static boolean isInitalize=false;
public static WebDriver driver=null;
public static boolean isBrowserOpened=false;

public void initialize() throws IOException{
if(!isInitalize){


App_Logger=Logger.getLogger("devpinoyLogger");

App_Logger.debug("Loading property file");
Config=new Properties();
FileInputStream ip=new FileInputStream("C:\\sunita_java\\Core_DDFramework_Webdriver\\src\\com\\qtpselenium\\config\\config.properties");
Config.load(ip);
//Config.getProperty("ReportPath");

OR=new Properties();
// System.out.println(System.getProperty(("user.dir")));
//FileInputStream ip1=new FileInputStream(System.getProperty(("user.dir")+"//src//com//qtpselenium//config//OR.properties"));
FileInputStream ip1=new FileInputStream("C:\\sunita_java\\Core_DDFramework_Webdriver\\src\\com\\qtpselenium\\config\\OR.properties");
OR.load(ip1);
//System.out.println(OR.getProperty("login_link"));
App_Logger.debug("Properties file loaded properly");

App_Logger.debug("Xls file loading");
suitexls=new Xls_Reader(System.getProperty("user.dir")+"//src//com//qtpselenium//xls//Suite.xlsx");
suite_shop_xls=new Xls_Reader(System.getProperty("user.dir")+"//src//com//qtpselenium//xls//Shop Suite.xlsx");
suite_cart_xls=new Xls_Reader(System.getProperty("user.dir")+"//src//com//qtpselenium//xls//Cart Suite.xlsx");
suite_productdisplay_xls=new Xls_Reader(System.getProperty("user.dir")+"//src//com//qtpselenium//xls//Product Display Suite.xlsx");
App_Logger.debug("Xls file loaded sucessfully");

isInitalize=true;
}
}

// selenium RC/ Webdriver
// open a browser if its not opened
public void openBrowser(){
if(!isBrowserOpened){
if(Config.getProperty("browserType").equals("MOZILLA"))
driver = new FirefoxDriver();
else if (Config.getProperty("browserType").equals("IE"))
driver = new InternetExplorerDriver();
else if (Config.getProperty("browserType").equals("CHROME"))
driver = new ChromeDriver();

isBrowserOpened=true;
String waitTime=Config.getProperty("default_implicitWait");
driver.manage().timeouts().implicitlyWait(Long.parseLong(waitTime), TimeUnit.SECONDS);
}

}

// close browser
public void closeBrowser(){
driver.quit();
isBrowserOpened=false;
}

}




----------------------------------------------------------------------------------CheckProductDisplay -------------------------


package com.qtpselenium.suite.shop;

import java.util.concurrent.TimeUnit;

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.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.qtpselenium.util.ErrorUtil;
import com.qtpselenium.util.TestUtil;
import com.qtpselenium.util.Xls_Reader;

public class CheckProductDisplay extends TestSuiteBase{
String runmodes[]=null;
int count=-1;
static boolean skip=false;
static boolean fail=false;
static boolean isTestpass=true;

@BeforeTest
public void isTestcaseSkiped(){
if(!TestUtil.isTestCaseRunnable(suite_shop_xls, this.getClass().getSimpleName())){

App_Logger.debug("checked testcase runmode set to no ");
throw new SkipException("skipping testcase as runmode set to no");

}
runmodes=TestUtil.getDataSetRunmodes(suite_shop_xls, this.getClass().getSimpleName());
}

@Test
public void TestcaseA1() throws Exception{
//test runmodes of current dataset
count++;
if(!runmodes[count].equalsIgnoreCase("Y")){
skip=true;
throw new SkipException("Testdata runmode set to no: "+ count);
}
App_Logger.debug("Executing Testcase A1");


//Webdriver code
openBrowser();
//System.setProperty("webdriver.chrome.driver","C:\\sunita_java\\driverExe\\chromedriver.exe");
//WebDriver driver=new ChromeDriver();
// driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.get("http://demo.virtuemart.net/");
driver.findElement(By.xpath("//*[@id='header']/ul[2]/li[1]/a")).click();

WebElement feature_view=driver.findElement(By.className("featured-view"));
int tot_img=feature_view.findElements(By.tagName("img")).size();
System.out.println("Number of images"+tot_img);

WebElement addbuttons=driver.findElement(By.className("featured-view"));
int button=addbuttons.findElements(By.className("product-details")).size();
System.out.println("Number of add buttons"+button);
//driver.quit();
closeBrowser();
}


@AfterMethod
public void reporter(){
if(skip){
TestUtil.setcellResultdata(suite_shop_xls,this.getClass().getSimpleName(), count+2, "skip");
}
else if(fail){
isTestpass=false;
TestUtil.setcellResultdata(suite_shop_xls,this.getClass().getSimpleName(), count+2, "fail");
}else{
TestUtil.setcellResultdata(suite_shop_xls,this.getClass().getSimpleName(), count+2, "pass");
}
skip=false;
fail=false;
}

@AfterTest
public void TestResporter(){
if(isTestpass){
TestUtil.setcellResultdata(suite_shop_xls,"Test Cases",TestUtil.getRowNumber(suite_shop_xls,this.getClass().getSimpleName()), "pass");
}else{
TestUtil.setcellResultdata(suite_shop_xls,"Test Cases", TestUtil.getRowNumber(suite_shop_xls,this.getClass().getSimpleName()), "fail");
}
}


}






-----------------------TestSuiteBase-------------------------------------------------------------------------------------------------------





package com.qtpselenium.suite.shop;

import java.io.IOException;

import org.testng.SkipException;
import org.testng.annotations.BeforeSuite;

import com.qtpselenium.base.TestBase;
import com.qtpselenium.util.TestUtil;

public class TestSuiteBase extends TestBase{

//check suite to skip or not
@BeforeSuite
public void CheckSuiteSkip() throws IOException{
initialize();
App_Logger.debug("checking Shop Suite Runmode");

if(!TestUtil.isSuiteRunnable(suitexls, "Shop Suite")){
App_Logger.debug("skiped Shop Suite as runmode is set to No ");
throw new SkipException("Runmode of Shop Suite is set to No");
}
}
}

M
Replied on 29/07/2016

Thanks no need to reply.... i did double quots for value of key in config file browserType="IE" instead of browserType=IE