Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Selenium WebDriver is a tool that can automate user interactions with web browsers for testing web applications. It automates processes such as file upload, getting input value, scrapping HTML content, etc.

In this tutorial, we’ll explore how to upload a file using the sendKeys() method in Selenium.

2. Uploading Files Using sendKeys()

Simply put, file upload is a common feature in many web applications. However, file upload can be tricky to test with Selenium WebDriver because it involves interacting with the native file system of the operating system. To overcome this challenge, we can use the sendKeys() method.

The sendKeys() method helps to simulate keyboard action. This method can send data as input to form elements in HTML.

sendKeys() accepts String as an argument and inserts it into a selected HTML element. It’s an important method in automation testing. Common use cases include filling out a web form and searching for a specific item on a web page.

In this tutorial, we’ll use the sendKeys() to fill a web form with a focus on uploading a file to a web page. Let’s see an example of uploading an image file using sendKeys():

class FileUploadWebDriverUnitTest {
    private WebDriver driver;
    private static final String URL = "http://www.csm-testcenter.org/test?do=show&subdo=common&test=file_upload";
    private static final String INPUT_NAME = "file_upload";
    
    @BeforeEach
    public void setUp() {
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
    }
    
    @AfterEach
    public void tearDown() {
        driver.quit();
    }
    
    @Test
    public void givenFileUploadPage_whenInputFilePath_thenFileUploadEndsWithFilename() {
        driver.get(URL);
        
        String filePath = System.getProperty("user.dir") + "/1688web.png"; 
        WebElement inputElement = driver.findElement(By.name(INPUT_NAME));
        WebElement submitButton = driver.findElement(By.name("http_submit"));
        
        inputElement.sendKeys(filePath);
        String actualFilePath = inputElement.getAttribute("value");
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
       
        WebElement submitButton = driver.findElement(By.name("submit"));
        submitButton.click();
        Assert.assertTrue(actualFilePath.endsWith(fileName));
    }
}

To begin with, we configure WebDriver to use Mozilla Firefox and write a teardown() method to close the browser when the test is done. Next, we declare a field called URL that holds the URL “http://www.csm-testcenter.org/test do=show&subdo=common&test=file_upload“, where we can upload the file. Then we find the name attribute of the HTML input element that accepts the file. The image resides in the project’s root directory.

Moreover, we create the WebElement instances to access the name attribute of the input field and the submit button. Also, we specify the file path and invoke the sendKeys() method on the inputElement to enter the image path in the input field.

Finally, we initiate the upload action by performing a mouse click on submitButton. We verify that the uploaded file has the same name and extension as the original file.

3. Conclusion

In this article, we learned how to upload files using Selenium WebDriver. Additionally, we use the sendKeys() method to send commands to an HTML input element. This skill is useful for automating web testing and interacting with different web elements.

As always, the complete source code for the example is available on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.