Selenium + Java (09): Use AutoIT tool to automate file uploads

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/qq_36396763/article/details/90748522

When using Selenium may meet the case "requires the use of pop-up window to upload a file, but not so that SendKeys, etc.", then this time we can use this convenient free automated tools AutoIT to achieve.

Principle:
1. Use the Windows AutoIT automation scripting upload files, and compiled into an executable .exe file
2. To upload files, the file can be called Selenium in.

Implementation:
1. Download & install AutoIT
Download: https://www.autoitscript.com/site/autoit/downloads/
installation process familiar basin Friends of the computer's open web glance understand it.
Do not know, then download and install the specific process may refer to a blog before: https://blog.csdn.net/qq_36396763/article/details/90741556
2. Start Script Editor & element locator.
Mounted implement described as follows:
Here Insert Picture Description
we open the element locators and script editor. FIG open:
Script Editor:

element locator:
Here Insert Picture Description
3. The captured information element
open file upload window, and the target-shaped button element locator to drag the file upload pop, may be captured corresponding to Class information and Title .
Here Insert Picture Description
Similarly, we can get information element "Open" button, retained to spare.
4. Edit the script
needs to be edited in the Script Editor.
Writing rules are as follows (all of the following elements of information can be made using the element locator):

ControlFocus("Title","","文件名输入框的Class+Instance")
ControlSetText("Title","","文件名输入框的Class+Instance","文件存放的路径")
ControlClick("Title","","‘打开’按钮的Class+Instance")

So if I want to upload the E drive with directory test.jpg picture, you can write a script in the Script Editor:

ControlFocus("文件上传","","Edit1")
ControlSetText("文件上传","","Edit1","E:\test.jpg")
ControlClick("文件上传","","Button1")

5. compiled into an executable file
first above written a good script is saved, for example, I will save it in the desktop named "FileUp.au3".
Here Insert Picture Description
Then click Edit above the script of "Tool-Compile",
we find into a FileUp.exe file saved FileUp.au3 script path swells.
Here Insert Picture Description
This file is a script that can be used to upload it.
6. Call the .exe file in Selenium in
now to the last step, we need to call the file in Selenium to upload image files to achieve:
Use the following three lines of code:

Runtime exe = Runtime.getRuntime();
	    String str = "E://FileUp.exe";
        exe.exec(str);

I can call here is successful, the results are as follows:
Here Insert Picture Description

If reported this error:
Here Insert Picture Description
please check your code is not wrong, such as the ControlSetText written ControlSetTest, will report this error.

Process needs more practice, is a return to attend to. Practice it ~

软件测试工程师一只,也在不断的学习阶段,平时的小经验不定期分享。
博主经验有限,若有不足,欢迎交流,共同改进~
愿与同在CSDN的你共同进步。

Guess you like

Origin blog.csdn.net/qq_36396763/article/details/90748522