TestNG dosn't support openBrowser() function in webdriver coding

TestBase main test case function it doesn’t open IE/Firefox/Chrome

Please help

TestBase.java

package com.qtpselenium.base;

import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Properties;

import org.apache.log4j.Logger; 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.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import org.testng.Assert;

import com.qtpselenium.util.ErrorUtil; import com.qtpselenium.util.Xls_Reader; import com.thoughtworks.selenium.Selenium;

public class TestBase {

public static Logger APP_LOGS=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 isInitalized=false;

public static WebDriver driver = null;

public static Selenium selenium=null;

public static boolean isBrowserOpened=false;

public void initialize () throws Exception

{// logs

if(!isInitalized)

APP_LOGS = Logger.getLogger(“devpinoyLogger”);

APP_LOGS.debug(“Loading Property files”);

Properties CONFIG = new Properties();

FileInputStream ip = new FileInputStream(System.getProperty(“user.dir”)+"//src//com//qtpselenium//config/config.properties");

CONFIG.load(ip);

System.out.println(CONFIG.getProperty(“screenshotPath”));

OR = new Properties();

ip = new FileInputStream(System.getProperty(“user.dir”)+"//src//com//qtpselenium//config/OR.properties");

OR.load(ip);

APP_LOGS.debug(“Loaded Property files successfully”);

//System.out.println(OR.getProperty(“login_link”));

APP_LOGS.debug(“Loading Property files”);

// xls file

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");

suiteXls = new Xls_Reader(System.getProperty(“user.dir”)+"\src\com\qtpselenium\xls\Suite.xlsx");

APP_LOGS.debug(“Loaded Property files Successfully”);

isInitalized=true;

}

public void openBrowser()

{

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();

}

public void closeBrowser()

{

driver.quit();

}

public boolean compareTitle(String expectedVal)

{

try{

Assert.assertEquals(driver.getTitle(), expectedVal);

}catch(Throwable t) {

ErrorUtil.addVerificationFailure(t);

APP_LOGS.debug(“Titles do not match”);

return false;

}

return true;

}

public boolean compareNumbers(int actualValue, int expectedVal)

{

try{

Assert.assertEquals(expectedVal, actualValue);

}catch(Throwable t) {

ErrorUtil.addVerificationFailure(t);

APP_LOGS.debug(“values do not match”);

return false;

}

return true; }

public boolean checkElementPresence(String xpathKey) { int count = driver.findElements(By.xpath(CONFIG.getProperty(xpathKey))).size(); try{ Assert.assertTrue(count>0, “No element present”);

}catch(Throwable t) {

ErrorUtil.addVerificationFailure(t);

APP_LOGS.debug(“No element present”);

return false; } return true; }

public WebElement getObject (String xpathKey) { try{ return driver.findElement(By.xpath(OR.getProperty(xpathKey))); }catch(Throwable t) { APP_LOGS.debug(“Cannot find object with key–”+ xpathKey); return null;

} }} &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&7 AddProductTest.java

package com.qtpselenium.suite.shop;

import org.testng.annotations.Test; import org.openqa.selenium.By; 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.TestUtil;

public class AddProductTest extends TestSuiteBase {

String runmodes[]=null;

static int count=-1;

static boolean fail=false;

static boolean skip=false;

static boolean isTestPass=true;

@BeforeTest

public void checkTestSkip(){

if(!TestUtil.isTestCaseRunnable(suite_shop_xls, this.getClass().getSimpleName())){

// APP_LOGS.debug(“Skipping Test Case as runmode set to NO”); //logs

//throw new SkipException(“Skipping Test Case as runmode set to NO”); //reports

}

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

}

@Test(dataProvider=“getTestData”)

public void testCaseA2(

String Mobile_Type,

String color,

String memory,

String Engraving,

String quantity

)

{

count++;

if(!runmodes[count].equalsIgnoreCase(“Y”)){

skip=true;

throw new SkipException(“Runmode for test set data set to no” +count);

}

APP_LOGS.debug(“Executing TestCase_A2”);

APP_LOGS.debug(Mobile_Type +" – “+color +” – "+memory );

openBrowser();

driver.get(CONFIG.getProperty(“testSiteName”));

getObject(“shopLink”).click();

getObject(“mobile_phone_link”).click();

}

@AfterMethod public void reportDataSetResult(){

if(skip)

TestUtil.reportDataSetResult(suite_shop_xls, this.getClass().getSimpleName(), count+2, “SKIP”);

else if(fail){

isTestPass=false;

TestUtil.reportDataSetResult(suite_shop_xls, this.getClass().getSimpleName(), count+2, “FAIL”);

}

else

TestUtil.reportDataSetResult(suite_shop_xls, this.getClass().getSimpleName(), count+2, “PASS”);

skip=false;

fail=false;

}

@AfterTest public void reportTestResult(){

if(isTestPass)

//TestUtil.reportDataSetResult(suiteAxls, “Test Cases”, count+2, “PASS”);

TestUtil.reportDataSetResult(suite_shop_xls, “Test Cases”, TestUtil.getRowNum(suite_shop_xls,this.getClass().getSimpleName()), “PASS”);

else

//TestUtil.reportDataSetResult(suiteAxls, “Test Cases”, count+2, “FAIL”);

TestUtil.reportDataSetResult(suite_shop_xls, “Test Cases”, TestUtil.getRowNum(suite_shop_xls,this.getClass().getSimpleName()), “FAIL”); }

@DataProvider

public Object[][] getTestData(){

return TestUtil.getData(suite_shop_xls, this.getClass().getSimpleName());

}

}

Hi,

The question is far too vague to answer.

this is the error

java.lang.NullPointerException

at com.qtpselenium.base.TestBase.openBrowser(TestBase.java:69)

at com.qtpselenium.suite.shop.AddProductTest.testCaseA2(AddProductTest.java:60)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)

at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)

at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)

at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)

at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)

at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)

at org.testng.TestRunner.privateRun(TestRunner.java:767)

at org.testng.TestRunner.run(TestRunner.java:617)

at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)

at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)

at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)

at org.testng.SuiteRunner.run(SuiteRunner.java:240)

at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)

at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)

at org.testng.TestNG.runSuitesSequentially(TestNG.java:1197)

at org.testng.TestNG.runSuitesLocally(TestNG.java:1122)

at org.testng.TestNG.run(TestNG.java:1030)

at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)

at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)

at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

this is function in base class public void openBrowser()

{

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();

}

it doesn’t open any of the browser

Can you show the snippet of your build script that configures your testng based test task? My first guess is, that a property is not correctly passed to the test jvm. Where does this CONFIG class come from?

cheers, René

Its in Test Package

package test;

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

public class TestConfig {

/**

  • @param args

  • @throws IOException

*/

public static void main(String[] args) throws IOException {

Properties CONFIG = new Properties();

FileInputStream ip = new FileInputStream(System.getProperty(“user.dir”)+"//src//com//qtpselenium//config//config.properties");

CONFIG.load(ip);

System.out.println(CONFIG.getProperty(“screenshotPath”));

Properties OR = new Properties();

ip = new FileInputStream(System.getProperty(“user.dir”)+"//src//com//qtpselenium//config//OR.properties");

OR.load(ip);

System.out.println(OR.getProperty(“testSiteName”));

}

}

Still, not not entirely sure what the problem is. I assume that the paths are not setup correctly. Can you do me a favor and create a small self contained example that shows the problem, Then I can take a look on it locally.

cheers, Rene

Rene give me your email id i will send you the files

Thanks,

rene.groeschke@gradleware.com

Hello Rene,

first thanks for the help, I just removed the null; from test base class.

public static Logger APPLOGS=null;

public static Properties CONFIG=null;

public static Properties OR=null;

public static XlsReader suiteXls=null;

public static XlsReader suiteshopxls=null;

public static XlsReader suitecartxls=null;

public static XlsReader suiteproductDisplay_xls=null;

public static boolean isInitalized=false;

public static WebDriver driver = null;

public static Selenium selenium=null;

Thanks, Harpreet

another question

its not doing Add_to_cart in the website.

config.properties file #- XPATHS, IDS, linkstext of my app

shopLink=html/body/div[3]/div[1]/div/div[1]/div/ul/li[2]/a/span mobile_phone_link=//[@id=‘vmMainPage’]/div[2]/div[1]/div[3]/div/h2/a/img show_cart_link=//[@id=‘rt-sidebar-c’]/div[1]/div/div/div[7]/a mp3_player_link=//[@id=‘vmMainPage’]/div[2]/div[1]/div[1]/div/h2/a/img part1=//[@id=‘vmMainPage’]/div[1]/div[ part2=]/div[ part3=]/div/h2/a

Dropdowns mobile_color_list=//[@id=‘Color_field’] mobile_memory_list=//[@id=‘Memory_field’]

#input box mobile_phone_quantity=//input[starts-with(@id, ‘quantity’)] mobline_phone_engraving=//input[starts-with(@id, ‘Engraving’)]

#buttons add_to_cart_button=//input[starts-with(@id, ‘addtocart_501c’)]/div[2]/span[3]/input

#text price_start=//*[@id=‘vmMainPage’]/div[1]/div[ price_end=]/div[4] body=//body

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& class file this link in the class (driver.findElement(By.className(“addtocart-button”)).click():wink:

package com.qtpselenium.suite.shop;

import org.testng.annotations.Test; import org.testng.annotations.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.ie.InternetExplorerDriver; 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.TestUtil;

public class AddProductTest extends TestSuiteBase {

String runmodes[]=null;

static int count=-1;

static boolean fail=false;

static boolean skip=false;

static boolean isTestPass=true;

@BeforeTest

public void checkTestSkip(){

if(!TestUtil.isTestCaseRunnable(suite_shop_xls, this.getClass().getSimpleName())){

APP_LOGS.debug(“Skipping Test Case as runmode set to NO”); //logs

throw new SkipException(“Skipping Test Case as runmode set to NO”); //reports

}

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

}

@Test(dataProvider=“getTestData”)

public void testCaseA2(

String Mobile_Type,

String color,

String memory,

String Engraving,

String quantity

) throws InterruptedException

{

count++;

if(!runmodes[count].equalsIgnoreCase(“Y”)){

//skip=true;

throw new SkipException(“Runmode for test set data set to no” +count);

}

APP_LOGS.debug(“Executing TestCase_A2”);

APP_LOGS.debug(Mobile_Type +" – “+color +” – "+memory );

openBrowser();

driver.get(CONFIG.getProperty(“testSiteName”));

getObject(“shopLink”).click();

getObject(“mobile_phone_link”).click();

driver.findElement(By.linkText(Mobile_Type)).click();

getObject(“mobile_color_list”).sendKeys(color);

getObject(“mobile_memory_list”).sendKeys(memory);

getObject(“mobline_phone_engraving”).sendKeys(Engraving);

//driver.findElement(By.xpath("[starts-with(@id, ‘Engraving’)]")).sendKeys(Engraving);

getObject(“mobile_phone_quantity”).clear();

//

int q= (int)Double.parseDouble(quantity);

getObject(“mobile_phone_quantity”).sendKeys(String.valueOf(q));

// add to cart

//getObject(“add_to_cart_button”).click();

driver.findElement(By.className(“addtocart-button”)).click();

Thread.sleep(3000L);

String x=driver.findElement(By.className(“shop_info”)).getText();

System.out.println(x);

//Assert.assertEquals(“Info: The product was added to your cart.”, x);

//compareStrings("","");

//Assert.assertEquals("", “”);

//compareTitle(expectedVal)

}

/*if(!checkElementPresence(“shopLink”)){

// screenshot

//capturescreenshot(this.getClass().getSimpleName()+"_"+count);

fail=true;

// quit

return;

}

getObject(“shopLink”).click();

getObject(“mobile_phone_link”).click();

driver.findElement(By.linkText(Mobile_Type)).click();

getObject(“mobile_color_list”).sendKeys(color);

getObject(“mobile_memory_list”).sendKeys(memory);

getObject(“mobline_phone_engraving”).sendKeys(Engraving);

//driver.findElement(By.xpath("")).sendKeys(engraving);

getObject(“mobile_phone_quantity”).clear();

//

int q= (int)Double.parseDouble(quantity);

getObject(“mobile_phone_quantity”).sendKeys(String.valueOf(q));

//System.out.println(“testCaseA2”);

} */

@AfterMethod public void reportDataSetResult(){

if(skip)

TestUtil.reportDataSetResult(suite_shop_xls, this.getClass().getSimpleName(), count+2, “SKIP”);

else if(fail){

isTestPass=false;

TestUtil.reportDataSetResult(suite_shop_xls, this.getClass().getSimpleName(), count+2, “FAIL”);

}

else

TestUtil.reportDataSetResult(suite_shop_xls, this.getClass().getSimpleName(), count+2, “PASS”);

skip=false;

fail=false;

}

@AfterTest public void reportTestResult(){

if(isTestPass)

//TestUtil.reportDataSetResult(suiteAxls, “Test Cases”, count+2, “PASS”);

TestUtil.reportDataSetResult(suite_shop_xls, “Test Cases”, TestUtil.getRowNum(suite_shop_xls,this.getClass().getSimpleName()), “PASS”);

else

//TestUtil.reportDataSetResult(suiteAxls, “Test Cases”, count+2, “FAIL”);

TestUtil.reportDataSetResult(suite_shop_xls, “Test Cases”, TestUtil.getRowNum(suite_shop_xls,this.getClass().getSimpleName()), “FAIL”); }

@DataProvider

public Object[][] getTestData(){

return TestUtil.getData(suite_shop_xls, this.getClass().getSimpleName());

}

}