Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"path is not absolute" exception while uploading file in selenium

path is not absolute: src/test/resources/testData/twt_Pic.jpg getting this exception while uploading the file using selenium.

Instead of uploading the file using the native os file explorer, sending the keys to the path file.

The same path used to work during the older version, not sure which version was it.

Error Log in Chrome:

org.openqa.selenium.WebDriverException: unknown error: path is not absolute: src/test/resources/testData/twt_Pic.jpg
  (Session info: chrome=66.0.3359.181)
  (Driver info: chromedriver=2.39.562713 (dd642283e958a93ebf6891600db055f1f1b4f3b2),platform=Mac OS X 10.13.4 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'nagarjunaMBP.local', ip: 'fe80:0:0:0:c2e:b816:67ae:922b%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.4', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.39.562713 (dd642283e958a9..., userDataDir: /var/folders/g4/dylg4g7s7wb...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 66.0.3359.181, webStorageEnabled: true}
Session ID: bdf391bf16ddbda6c9f73d559404bae7
    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:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
    at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
    at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:276)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:100)
    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:497)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:51)
    at com.sun.proxy.$Proxy17.sendKeys(Unknown Source)
    at actions.PublishBroadcast.MediaLibAction.uploadImage(MediaLibAction.java:43)
    at stepDefinitions.Publish.SDMediaLibrary.Upload_an_Image(SDMediaLibrary.java:43)
    at ✽.When Upload an Image(features/publish/mediaLibrary/MediaLibrary.feature:11)

Error Log for Firefox:

org.openqa.selenium.InvalidArgumentException: File not found: src/test/resources/testData/twt_Pic.jpg
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T14:04:26.12Z'
System info: host: 'nagarjunaMBP.local', ip: 'fe80:0:0:0:c2e:b816:67ae:922b%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.13.4', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 60.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 53177, moz:profile: /var/folders/g4/dylg4g7s7wb..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: MAC, platformName: MAC, platformVersion: 17.5.0, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 7c932de3-ea6a-a143-a7b4-bf7dfa3e2660

Project Structure:

  1. Project/src/main/java - Contains actions, UI element repository, Step Defenitions. From where the file is being called
  2. Project/src/test/java - Contains only runner class
  3. Project/src/test/resources/customLib - Contains drivers
  4. Project/src/test/resources/features - Contains feature files
  5. Project/src/test/resources/testData - Contains the test data including the file which i'm trying to call here.
like image 370
Nagarjuna Reddy Avatar asked Jun 04 '18 15:06

Nagarjuna Reddy


2 Answers

You can try creating a File instance and getting the absolute path from there.

File file = new File("src/test/resources/testData/twt_Pic.jpg");
yourElement.sendKeys(file.getAbsolutePath());
like image 84
iamkenos Avatar answered Nov 14 '22 22:11

iamkenos


the error message is clearly telling you what's wrong.

you are trying to upload src/test/resources/testData/twt_Pic.jpg, which is a relative path to an image. As the error states, it must be an absolute path (not relative). Replace the path and try again.

like image 40
Corey Goldberg Avatar answered Nov 15 '22 00:11

Corey Goldberg