The Easy way of uploading a file is simple case of just finding the element and typing the absolute path of the document into it.
It is mandatory that it works only when the textbox is enabled. So please make sure that the input element is visible.
Sample HTML Code should look similar to this :
<html>
<body>
<form enctype="multipart/form-data" action="parse_file.php" method="post">
<p>Browse for a file to upload: </p>
<input type="file" name="uploadsubmit">
<br/><br/>
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
The following is the example program to upload a file using sendKeys() in selenium webdriver without using any third party tools:
package com.easy.upload;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class UploadFile {
static WebDriver driver;
String URL = "application URL";
@Test
public void testUpload() throws InterruptedException
{
driver = new FirefoxDriver();
driver.get(URL);
WebElement element = driver.findElement(By.name("uploadsubmit"));
element.sendKeys("D:/file.txt");
driver.findElement(By.name("uploadSubmit"));
String checkText = driver.findElement(By.id("message")).getText();
Assert.assertEquals("File uploaded successfully", checkText);
}
}
Selenium Tutorials:
Add new comment