0

I try to automate a page that I have. In the page I need to upload a PDF file. The problem is that I want selenium via java will get the file from the resource of the project and not hard coded. (since some have windows and some mac and some ubonto so the solution is to get the path dynamically).

I manually hard coded I enter the path it worked and return the path, however to let java return the path it crash. this is my code:

  /******* test *******/
    public  void testFileUpload() throws Exception {
        WebDriver deiver2 = getWebDriver();
      

        Thread.sleep(3000);
        deiver2.switchTo()
                .activeElement();
        deiver2.findElement(By.xpath("//input[@type='file']"))
        .sendKeys(
                "X:\\project\\src\\test\\resources\\TAX12.pdf");

        ClassLoader classLoader = getClass().getClassLoader();
        String path = classLoader.getResource("TAX12.pdf").getPath();
        System.out.println("\n\n path is  " + path);

        System.out.println("END");

    }

I want in the send keys to send the file, the exists however it is not worked.

java.lang.NullPointerException

    at org.testng.Assert.fail(Assert.java:97)
    at tests.ExPubPaymentPageTest.tryFile(ExPubPaymentPageTest.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at org.testng.TestRunner.privateRun(TestRunner.java:766)
    at org.testng.TestRunner.run(TestRunner.java:587)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
    at org.testng.TestNG.runSuites(TestNG.java:1039)
    at org.testng.TestNG.run(TestNG.java:1007)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)

the file exists in the folder and worked if manually set the path, but not locate the file if I want it to do it by himself (for win / mac / linux etc)

enter image description here

Bastian
  • 1,089
  • 7
  • 25
  • 74
  • Usually resources reside in the final JAR. So, even if you can get the path you cannot write to it since it is inside a zipped file. See [this answer](https://stackoverflow.com/a/3457999/1744774) to _How a JAR file can read an external properties file_: "_Simplest way, use the -D switch to define a system property on a java command line. That system property may contain a path [...]_". – Gerold Broser Jan 16 '21 at 11:23
  • A .jar entry is not a file and will never be a file. You cannot refer to it as a file. You cannot treat it as a file. Calling `getPath()` on a URL *does not* return a valid file name. It just returns part of the URL, which can have percent-escapes in it for various reasons. Don’t use `getPath()` and don’t try to read the resource as a file. Use [getResourceAsStream](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/ClassLoader.html#getResourceAsStream(java.lang.String)) to read your resource. – VGR Jan 16 '21 at 15:17

0 Answers0