banner



How To Handle File Upload Window In Selenium Webdriver Java

Today, nosotros've come with three exam automation techniques that you tin can utilise to handle file upload in your Selenium Webdriver projects.

Nosotros've practically verified all of these file upload methods, and you should be able to adapt them quickly. These are three unique ways that you can understand and integrate hands with your code.

In this post, we'll bring AutoIt to your discover which is a Windows GUI automation tool and share its potential to handle file upload workflows in a spider web application. You'll also get to know about the Jacob COM interface and run across how to apply information technology with Selenium to interact with the file upload dialog.

Handle File Upload In Selenium – 3 Techniques

Contents

  • 1 Prerequisites and Required Tools
  • 2 File Upload Grade for Offline Testing
  • 3 File Upload Techniques by TechBeamers
    • three.1 SendKeys to Handle File Upload
    • 3.2 AutoIt Script to Handle File Upload
    • 3.3 Jacob API to Handle File Upload

Before we step forward, it'south important to understand the prerequisites and list downwards the tools that you'll need to employ the above file upload tricks. Please bank check out the necessary details below.

3 Unique Ways to Handle File Upload in Selenium Webdriver
three Unique Ways to Handle File Upload

Prerequisites and Required Tools

i- You'll either have to create a uncomplicated Selenium Webdriver projection or add a TestNG projection in Eclipse. Read any of the below posts if you want to know how to practice it from scratch.

a. How to create a Selenium Webdriver projection in Eclipse?
b. How to build your start TestNG test case in Eclipse?

2- Download the AutoIt software from here and install it on your organization. If yous are using a 64-bit system, then do select the suitable option during installation. Also, brand sure you lot don't copy the AutoIt files from some other arrangement, instead, permit the installer run. It not just installs all the required stuff but also registers them with your machine. It's helpful in the application where y'all use AutoIt interfaces to piece of work with the window based dialogs.

Important Annotation:We suggest to use the tools that are consistent with the architecture of the Os you lot are using. Like, for 64-flake systems, install the JDK and Eclipse of the same flavor. If you follow this arroyo, you'll eventually reduce the chances of whatsoever platform-specific event while edifice your applications.

three- Download the AutoItX4Java jar file which is a Java API wrapper for the AutoIt functions. Apply the Eclipse < Build Path->Configure > pick to Add this file as an external library in your project.

four- Download the Jacob library package, it comes as a nix file. Unzip the < jacob-1.18.zip > (or <jacob-i.19.aught> whichever is bachelor) file. You'll find the post-obit files under the < jacob-i.18 > folder.

  • jacob.jar.
  • jacob-one.xviii-x64.dll.
  • jacob-1.xviii-x86.dll.

Add together the < jacob.jar > every bit an external jar into your Eclipse project while copy/paste the < jacob-1.18-x86.dll > to your project. It'll get the DLL file added to it.

Now you are all set to expect over the special techniques to handle file upload in Selenium Webdriver. Let'south commencement to review them 1 by one.

File Upload Form for Offline Testing

We'll be using the post-obit HTML code to examination the file upload techniques. Please salve it as < SeleniumWebdriverUploadFile.html > and copy/paste this file into the Eclipse project. So that we tin can readily determine the path of this file past using Coffee's < user.dir > Organization property. Nosotros'll load this file from the sample code provided under each technique.

<!DOCTYPE html> <html>  <body> <class>     Choice Any File to upload:     <input type="file" name="uploadfile" id="uploadfile"> </grade>  </torso> </html>        

File Upload Techniques by TechBeamers

Handling file uploads is a complicated automation chore. Just you tin manage to do it with ease using the below three methods.

SendKeys to Handle File Upload

Information technology's the nearly basic technique to perform the upload of a file.

Get the file upload element either by using the Id or Proper noun. And call the Webdriver'due south sendKeys() method to set the value of the file to upload. Cheque out the beneath code for clarity. You can add together it as is to your projection. Delight make sure to replace the Id/Name accordingly.

          public void UploadFileUsingSendKeys() 			throws InterruptedException {  		commuter = new FirefoxDriver(); 		String workingDir = Organisation.getProperty("user.dir"); 		String filepath = workingDir + "\\SeleniumWebdriverUploadFile.html"; 		driver.become(filepath);  		WebElement fileInput = driver.findElement(Past.name("uploadfile")); 		fileInput.sendKeys(filepath);  		// Added a wait to brand yous notice the deviation. 		Thread.sleep(1000);  		driver.findElement(By.id("uploadfile")).sendKeys( 				"C:\\path\\to\\fileToUpload.txt");  		// Added sleep to make yous come across the difference. 		Thread.sleep(1000);  		fileInput.sendKeys(filepath); 	}

AutoIt Script to Handle File Upload

Firstly create a new file in your Eclipse project and proper noun it as < File_upload_selenium_webdriver.au >. Add the following AutoIt script code into the newly created file. This script accepts a unmarried argument which is the path of the file to upload. Information technology'll help united states handle the file upload window. We'll execute this file from Java code using the <Runtime.getRuntime().exec()> method.

WinWaitActive("File Upload")  Transport($CmdLine[1]) Send("{ENTER}")        

Now, discover out the Java code that opens the file upload window and runs the AutoIt script mentioned above to acquit out the file upload.

          public void UploadFileUsingAutoIt() 			throws InterruptedException, IOException {  		String workingDir = System.getProperty("user.dir"); 		String autoitscriptpath = workingDir + "\\" 				+ "File_upload_selenium_webdriver.au";  		commuter = new FirefoxDriver(); 		String filepath = workingDir + "\\SeleniumWebdriverUploadFile.html"; 		driver.get(filepath);  		// Added a wait to make y'all observe the difference. 		Thread.sleep(thou);  		commuter.findElement(By.id("uploadfile")).click();  		// Added sleep to make you see the difference. 		Thread.sleep(one thousand);  		Runtime.getRuntime().exec( 				"cmd.exe /c Start AutoIt3.exe " + autoitscriptpath + " \"" 						+ filepath + "\""); 	}

Jacob API to Handle File Upload

It'south a native API technique that gives y'all full control to write Java code that can manage whatever GUI Window. But y'all need to make certain all the prerequisites and tools are in identify before you lot get to run the beneath code.

          public void UploadFileUsingJacobDll() 			throws InterruptedException {  		Cord workingDir = System.getProperty("user.dir");  		terminal String jacobdllarch = Organization.getProperty("sun.curvation.data.model") 				.contains("32") ? "jacob-ane.eighteen-x86.dll" : "jacob-1.eighteen-x64.dll"; 		String jacobdllpath = workingDir + "\\" + jacobdllarch;  		File filejacob = new File(jacobdllpath); 		Organisation.setProperty(LibraryLoader.JACOB_DLL_PATH, 				filejacob.getAbsolutePath()); 		AutoItX uploadWindow = new AutoItX();  		driver = new FirefoxDriver(); 		String filepath = workingDir + "\\SeleniumWebdriverUploadFile.html"; 		driver.become(filepath);  		Thread.slumber(1000);  		driver.findElement(Past.id("uploadfile")).click();  		Thread.slumber(1000);  		if (uploadWindow.winWaitActive("File Upload", "", five)) { 			if (uploadWindow.winExists("File Upload")) { 				uploadWindow.sleep(100); 				uploadWindow.transport(filepath); 				uploadWindow.controlClick("File Upload", "", "&Open");  			} 		} 	}

We wish the above tricks would have given you a new perspective to handle file upload windows, and you'll use them more efficiently in your Selenium projects.

If y'all knew a different approach to piece of work with the file uploads, and so please let us know. It'll aid usa connect you more than closely, and nosotros'll publish information technology to enlighten our readers.

All the Best,

TechBeamers

Source: https://www.techbeamers.com/handle-file-upload-selenium-webdriver/

Posted by: shackelfordsaut1997.blogspot.com

0 Response to "How To Handle File Upload Window In Selenium Webdriver Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel