简体   繁体   English

如何参数化 AutoIt 脚本以处理跨浏览器 window 弹出标题和文件?

[英]How to parametrize an AutoIt script to handle cross-browser window popup titles and files?

My AutoIt script should upload files using a browser:我的 AutoIt 脚本应该使用浏览器上传文件:

ControlFocus("Open","","Edit1")
ControlSetText("Open","","Edit1","C:\MWUploads\myFile.pdf")
ControlClick("Open","","Button1")

I'm using above script's compiled executable from Selenium WebDriver for Java:我正在使用从 Selenium WebDriver 为 Java 编译的上述脚本的可执行文件:

Runtime.getRuntime().exec(autoItScExecutableDir+"autoitScript.exe");

Depending on browser, title of upload popup window differs (for Google Chrome and Microsoft Edge it's "Open" whereas for Firefox it is "File Upload").根据浏览器,上传弹出窗口 window 的标题不同(对于 Google Chrome 和 Microsoft Edge,它是“打开”,而对于 Firefox,它是“文件上传”)。 How can I make it work for any file path on any browser, like below?如何使它适用于任何浏览器上的任何文件路径,如下所示?

Runtime.getRuntime().exec(myWinTitle, myLocator, myFileToUploadPath);

//attaching an img for more clarification, when tried as per Answer //根据答案尝试时附加一个img以获得更多说明

在此处输入图像描述

Here is the o/p of sysOut from else block-这是来自 else 块的 sysOut 的 o/p-

E:\AOS\src\test\resources\AutoItScripts\dynamicAutoItScript.exe File Upload 
E:\AOS\src\test\resources\FilesToUpload\Greetings.png

here is what happening in actual-这是实际发生的事情- 在此处输入图像描述

You can send parameters to the autoitScript.exe.您可以将参数发送到 autoitScript.exe。

Cross browser differences跨浏览器差异

The Upload Window changes based on the browser:上传Window根据浏览器变化:

  1. Firefox -> the upload window title is: File Upload Firefox -> 上传 window 标题为:文件上传
  2. Chrome and Edge -> the upload window title is: Open Chrome 和 Edge -> 上传 window 标题为:打开
  3. Internet Explorer -> the upload window title is: Choose File to Upload Internet Explorer -> 上传 window 标题为:选择要上传的文件

However these remain the same:然而,这些保持不变:

  • The location bar (ToolbarWindow32 on windows, the one on top)位置栏(windows 上的 ToolbarWindow32,顶部的那个)
  • File Name bar文件名栏
  • Open Button打开按钮

The AutoIt script is: AutoIt 脚本是:

Sleep(500);
If $CmdLine[0] > 0 Then
   ControlFocus($CmdLine[1],"","Edit1")
   ControlSetText($CmdLine[1],"","Edit1",$CmdLine[2])
   ControlClick($CmdLine[1],"","Button1")
EndIf

Parameters explained:参数说明:

Sleep(500);睡眠(500); is a half of a second wait.是半秒的等待。 I encounter some issues on an old pc's with a slow HDD.我在硬盘速度慢的旧电脑上遇到了一些问题。 As a result the upload window didn't open in time for the script to be executed.结果,上传 window 没有及时打开以执行脚本。 Better safe then sorry.最好安全然后抱歉。

$CmdLine[0] contains the number of parameters sent. $CmdLine[0] 包含发送的参数数量。 It will contain 0 if no parameter was sent.如果没有发送参数,它将包含 0。

$CmdLine[1] - represents the upload window title $CmdLine[1] - 代表上传 window 标题

$CmdLine[2] - represents the text sent in the File Name bar $CmdLine[2] - 代表文件名栏中发送的文本

Usage in Java: Java 中的用法:

Firefox: Runtime.getRuntime().exec("E:\\compiledScript.exe \"File Upload\" E:\\file.txt"); Firefox: Runtime.getRuntime().exec("E:\\compiledScript.exe \"File Upload\" E:\\file.txt");

Chrome: Runtime.getRuntime().exec("E:\\compiledScript.exe Open E:\\file.txt"); Chrome: Runtime.getRuntime().exec("E:\\compiledScript.exe Open E:\\file.txt");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM