Why im getting java.io.FileNotFoundException, (No such file or directory) In Eclipse when trying the run the Testng.xml file

132 views Asked by At

I'm trying to run the testng.xml in eclipse,(Why the / slashes is getting changed to \ slashes) I get the following file not found error:

Caused by: java.io.FileNotFoundException: /Users/nandhakumar/Desktop/Resources/Automation Workspace/liveproject1\src\main\java\resources\config.properties (No such file or directory)

The file path which is being fetched from the WebDriverInstance class for Config.properties but when i do the file path is getting changed to \ backslashes from src.(As i'm using mac i'm using forward slashes for giving file path).

Attaching the WebDriverInstance class code where im giving the config.properties file path and browser paths:

package base;

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

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverInstance {

    public static ThreadLocal<WebDriver> driver = new ThreadLocal<>();

    public static WebDriver getDriver() {
        if (driver.get() == null) {
            try {
                driver.set(createDriver());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return driver.get();
    }

    public static WebDriver createDriver() throws IOException {
        WebDriver driver = null;

        Properties prop = new Properties();
        FileInputStream data = new FileInputStream(
                System.getProperty("user.dir") + "/src/main/java/resources/config.properties");
        prop.load(data);

        if (prop.getProperty("browser").equals("chrome")) {
            System.setProperty("webdriver.chrome.driver",
                    System.getProperty("user.dir") + "/src/main/java/drivers/chromedriver");
            driver = new ChromeDriver();
        } else if (prop.getProperty("browser").equals("firefox")) {
            System.setProperty("webdriver.gecko.driver",
                    System.getProperty("user.dir") + "/src/main/java/drivers/geckodriver.exe");
            driver = new FirefoxDriver();
        } 
        
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        return driver;
    }

    public static void cleanupDriver() {
        driver.get().quit();
        driver.remove();
    }

}

I guess due to this Testng Cannot instantiate class for the class which im trying to run org.testng.TestNGException: Cannot instantiate class uk.co.automationtesting.AddRemoveItemBasketTest

When I tried running it I got a FileNotFoundException.

How do I correctly specify the path?

Will attach the Console error which i'm getting is there any other way to workaround this issue ?

[RemoteTestNG] detected TestNG version 7.4.0
org.testng.TestNGException: 
Cannot instantiate class uk.co.automationtesting.AddRemoveItemBasketTest
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30)
    at org.testng.internal.InstanceCreator.instantiateUsingDefaultConstructor(InstanceCreator.java:193)
    at org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:113)
    at org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79)
    at org.testng.internal.objects.SimpleObjectDispenser.dispense(SimpleObjectDispenser.java:25)
    at org.testng.internal.objects.GuiceBasedObjectDispenser.dispense(GuiceBasedObjectDispenser.java:30)
    at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:112)
    at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:165)
    at org.testng.TestClass.getInstances(TestClass.java:122)
    at org.testng.TestClass.initTestClassesAndInstances(TestClass.java:102)
    at org.testng.TestClass.init(TestClass.java:94)
    at org.testng.TestClass.<init>(TestClass.java:59)
    at org.testng.TestRunner.initMethods(TestRunner.java:463)
    at org.testng.TestRunner.init(TestRunner.java:339)
    at org.testng.TestRunner.init(TestRunner.java:292)
    at org.testng.TestRunner.<init>(TestRunner.java:223)
    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.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55)
    at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:659)
    at org.testng.SuiteRunner.init(SuiteRunner.java:173)
    at org.testng.SuiteRunner.<init>(SuiteRunner.java:107)
    at org.testng.TestNG.createSuiteRunner(TestNG.java:1300)
    at org.testng.TestNG.createSuiteRunners(TestNG.java:1276)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1125)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
    at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23)
    ... 29 more
Caused by: java.io.FileNotFoundException: /Users/nandhakumar/Desktop/Resources/Automation Workspace/liveproject1\src\main\java\resources\config.properties (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
    at base.BasePage.<init>(BasePage.java:26)
    at base.Hooks.<init>(Hooks.java:12)
    at uk.co.automationtesting.AddRemoveItemBasketTest.<init>(AddRemoveItemBasketTest.java:24)
    ... 35 more

I tried the project -> clean , Maven -> clean , Maven ->update project and file.separator too nothing worked it would be helpful if anyone can help with the solution.

Just wanted to know why the forward / slashes are getting changed to \ slashes even though i give correct file path,What wrong with this code ?

0

There are 0 answers