package com.qa.easy.testbase;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.qa.easy.testutil.WebEventListener;
public class TestBase {
public static WebDriver driver;
public static Properties prop;
public static EventFiringWebDriver e_driver;
public static WebEventListener eventListener;
public TestBase(){
try {
prop = new Properties();
FileInputStream ip = new FileInputStream(System.getProperty("F:\\Azar\\SeleniumMaven_Git_Jenkins"
+ "\\src\\main\\java\\com\\qa\\easy\\config\\config.properties"));
prop.load(ip);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization(){
String browserName = prop.getProperty("browser");
if(browserName.equals("chrome")){
System.setProperty("webdriver.chrome.driver","F:\\Installation\\chromedriver.exe");
driver = new ChromeDriver();
}
e_driver = new EventFiringWebDriver(driver);
// Now create object of EventListerHandler to register it with EventFiringWebDriver
eventListener = new WebEventListener();
e_driver.register(eventListener);
driver = e_driver;
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get(prop.getProperty("url"));
}
}
**My Page Class:**
package com.qa.easy.pages;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import com.qa.easy.testbase.TestBase;
public class Demopage extends TestBase {
@FindBy(className="cbt")
WebElement CBTImage;
@FindBy(className="round-tabs four")
WebElement advanced;
@FindBy(xpath="//a[contains(text(),'Table Sort & Search')]")
WebElement tablesearch;
@FindBy(xpath="//input[@type='search']")
WebElement searchbox;
@FindBy(xpath="//table[@id='example']//tbody")
WebElement searchresults;
public Demopage() {
PageFactory.initElements(driver, this);
}
public boolean CBTImage() {
return CBTImage.isDisplayed();
}
}
**My Test class:**
package com.qa.easy.testcases;
import java.io.IOException;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.qa.easy.pages.Demopage;
import com.qa.easy.testbase.TestBase;
public class Demotest extends TestBase{
Demopage demopage;
public Demotest() throws IOException {
super();
}
@BeforeMethod
public void setUp(){
initialization();
demopage = new Demopage();
}
@Test
public void cbtLogo(){
boolean flag = demopage.CBTImage();
Assert.assertTrue(flag);
}
@AfterMethod
public void tearDown(){
driver.quit();
}
}
Actual Result now:
[RemoteTestNG] detected TestNG version 6.14.3
org.testng.TestNGException:
Cannot instantiate class com.qa.easy.testcases.Demotest
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:423)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:336)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:125)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:190)
at org.testng.TestClass.getInstances(TestClass.java:95)
at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:81)
at org.testng.TestClass.init(TestClass.java:73)
at org.testng.TestClass.<init>(TestClass.java:38)
at org.testng.TestRunner.initMethods(TestRunner.java:389)
at org.testng.TestRunner.init(TestRunner.java:271)
at org.testng.TestRunner.init(TestRunner.java:241)
at org.testng.TestRunner.<init>(TestRunner.java:192)
at org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33)
at org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:713)
at org.testng.SuiteRunner.init(SuiteRunner.java:260)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:198)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1295)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1273)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1128)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:24)
... 25 more
Caused by: java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.java:130)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.qa.easy.testbase.Basetest.<init>(Basetest.java:23)
at com.qa.easy.testcases.Demotest.<init>(Demotest.java:21)
... 30 more
1 Answer
Caused by: java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.java:130)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at com.qa.easy.testbase.Basetest.<init>(Basetest.java:23)
at com.qa.easy.testcases.Demotest.<init>(Demotest.java:21)
Which means that you the following line returns a null.
System.getProperty("F:\\Azar\\SeleniumMaven_Git_Jenkins"
+ "\\src\\main\\java\\com\\qa\\easy\\config\\config.properties")
-
Thanks Joao... But my properties file is exactly in that location. Any advice to fix this or find if it's returning null value ? Jul 17, 2019 at 10:05
-
1Are you sure the command doesn't return null? System.getProperty expects the name of a property (os.name, e.g.) not a path to a property file. Can you add a break point and check the result of this command specifically at this point? Jul 17, 2019 at 10:40
-
When I updated the line as FileInputStream inp = new FileInputStream("F:\\Azar\\SeleniumMaven_Git_Jenkins\\src\\main\\resources\\com\\qa\\easy\\config\\config.properties"); The error is java.io.FileNotFoundException. But I'm able to find this file in the same location in my drive. any issues with access settings ? Jul 17, 2019 at 11:07
-
1Thanks Joao.... I found the issue. prop.load(ip); where the issue was. instead of Inputstream method I changed it to Reader method and it is working fine now. Jul 17, 2019 at 11:20
-
This issue may have different causes, since it's highly coupled to the state of your file system. I would try out using the user.dir property as prefix to the path. Check this answer (and others on the same link) for more information stackoverflow.com/a/56092473/2252076 Jul 17, 2019 at 11:23