November 20, 2019

How to use AutoIT and Send Keys to do File upload in selenium

  November 20, 2019
As we all know, selenium being a web automation tool it has no inbuilt option use the windows dialog-based operations.


So we have two ways to do a file upload action on windows dialog.
  1. Using SendKeys
  2. Using the AutoIT Tool
Now we will look through details on the implementation of each of these

Using SendKeys

We can use the basic Send Keys action of selenium to upload a file. Simply we can send the local path of the file to the input type button inside the web page. This will set the path in the input box.

But this option is available only if the web element to upload the file is of input type.

Sample below is given in Selenium C#
var dir = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
string strFilePath = dir + "\\FileUpload\\" + "TestFile.pdf";

Console.Out("The file path is " + strFilePath);
driver.FindElement(By.XPath("//input[@type='file' and @name='file1']").SendKeys(strFilePath);

Using AutoIT Tool

AutoIt is an open-source automation language for Windows operating system, It uses a combination of mouse movement, keystrokes, and window control manipulation to automate a task which is not possible by selenium web driver.

In AutoIT also we have to keep the file to upload inside the project or in the system then get the file path.

In Java, we can use the below code to do this.

String FPath=URLDecoder.decode(this.getClass().getResource("/Files/TestFile.pdf").getPath(), "UTF-8");
if (FPath.startsWith("/"))
FPath=FPath.substring(1,FPath.length()); 
FPath=FPath.replaceAll("/", "\\\\");  
System.out.println("The file path is "+FPath);

Now we need to write a code to file with the extension.Au3 and execute it using AutoIT to create an exe application.

While creating the .exe, we will be passing the file path also with it. So now this .exe file will act as an independent application that will click on the file upload button and do the action inside the windows dialog.

Sample.Au3 file to upload a file is given below (Works in Chrome, Firefox and IE)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
sleep(1000)
If WinExists("[TITLE:Open]") Then

 Local $hWnd = WinWaitActive ("[TITLE:Open]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("Open","","Edit1")
 ControlsetText("Open","","Edit1",$CmdLine[1])
 ControlClick("Open","","Button1")

ElseIf WinExists("[TITLE:File Upload]") Then

 Local $hWnd = WinWaitActive ("[TITLE:File Upload]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("File Upload","","Edit1")
 ControlsetText("File Upload","","Edit1",$CmdLine[1])
 ControlClick("File Upload","","Button1")

Else

 Local $hWnd = WinWaitActive ("[TITLE:Choose File to Upload]", "",15)
 WinActivate($hWnd)
 ;WinWaitActive("Open", "", 10)
 ControlFocus("Choose File to Upload","","Edit1")
 ControlsetText("Choose File to Upload","","Edit1",$CmdLine[1])
 ControlClick("Choose File to Upload","","Button1")

EndIf

We can pass the File name through the parameter $CmdLine[1].
To trigger the exe file, use the below code.


String sExe=(<EXE file path>+" "+<Upload file path>);
  
Runtime.getRuntime().exec(sExe);
Thread.sleep(5000);

In the automation flow, this .exe can be triggered when we reach the page with the file upload button is displayed. It is preferred to keep the .exe file and the file to upload inside the project.

We can keep the .Au3 file also in the project, in case if AutoIT is installed we will be able to modify it. According to the use and scenario, we can modify the .Au3 file to handle any kind of Windows dialog actions.

Use can refer https://www.autoitscript.com/site/autoit/ for more detailed documentation on AutoIT.

logoblog

Thanks for reading How to use AutoIT and Send Keys to do File upload in selenium

Previous
« Prev Post

No comments:

Post a Comment

Fixing javascript error: Cannot read properties of null (reading 'querySelector') issue when using CSS identifier in selenium

 JavaScript is another way to interact with web elements when normal selenium methods fail to act. But one issue with javascript is, it does...