简体   繁体   中英

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

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);

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM