2

I have an E2E test to upload a file to the application which works on my local machine but fails to run on Browserstack. But fails with reason :

invalid argument: File not found : /home/travis/build/xx/xx/e2e/src/xx/testfile

Here is the code

let fileToUpload = 'testfile';
let absolutePath = path.resolve(__dirname, fileToUpload);
await browser.setFileDetector(new remote.FileDetector());
let fileElem = $('input[type="file"]');
await fileElem.sendKeys(absolutePath);

I have the files upload in my code base for travis to pick them. Any inputs are appreciated.

Thanks

asl
  • 77
  • 1
  • 2
  • 11

3 Answers3

2

Since the file upload is working from your local machine, you can try using the Local File Detector Option.

driver.setFileDetector(new LocalFileDetector());
driver.get("http://www.fileconvoy.com/");
driver.findElement(By.id("upfile_0")).sendKeys("C:\\Users\\hello\\url.txt");
driver.findElement(By.id("readTermsOfUse")).click();
driver.findElement(By.name("form_upload")).submit();

The above code snippet will upload a file located on the local machine. The same details are available here: https://www.browserstack.com/automate/java#enhancements-uploads-downloads

You can port this in the language of your choice.

ANM1996
  • 161
  • 3
  • Hi , Thanks for your response. But I am already using the remote FileDetector. Your answer and the browserstack post talks about local Filedetector. Is there a difference ? I thought I should be using remote FileDetector to upload files on browserstack and CI. could you please confirm. – asl May 23 '19 at 07:00
  • Having said that , I tried local File Detector as well. But couldn't get that to work as I get the same `Invalid argument: File not found`error. – asl May 23 '19 at 12:17
0
 ((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
 driver.get("https://www.monsterindia.com/seeker/registration");                 

 WebElement browser = driver.findElement(By.xpath("//*[@id=\"file-upload\"]"));                  
     //Upload button xpath
                        
   browser.sendKeys("add here upload file path");              
                        
   System.out.println("File upload Successfully");

This will also simple way to upload file. I have worked this code in Chrome browser.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

This code worked for me to run the test in BrowserStack. I'm using Java-selenium-Jenkins-BrowserStack

WebElement uploadFile = driver.findElement(By.xpath("//input[@type='file']"));
((RemoteWebElement)uploadFile).setFileDetector(new LocalFileDetector());
        upload_file.sendKeys(System.getProperty("user.dir")+"/src/main/java/resources/common/<Name of your file>.csv");
Anj Raju
  • 606
  • 7
  • 7