Selenium RC 用AutoIt实现IE里上传和下载窗口的操作

Selenium RC 用AutoIt实现IE里上传和下载窗口的操作
网上瞅见的一篇文章,关于用Selenium RC操作上传、下载对话框的,需要用到第三方的一个软件AutoIt3,还要写一些AutoIt3脚本,然后在Selenium脚本中调用AutoIT3程序并执行AutoIt3脚本,本人没确认过,谁如果哪天用了并成功解决(希望可以解决!),来这告诉大家!~做第一个吃螃蟹的人吧!~
AutoIT3的网站:http://www.autoitscript.com 可以在此处下载该软件并安装,安装后目录下\Examples里有很多脚本实例。

操作AutoIt脚本的方法:
public void executeAutoItScript(String script) throws Exception {
//下边三行可能无法使用,如果在Eclipse里面可能找不到相应的类。可以使用Runtime相应的getRuntime、exec方法实现下面的调用
Command command = new Command();
command.setCmd(new String[]{"\"C:\\AutoIt3\\AutoIt3.exe",script});
Execute.execute(command, true, true, true, true);

//下边代码就是你Selenium相关操作代码,最好将AutoIt的操作代码放放在你要操作的Selenium动作之前
code.....
}
在IE下操作下载文件的AutoIt脚本:download.au3(autoit3的文件格式)
【下面的代码是直接可以拿来用的,只要你的窗体是英文,如果是中文窗体,将文字改成相应的显示文字就可以了】
WinWait("File Download");
WinActivate("File Download");
ControlClick("File Download", "","[CLASS:Button; INSTANCE:2]");
WinWait("Save As");
WinActivate("Save As");
ControlSetText("Save As", "", "[CLASS:Edit; INSTANCE:1]", "c:\ddd.bmp")
ControlClick("Save As", "","[CLASS:Button; INSTANCE:2]");
WinWait("Download complete");
WinActivate("Download complete");
ControlClick("Download complete", "","[CLASS:Button; INSTANCE:4]");
This code should be run in thread before you press on the download link.

原文如下:
Recently,I had the challenge of writing some automation for a workflow which includeduploading a file, and then downloading a file later in the workflow. The team with whom I am working had used some Selenium fortheir tests, and preferred to keep doing so, if possible, but their effortsprior to my arrival on the team to get the upload/download portions of the codeworking with Selenium had been unsuccessful.
The problem basically boils down to twopoints:
The JavaScript interpreters in mostbrowsers have specific protections built in to prevent JavaScripts from filling in “file”fields, such that when they tried “type” “filefield” “myfile.txt”, nothingwould happen in the browser.
When attempting to use the click commandon the “browse” buttonof the field, moreover, causes a modal dialog which holds focus such that theclick command does not return, because it is waiting for feedback from thebrowser, until the modal file picking dialog closes.
So, you can’t type into the file field,and you can’t click on it to open the file dialog, because even if you can typeinto it, using native key pressing, or another automation tool such as AutoItor AutoHotKey, which can grab window focus, and send typing events, you’restill kind of hosed, because until your script returns from the click event,which it won’t because there’s this modal window stuck out there, blocking it. KeyDown and KeyUp don’t triggerany action in the control, good or bad.
So what to do? Well, there are solutions, evenfairly easy ones, but they are probably going to involve at least a littleplatform and browser-specific kludges, which you can hopefully write intofunctions, and then never worry about again. In some browsers, I was ableto use the focus(locator) command, and then do native system typing withKeyPressNative, or AutoIt, which allows me to grab the appropriate windowfirst, and simply send the desired text. However, in some browsers, thiswill not work, as the field will bring up the dialog when you begin to typeinto the field. Below, you can find my solution.
System.out.println(”focusingon file field”);
selenium.focus(”uploadfile”);
if(T1.Browser.startsWith(”*ie”))
{
System.out.println(”Doingthe ie tabbing”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyDownNative(java.awt.event.KeyEvent.VK_SHIFT+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyUpNative(java.awt.event.KeyEvent.VK_SHIFT+ “”);
System.out.println(”Submittingfile: ” + fname);
Processfileup = Runtime.getRuntime().exec(”C:\\autoit\\install\\AutoIt3.exe C:[url=file:/testfiles/scripts/submitFileName.au3]\\testfiles\\scripts\\submitFileName.au3[/url]\”" + fname + “\”");
fileup.waitFor();
selenium.click(”ok”);
}else {
if(T1.Browser.startsWith(”*firefox”))
{
getSystemFocus(”PCMS”);
selenium.focus(”uploadfile”);
System.out.println(”Doingthe firefox tabbing”);
selenium.setSpeed(”200″);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_TAB+ “”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_SPACE+ “”);
getSystemFocus(”PCMS”);
System.out.println(”SpacePressed”);
System.out.println(”Submittingfile: ” + fname);
Processfileup = Runtime.getRuntime().exec(”C:\\autoit\\install\\AutoIt3.exe C:[url=file:/testfiles/scripts/submitFileNameFF.au3]\\testfiles\\scripts\\submitFileNameFF.au3[/url]\”" + fname + “\”");
fileup.waitFor();
selenium.focus(”ok”);
selenium.keyPressNative(java.awt.event.KeyEvent.VK_SPACE+ “”);
System.out.println(”Dismissingthe security warning”);
Processsecwarning = Runtime.getRuntime().exec(”C:\\autoit\\install\\AutoIt3.exe C:[url=file:/testfiles/scripts/secwarning.au3%E2%80%B3]\\testfiles\\scripts\\secwarning.au3″[/url]);
secwarning.waitFor();
System.out.println(”Dismissingthe security warning”);
selenium.setSpeed(”1200″);
}
There are a couple of small problems withthis solution as a general solution, but it does work for multiple browsers onWindows. Not the leastproblem is that this will fail when running on a non-local RC server. That can be gotten around by calling psexec or some similar command on thehostname of the rc server, but this works only in a Windows setup. Fornon-windows machines, a similar strategy could be done, and you could probablyeliminate AutoIt and any outside tool by calling keyPressNative commands foreach character of the file string, but you may run into difficulties withsystem focus of the window.
What would be ideal is a java tool whichcan grab window focus, and then type in the appropriate file string, given asparameters, but I wasn’t able to find or implement both of these requirementsin sufficient time.

注:AutoIt经过最近两天的使用,完全可以实现如题的功能。希望在windows环境下,对于大家是个帮助。大家可以讨论遇到的问题!~





vipid 2009-06-02 12:09
支持一下。


vipid 2009-06-02 12:13
AutoIt 我原来也初略的研究过,最初用来实现 firefox 证书弹出窗口的时候 也用 autoit 。
但是那时候只是写了一个autoit的程序,让他监控在那里,发现有证书弹出窗口就主动点击。

如果需要嵌入到java代码里面去,好像有一个叫JAutoIt 的jar包提供一些api。。但是当时用起来好像效果不好。不知道现在开发的怎么样了。

有兴趣的可以去尝试一下。解决一下IE里面的一些问题。。


captain 2009-06-02 15:09
顶……


liate 2009-06-02 22:26
看名字,希望是好文。


lishijia 2009-06-03 09:59
顶。。


看雪时节 2009-06-19 10:26
此问题已成功验证,可以实现相关功能,而且AutoIt也做的相当不错,大家可以照帖子描述实现你的相应功能。


white_kaweh 2009-06-19 11:20
支持啊!好文章。学习了


李村民 2009-07-02 23:16
估计最近会使用到这个东西,先记住一下,感谢楼主分享!


abiao610 2009-07-09 16:55
貌似会游泳!支持一下!


haigui 2009-07-09 17:00
高手 牛逼文章 谢过先


haigui 2009-07-15 15:43
问下这个东西能不能处理输ID和密码的登陆框??谢了!


vipid 2009-07-15 16:01
怎么样的登录框,最好贴个图上来。
引用
引用第11楼haigui于2009-07-15 15:43发表的 :
问下这个东西能不能处理输ID和密码的登陆框??谢了!



haigui 2009-07-15 16:12
就是一个windows框 seleniumIDE不支持它 贴图 我也很想贴 但是公司不让 说是商业机密!!!


看雪时节 2009-07-20 20:54
引用
引用第13楼haigui于2009-07-15 16:12发表的 :
就是一个windows框 seleniumIDE不支持它 贴图 我也很想贴 但是公司不让 说是商业机密!!!


如果是Windows的对话框,应该是可以的。只要你按我类似的方法做就可以了!


xiawared 2009-09-23 18:03
Command command = new Command();
command.setCmd(new String[]{"\"C:\\AutoIt3\\AutoIt3.exe",script});
Execute.execute(command, true, true, true, true);
这里的Command 是抽象类啊,楼主是怎么验证的。能否详细一点啊?


看雪时节 2009-09-23 19:38
//下边三行可能无法使用,如果在Eclipse里面可能找不到相应的类。可以使用Runtime相应的getRuntime、exec方法实现下面的调用

看代码上边的绿色字体的描述,如上


xiawared 2009-09-24 09:48
关于参数script,是script 的目录地址,还是script的内容尼?请赐教


看雪时节 2009-09-24 11:26
我不知道你怎么看的,我说用RunTime类方法代替Command类,你却依然问script参数

Runtime rt = Runtime.getRuntime();
rt.exec(new String[]{autoItPath,scriptPath});


xiawared 2009-09-24 13:09
script参数肯定也是runtime的那个参数啊,我没有问错娄。


vipid 2009-09-24 13:28
算了。还是给你一个例子
复制代码
Runtime r = Runtime.getRuntime();
r.exec("D:\\Program Files\\AutoIt3\\AutoIt3.exe D:\\hello.au3");



不过这个东西,自己试试不就知道了吗?还需要花更多的时间来发个帖,然后等回复吗?
另外说一下。 r.exec 里面的参数内容就是 你平时用cmd打开一个dos窗口。然后输入 一串命令,这串可执行的命令 就是这个参数的值。当然特殊字符需要转换一下。


引用
引用第19楼xiawared于2009-09-24 13:09发表的 回 18楼(看雪时节) 的帖子 :
script参数肯定也是runtime的那个参数啊,我没有问错娄。



xiawared 2009-09-24 14:14
当然会自己试试拉。万一很长时间试不出来再来问,不是浪费时间嘛。


xiawared 2009-09-24 17:08
楼主,submitFileName.au3 内容哪里有?


bruce 2009-09-24 17:13
submitFileName.au3 ? 什么意思。r.exec("D:\\Program Files\\AutoIt3\\AutoIt3.exe D:\\hello.au3");的意思就是,让AutoIt3.exe 去运行一个.au3的文件。文件里面包含了你写的脚本。比如 lz提到的,把下载窗口关掉的脚本可能就是以下一些。。如果你是其他窗口,就需要自己写其他的.au3的脚本。复制代码
WinWait("File Download");
WinActivate("File Download");
ControlClick("File Download", "","[CLASS:Button; INSTANCE:2]");
WinWait("Save As");
WinActivate("Save As");
ControlSetText("Save As", "", "[CLASS:Edit; INSTANCE:1]", "c:\ddd.bmp")
ControlClick("Save As", "","[CLASS:Button; INSTANCE:2]");
WinWait("Download complete");
WinActivate("Download complete");
ControlClick("Download complete", "","[CLASS:Button; INSTANCE:4]");



引用
引用第22楼xiawared于2009-09-24 17:08发表的 回 20楼(vipid) 的帖子 :
楼主,submitFileName.au3 内容哪里有?



xiawared 2009-09-24 17:27
lz上传文件时候用的submitFileName.au3 。
小弟对autoit的script不懂啊,楼上的赐教,上传文件的script怎么写?
ps:lz上传文件的部分不是很懂啊,能否简单解释下啊


bruce 2009-09-24 17:40
我我好像没发现楼主有贴上传文件弹出框口的代码嘛。
另外,上传文件需要 用到这个吗? 直接用selenium.type 把要上传的文件路径输入到file类型的input 元素里,就可以了。不要去click 那个file类型的input 元素。不需要把文件选择框打开来。


xiawared 2009-09-24 17:43
我试过了,type不行的啊。输不进去的。你有成功过吗


bruce 2009-09-24 17:45
我们一直这样做。Firefox 下面。


xiawared 2009-09-24 17:51
我的是ie下面
<input type="file" id="**" name="**" accept="text/plain" />
怎么都不行


xiawared 2009-09-25 09:57
请教个问题啊:
我用robot在ie浏览器的iput type="file"的框里输入文件路径,但是使用click()方法不能上传文件。
如果不用selenium,手动在框里输入文件路径,点击按钮,是可以上传的。
使用selenium,在click()方法之前,暂停,然后手动点击按钮,也是可以上传的。
我感觉问题在于selenium的click()不是完全实现点击行为,大侠们有没有什么见解啊


vipid 2009-09-25 10:02
你指的click 是click 什么?
我怀疑你的问题是不是不在于输入要上传的文件的path,而是在于没有触发 开始上传这个过程。
开始上传的时候是不是要click 一个button 或者其他之类? 如果是button 一般触发事件都用的是click 而其他元素的话就不一定了。

另外请看 帖子 Selenium.click 不等于 手动单击


引用
引用第29楼xiawared于2009-09-25 09:57发表的 回 27楼(bruce) 的帖子 :
请教个问题啊:
我用robot在ie浏览器的iput type="file"的框里输入文件路径,但是使用click()方法不能上传文件。
如果不用selenium,手动在框里输入文件路径,点击按钮,是可以上传的。
使用selenium,在click()方法之前,暂停,然后手动点击按钮,也是可以上传的。
我感觉问题在于selenium的click()不是完全实现点击行为,大侠们有没有什么见解啊



xiawared 2009-09-25 10:25
我不确定有没有触发。
页面就是一个选择文件的输入框和一个”下一步“的按钮。
因为页面是有错误提示的,而且这个错误提示和直接enter不点击按钮的错误是一样。
如果没有触发,不应该有任何错误提示的。


vipid 2009-09-25 10:29
所以你可以尝试一下其他的动作,mouseDown 之类


xiawared 2009-09-25 10:36
都试过了,还是不行啊。不知道问题出在哪里啊


vipid 2009-09-25 10:39
贴图,贴代码,还有那个错误信息应该可以看一下是什么错误。


xiawared 2009-09-25 10:47
selenium.focus("input_field_name");

selenium.keyPressNative(KeyEvent.VK_F + "");
selenium.keyDownNative("" + KeyEvent.VK_SHIFT);
selenium.keyPressNative("" + KeyEvent.VK_SEMICOLON);
selenium.keyUpNative("" + KeyEvent.VK_SHIFT);
selenium.keyPressNative("" + (KeyEvent.VK_BACK_SLASH));
selenium.keyPressNative(KeyEvent.VK_E + "");
selenium.keyPressNative(KeyEvent.VK_O + "");
selenium.keyPressNative(KeyEvent.VK_S + "");
selenium.keyPressNative(KeyEvent.VK_PERIOD + "");
selenium.keyPressNative(KeyEvent.VK_C + "");
selenium.keyPressNative(KeyEvent.VK_S + "");
selenium.keyPressNative(KeyEvent.VK_R + "");
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
selenium.click("button_name");
错误:





xiawared 2009-09-25 10:49
错误就是:没有选择文件。


xiawared 2009-09-25 10:50
如果是使用Mousedown的话,就是没有反应,貌似没有点击button。没有错误,也没有到下一步


vipid 2009-09-25 11:03
尝试 在 selenium.click("button_name");
之前加入 selenium.fireEvent("input_field_name", "blur")


xiawared 2009-09-25 11:40
我用了autoit的方式,现在可以了:
代码如下:
WinWait("Choose file")
WinActivate("Choose file")
ControlSetText("Choose file", "", "[CLASS:Edit; INSTANCE:1]", "F:\eos.csr")
ControlClick("Choose file", "Look &in", "&Open")


dearlmm 2010-04-20 09:47
有没有perl调用autoit的例子呢?我不是用java


a105064826 2010-04-23 11:24
1请问autoit可以实现对下载下来的文件能否正确运行验证吗?
2Runtime.getRuntime().exec(" C:\\autoit\\install\\AutoIt3.exe C:[url=file:/testfiles/scripts/submitFileNameFF.au3]\\testfiles\\scripts\\submitFileNameFF.au3[/url]\"" + fname + "\"");
这条命令是做什么用的?Runtime.getRuntime().exec(" C:\\autoit\\install\\AutoIt3.exe")如果只写到这里我可以理解为运行C盘这个目录下的autoit3.exe文件


jimmy200246 2010-04-23 11:42
用这个方法可以实现IE上传文件的操作,但是问题是如何可以打开这个上传文件的window窗口?
上传文件的这个field是一个input , type是‘file’,我用selenium.click可以打开,但是无法执行到下一条命令,就一直卡在那边,
直到出现错误Timeout::Error: execution expired
所以貌似click无法实现点击这种field的功能,我尝试过用mouse down,调用js方法等,还是没有办法顺利打开上传文件的窗口,诚心请教各位selenium支持者能够给我解决这个问题的建议和方法,谢谢了!

也可以发邮件给我,jimmy200246@gmail.com

真心期待你们的回复!


看雪时节 2010-05-21 18:30
引用
引用第42楼jimmy200246于2010-04-23 11:42发表的 回 39楼(xiawared) 的帖子 :
用这个方法可以实现IE上传文件的操作,但是问题是如何可以打开这个上传文件的window窗口?
上传文件的这个field是一个input , type是‘file’,我用selenium.click可以打开,但是无法执行到下一条命令,就一直卡在那边,
直到出现错误Timeout::Error: execution expired
所以貌似click无法实现点击这种field的功能,我尝试过用mouse down,调用js方法等,还是没有办法顺利打开上传文件的窗口,诚心请教各位selenium支持者能够给我解决这个问题的建议和方法,谢谢了!

.......


初步看你这个问题,你应该先加载AutoIT的代码,让它运行起来在那等着弹出的上传或下载对话框,这样就可以解决!~



zhangfei 2010-05-24 10:42
Runtime.getRuntime().exec(new String[]{"C:\\Program Files\\AutoIt3\\AutoIt3.exe C:\\Program Files\\AutoIt3\\Examples\\testClose.au3"});

运行时报错: CreateProcess: "C:\Program Files\AutoIt3\AutoIt3.exe C:\Program Files\AutoIt3\Examples\testClose.au3" error=123

error=123代表什么错误?


zhangfei 2010-05-24 11:01
单独运行autoit脚本时,是可以运行的,Runtime.getRuntime().exec()执行就报错,



vipid 2010-05-24 11:09
把.au3默认打开方式设置为执行。
然后直接Runtime.getRuntime().exec("C:\\Program Files\\AutoIt3\\Examples\\testClose.au3");


zhangfei 2010-05-24 11:23
难道是不能装在C:\Program Files\下面?如果装在这下面,运行的时候报错,error=123,
重新安装在D盘,运行 能成功通过


ahtest 2010-07-14 16:29
貌似会游泳!支持一下


oliviazhu 2010-08-17 22:44
It works, Thanks!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值