0

We have application where we do lot of file upload operations, the below code works in local if we keep the file in classpath say "src\test\resources\csvImports\sample.csv". Since its in local it gets the absolute path, however when we try to run from remote machine or Jenkins it fails saying path not found.

File f = new File("src/test/resources/csvImports/"+fileName); getDriver().findElement(By.xpath("//input[@type='file']")).sendKeys(f.getAbsolutePath());

  • Update the question with the relevant HTML of the element. – undetected Selenium Dec 29 '21 at 19:24
  • You want to set a "file detector"... this transfers the file to the remote end, and updates the path: https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/remote/index_exports_FileDetector.html Also see this answer: https://stackoverflow.com/questions/61658005/how-to-upload-file-in-input-file-field-using-selenium-webdriver-when-script-exec/61658006#61658006 – pcalkins Dec 29 '21 at 19:29
  • @pcalkins Thanks a lot, I am using serenity, and it giving me error when I am trying to use setFileDetector method for the element, any suggestion? WebElement uploadElement = getDriver().findElement(By.xpath("//input[@type='file']")); LocalFileDetector detector = new LocalFileDetector(); File localFile = detector.getLocalFile(f.getAbsolutePath()); **uploadElement.setFileDetector(detector);** – Brajesh Sahu Dec 29 '21 at 23:16
  • Move your additions from the comments into the post to make it readable – Lies Jan 10 '22 at 11:51

1 Answers1

0

You can try: File f = new File(System.getProperty("user.dir") + "src/test/resources/csvImports/"+fileName);

Serghei Mardar
  • 161
  • 1
  • 3