All IT Courses 50% Off
Uncategorized

File Upload and Download using Selenium Webdriver

In this article, we will learn how to handle file uploads and downloads.

Uploading Files

To Upload files, we will use http://the-internet.herokuapp.com/upload as our test application. Through this site any user can upload files without requiring them to sign up.
In Selenium Webdriver we can Upload files by simply using the method sendKeys() on the select file input field to enter the path to the file to upload.

upload.PNG

Let’s create a test case in which we will automate the following scenarios to handle File-Upload:

  • Invoke a Google chrome browser.
  • Open URL: http://the-internet.herokuapp.com/upload
  • Click on Choose file to upload a file.
  • Click on Upload button

Now, we will create a test case step by step in order to understand of how to handle file-upload in WebDriver.

Step 1: Launch Eclipse IDE.

Step 2: Go to File > New > Click on Java Project.

Enter Project Name: Demo_FileUpload

File.PNG

Step 3: Right click on the Project Name and click on the New > class.

PNFileupload.PNG

Give your Class name as “Test_Fileupload” and Select the checkbox “public static void main(String[] args) and click on “Finish” button.

Uploadclass.PNG

Step 4: Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.

Here is the sample code to set Chrome driver system property:

// System Property for Chrome Driver   

 System.setProperty(“webdriver.chrome.driver”, “ D:\\Drivers\\geckodriver.exe “); 

 

After that we have to initialize the Chrome driver using ChromeDriver Class. Below is the sample code to initialize Chrome driver using ChromeDriver class.

// Instantiate a ChromeDriver class.      

WebDriver driver=new ChromeDriver(); 

 

We will get the below code to launch Google Chrome browser after combining both of the above codes.

System.setProperty(“webdriver.chrome.driver”, “ D:\\Drivers\\geckodriver.exe “);  

WebDriver driver=new ChromeDriver();

After that we need to navigate to the desired URL.

Below is the sample code to navigate to the desired URL:

// Launch Website  

driver.navigate().to(“http://the-internet.herokuapp.com/upload“);

Here is the complete code for above scenario:

import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
public class Test_Fileupload {  
  
    public static void main(String[] args) {  
          
        // System Property for Chrome Driver   
 System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");  
  
    String baseUrl = ("http://the-internet.herokuapp.com/upload");  
    WebDriver driver=new ChromeDriver();  
  
     driver.get(baseUrl);
    }    
} 
Facebook Comments
Tags

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button
Close
Close