user3726499
user3726499

Reputation: 81

Upload file from webapp with selenium webDriver

My project tree

Project
    JavaRessources
        src/test/java
            myclass.java
    DeployedRessources
        webApp
            myFile.txt

in my class.java i want to upload myFile.txt with selenium webdriver:

driver.findElement(By.id("upload1")).sendKeys("myFile.txt");

when i do :

private void verifyFile(final String myId, final String src) {
        final WebElement file = this.driver.findElement(By.id(myId));
        final String value = file.getAttribute("value");
        assertEquals("name of uploaded file: ", src, value);
    }
 verifyFile("upload1", "myFile.txt");

it return : name of uploaded file: expected: [myFile.txt] but was []

so i dont know how to use relative path to upload this file with seleniumWebDriver

Thanks

Upvotes: 1

Views: 1483

Answers (2)

user3726499
user3726499

Reputation: 81

I solved my problem. it works very well. Below is my solution if it helps someone: the code below:

// Find path url of files to upload
final String pathDir = new java.io.File("").getAbsolutePath();
final String pathFile = pathDir + "\\src\\main\\webapp\\myFile.txt";
// End find path url of files to upload
driver.findElement(By.id("upload1")).sendKeys(pathFile);

Upvotes: 1

Vignesh Paramasivam
Vignesh Paramasivam

Reputation: 2460

As far i understand your question, the sendkeys you're sending only the file name. So try sending the absolute path of the file.

driver.findElement(By.id("upload1")).sendKeys("d:\\folder\\myFile.txt");

Edited according to your comment:

You need to use relative path of the file to reference it,

driver.findElement(By.id("upload1")).sendKeys("./webApp/myFile.txt");

Upvotes: 0

Related Questions