0

I'm trying to upload a local file (C:\sample.txt) to my server. I have tried to implement this with Chrome web driver and its working absolutely fine. But during implementing the same with HTMLUnitDriver, i couldn't browse the file item from my local disk. I tried the below two methods as well,

1) Send keys:

        WebElement inputFile = driver.findElement(By.id("file"));
        System.out.println(driver.getCurrentUrl());
        LocalFileDetector detector = new LocalFileDetector();
        String path = "C:\\UploadSample1.txt";
        File f = detector.getLocalFile(path);
        inputFile.sendKeys(f.getAbsolutePath());

2) Using a Robot:

        WebElement browseFile = fluentWait(By.id("browseFile"), driver);
        browseFile.click();
        File file = new File("C:\\UploadSample1.txt");
        driver.switchTo().activeElement();
        StringSelection fileNameToWrite = new StringSelection(
                file.getAbsolutePath());
        Toolkit.getDefaultToolkit().getSystemClipboard()
                .setContents(fileNameToWrite, null);
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);

I need the file item to be browsed, then only i can save it to my server. Because just sending the file path will be searching the file in server disk. Now i'm really stuck and couldn't move further.

Any help is highly appreciated. Thankyou!

Diya
  • 43
  • 10

2 Answers2

0

If you need to browse to the file first, that isn't possible IMHO; for that you will need AutoIT (as Robot class is not recommended). So your best bet would be sending file path using sendKeys.

Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
  • Thanks Kushal for your reply. Actually i tried passing the file path using sendKeys, but while on server, its actually looking for the file in the server system, not in my local. Could you please suggest any workaround for that? – Diya Jan 25 '17 at 08:32
0

formInput.setValueAttribute(formValue); worked fine for me.

Code snippet:

Iterator<String> formValueIterator =  formValues.keySet().iterator();
while(formValueIterator.hasNext()){
    String formKey = formValueIterator.next();
    String formValue = formValues.get(formKey);

    HtmlInput formInput =  form.getInputByName(formKey);

    if (formInput != null)
        if (formInput instanceof HtmlPasswordInput) {
            ((HtmlPasswordInput)formInput).setValueAttribute(formValue);
        } else {
            formInput.setValueAttribute(formValue);
        }

}
Pang
  • 9,564
  • 146
  • 81
  • 122
Ravikumar
  • 57
  • 3
  • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written – WhatsThePoint Jul 14 '17 at 07:24