Home > Automation Testing > Advance Selenium > File Upload Using Windows Automation Tool And Selenium

File Upload Using Windows Automation Tool And Selenium

This post was most recently updated on July 29th, 2024

Here are the step to upload file using selenium

Click on Select file / Select button (This part could be handled using Selenium)
Select file from open dialogue box(This part could not be handled using selenium)
Click on Upload or Upload file button.
File Upload for INPUT tag with TYPE =”file”

Example Url:

If Select file button is input tag like

Create element for file upload input box

@FindBy(how = How.NAME, using = “upfile”) WebElement fileUpload;

Send file path sendKeys(CharSeq..) function

fileUpload.sendKeys(filePath);

Not every time Input tag used for file upload in that case we need to handle window’s open file dialog box.

In that case below mentioned solution will be useful.

File Upload for Other tags.

In this case we need handle OpenFileDialog box which will be opened after click on select file button.

To handle OpenFileDialog we required following tools

WinAppDriver UI Recorder v1.1 Used to find windows locator.
Winium.Desktop.Driver.exe Used to handle windows elements.
Java Library required
winium-webdriver-0.1.0-1.jar
winium-elements-desktop-0.2.0-1.jar
How to use WinAppDriver UI Recorder V1.1?

Unzip downloaded WinAppDriverUIRecorder.zip – ZIP archive and open “WinAppDriverUiRecorder.exe” file.
Open url “https://cgi-lib.berkeley.edu/ex/fup.html” and click on “choose file” select file open dialog box will be opened.
Click on “Record” Button in WinAppDriverUiRecorder.
Move mouse cursor to file name input box OR open button wait until button or input box highlighted, then click on pause button. It will generate xpath, we could use class name or name from that xpath.
Example:

Project Structure and code

Create java project and add Selenium jar files and TestNG library.
Add downloaded jars to the project winium-webdriver-0.1.0-1.jar, winium-elements-desktop-0.2.0-1.jar
Create page object model for file upload and write TestNg script for file upload.
Here is project structure:

Note: Run Winium.Desktop.Driver.exe before executing script and allow public access on windows security alert.

File upload page object model implementation

package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

public class FileUploadPage {

WebDriver driver;

public FileUploadPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}

@FindBy(how = How.NAME, using = “upfile”)
WebElement fileUpload;

@FindBy(how = How.NAME, using = “note”)
WebElement fileUploadNote;

@FindBy(how = How.XPATH, using = “//input[@value=’Press’]”)
WebElement pressButton;

public void clickPressButton() {
pressButton.click();
}

public void sendFile(String filePath) {
fileUpload.clear();
fileUpload.sendKeys(filePath);
}

public void enterNote(String uploadNote) {
fileUploadNote.sendKeys(uploadNote);
}

public void clickChooseFileButton() {
fileUpload.click();
}
}
TestNG Test script

package script;

import java.awt.AWTException;
import java.io.IOException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.winium.DesktopOptions;
import org.openqa.selenium.winium.WiniumDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import pages.FileUploadPage;

public class TestScript {
WebDriver driver;
FileUploadPage uploadPage;

@BeforeTest
void setupBrowser() throws IOException {
System.setProperty(“webdriver.chrome.driver”, “./chromedriver.exe”);
this.driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://cgi-lib.berkeley.edu/ex/fup.html”);
uploadPage = new FileUploadPage(driver);
}

@Test
void uploadFile() throws InterruptedException, AWTException, IOException {
String filePath = “./TEST.txt”;
String fileUploadNote = “Testing upload”;
// Thread.sleep(5000);
// click On select file button
uploadPage.clickChooseFileButton();
// execute Winium.Desktop.Driver.exe it will run on http://localhost:9999
Runtime.getRuntime().exec(“./Winium.Desktop.Driver.exe”);
// Create object of DesktopOptions
DesktopOptions options = new DesktopOptions();
// setDebugConnectToRunningApp set to True it will work on existing open
// application
options.setDebugConnectToRunningApp(true);
// Create object for WiniumDriver
WiniumDriver driverWin = new WiniumDriver(new URL(“http://localhost:9999”), options);
// Send file path to FileName input box in open dialog
driverWin.findElementByName(“Edit”).sendKeys(filePath);
// Find list of buttons
List buttons = driverWin.findElementsByClassName(“Button”);
for (int i = 0; i < buttons.size(); i++) { //System.out.println("buttons.get(i).getAttribute(\"Name\") " + buttons.get(i).getAttribute("Name")); // Click on button if it match the Name i.e. Open if (buttons.get(i).getAttribute("Name").matches("Open")) { buttons.get(i).click(); break; } } // close winium driver driverWin.close(); // Kill process Winium.Desktop.Driver.exe Runtime.getRuntime().exec("taskkill /F /IM Winium.Desktop.Driver.exe"); // enter note using selenium web driver uploadPage.enterNote(fileUploadNote); // Thread.sleep(5000); //used to pause script // click on press button to upload the file uploadPage.clickPressButton(); } @AfterTest void closeBrowser() { driver.quit(); } } TestNg Xml configuration file to execute script using TestNG Framework










This Article is TAGGED in , , . BOOKMARK THE permalink.

Avatar photo
Neeraj Vishwakarma
Senior QA Engineer Skills: Automation Testing, Manual Testing, STLC, Java, Selenium Webdriver, Protractor, Database Testing.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">