Javascript 硒RC>;如何使用attachFile()上载文件
我将Selenium RC与Junit框架一起使用。我正在尝试使用attachFile()方法上载文件Javascript 硒RC>;如何使用attachFile()上载文件,javascript,selenium-rc,Javascript,Selenium Rc,我将Selenium RC与Junit框架一起使用。我正在尝试使用attachFile()方法上载文件 谁能告诉我如何定义“文件定位器”。我没有在这里指定哪个URL。如果可能的话,请给我一个例子。我得到了解决方案,使用selenium.focus方法和selenium.keyPressNative/keyreleaseenative方法 您将需要使用以下方法使文本框具有焦点: selenium.focus(“文本框定位器”) 然后,如果输入文件是C:\tools\file.txt,则需要按如下方
谁能告诉我如何定义“文件定位器”。我没有在这里指定哪个URL。如果可能的话,请给我一个例子。我得到了解决方案,使用selenium.focus方法和selenium.keyPressNative/keyreleaseenative方法 您将需要使用以下方法使文本框具有焦点: selenium.focus(“文本框定位器”) 然后,如果输入文件是C:\tools\file.txt,则需要按如下方式键入字母: selenium.keyDownNative(“16”)//转向 selenium.keyPressNative(“67”);//c移位使其变为c selenium.keyPressNative(“59”);/;Shift成功了:(你不能直接做冒号) 硒。keyupunative(“16”);//移开 selenium.keyPressNative(“47”);//刀砍 selenium.keyPressNative(“84”);//t selenium.keyPressNative(“79”);//o selenium.keyPressNative(“79”);//o selenium.keyPressNative(“76”);//l selenium.keyPressNative(“83”);// selenium.keyPressNative(“47”);//刀砍 selenium.keyDownNative(“16”)//转向 selenium.keyPressNative(“70”);//f移位使其成为f 硒。keyupunative(“16”);//移开 selenium.keyPressNative(“73”);//我 selenium.keyPressNative(“76”);//l selenium.keyPressNative(“69”);//e selenium.keyPressNative(“46”);/ selenium.keyPressNative(“84”);//t selenium.keyPressNative(“88”);//x selenium.keyPressNative(“84”);//t selenium.keyPressNative(“10”);//进入 selenium.keyReleaseNative(“10”);//进入 我用一个“回车”字符结束了这个序列。有时这不起作用,因此您可能需要单击该按钮(如果您知道该按钮的元素定位器)。“fileLocator”不是url,而是Selenium类的javadoc顶部指定的定位器。 它是用于选择文件的输入的定位器 “fieldLocator”是一个url,指向您要在表单的输入字段中设置的文件,如您引用的文档中所指定 当Firefox处于chrome模式(browserId=*chrome而不是*Firefox)时,这一切都如期进行。有文件证明仅使用此浏览器ID)
例如:attachFile(“uploadField”,Thread.currentThread().getContextClassLoader().getResource(“files/sample.pdf”).toString()) 使用$sel->type和$sel->focus更容易。下面是一篇好文章
这是一个老问题,但我最近解决了这个问题
//Start an auto it script that selects the file manually
if(browser.contains("iexplore")){
Runtime r= Runtime.getRuntime();
Process p = null;
try {
p = r.exec("C:\\uploadFile.exe \"Files\" \"ctl00$ContentPlaceHolder1$FilesView$ctl02$NewFile\" \"C:\\GhostTagBug2.ttx\"");
}catch(Exception e){}
p.waitFor();
} else {
//Tested on firefox
//Get focus and type the path manually
selenium.focus("xpath=//input[contains(@id,\"_NewFile\")]");
selenium.type("xpath=//input[contains(@id,\"_NewFile\")]", "C:\\GhostTagBug2.ttx");
}
browser只是一个变量,包含Selenium脚本正在运行的浏览器,代码显然是java
对于IE,uploadFile.exe是一个自动it脚本,如下所示
#include IE.au3
AutoItSetOption("WinTitleMatchMode","2") ; set the select mode to select using substring
;Normally run from command line
if($cmdLine[0] > 2) then
$titlex = $cmdLine[1] ;Title of the window
$form = $cmdLine[2] ;Name of the file upload/save form object
$file = $cmdLine[3] ;Path of the file to upload
Else
;Testing fields
$titlex = "Files"
$form = "ctl00$ContentPlaceHolder1$FilesView$ctl02$NewFile"
$file = "C:\\GhostTagBug2.ttx"
EndIf
WinWait($titlex) ; match the window with substring
$title = WinGetTitle($titlex) ; retrives whole window title
WinSetState($title, "", @SW_MAXIMIZE) ;Maximize the window incase button is hidden
WinActivate($title)
WinWaitActive($title)
$oIE = _IEAttach ("Files")
$oT = _IEGetObjByName ($oIE, $form)
;Move the mouse to the button on the form and click it
MouseMove (_IEPropertyGet ($oT, "screenx") + _IEPropertyGet ($oT, "width") - 10, _IEPropertyGet ($oT, "screeny") + _IEPropertyGet ($oT, "height") / 2)
MouseClick ("left")
;Wait for upload screen then input the file and close it
WinWait ("Choose File to Upload")
$hChoose = WinGetHandle ("Choose File to Upload")
ControlSetText ($hChoose, "", "Edit1", $file)
ControlClick ($hChoose, "", "Button2")
;Restore window state
WinSetState($title, "", @SW_RESTORE)
它基本上抓住了窗口的标题,将其最大化,输入要上传的文件,单击选择按钮并返回Selenium,我已经在IE8中对其进行了测试,但我不明白为什么任何受auto-It's(IE库)支持的IE都无法处理此问题
我见过很多机器人脚本和firefox黑客,他们让javascript做额外的事情。这两种方法都不需要修改浏览器
很抱歉没有评论,此代码仍在编写中。我的解决方案是在RC仿真模式下使用Selenium-2。这允许您保留传统的Selenium-1测试,但在需要执行文件上传等任务时,可以切换到Selenium-2API 硒-2目前处于Beta状态,但似乎相对稳定。但是,并不是Selenium-1所能做的一切都受到Selenium-2 RC仿真模式的支持,所以在这样做之前要三思 更多信息请点击此处:
使用Selenium/Rspec/Internet Explorer 我的解决方案是在我的windows计算机上创建一个AutoIt脚本
WinWaitActive("Choose File to Upload")
Send("c:\tests\school.jpg")
Send("{ENTER}")
run("selectfile2.exe")
然后在windows计算机上以管理员身份运行此程序。右键单击exe文件并以管理员身份运行
然后rspec将创建一个页面。单击“浏览按钮的id”。当浏览窗口在windows计算机上打开时,自动填充文本框并关闭。希望这能帮助别人,因为这让我发疯。你可以在AutoIt中尝试这个脚本。基本上,它等待文件选择器窗口。然后输入文件路径并发送快捷键。最后检查是否有任何弹出错误消息,如果有,则读取文本并将exitcode设置为1,如果没有,则将exit code设置为0。 该脚本还确保关闭“文件选择器”窗口 该脚本可以通过Aut2Exe转换为可执行(.exe)-标记console很重要吗?复选框,然后可以从java
(Runtime.getRuntime().exec())执行exe。
还有一件事很重要,就是在单独的线程中运行“单击文件上载”按钮
new Thread() {
public voi run() {
browser.click([LOCALTOR]).
}
}.start();
否则,selenium将挂起等待click命令完成,因为文件选择器窗口已打开而未关闭,所以这种情况永远不会发生
剧本:
$title="Choose File to Upload"
If($cmdLine[0] == 1 OR $cmdLine[0] == 2) Then
$file=$cmdLine[1]
If ($cmdLine[0] == 2) Then
$title=$cmdLine[0]
EndIf
Else
ConsoleWriteError("Wrong number of argument. Use 2 argument: [FILE PATH] [FILE UPLOAD WINDOW TITLE]. Second argument is optional")
Exit(-1)
EndIf
If WinWaitActive($title,"",5)==0 Then ; wait 5 sec.
ConsoleWriteError($title & " window wasn't opened")
Exit (2)
EndIf
Send($file)
Send("{ENTER}")
$status=WinWaitActive($title, "", 1)
$success = ($status = 0)
If Not $success Then
$text = ControlGetText($title,"","[CLASS:Static; INSTANCE:2]")
WinClose($title)
WinClose($title)
ConsoleWriteError($text)
EndIf
Exit Not $success
我刚刚成功地上传了文件,使用Selenium设置为使用*firefox作为浏览器。我想他们还没有更新文档 我使用的是Ruby客户端,所以它是这样工作的
$browser.click "css=input.file" # This is the 'Choose File' button
$browser.type "css=input.file", "/absolute/path/to/file.file"
有人回答这个问题吗???请帮助我…实际上我的应用程序不支持firefox…所以这个attachFile方法对我不起作用…而keyPressNative/keyReleaseNative方法与IE7一起工作…我认为是另一种方式。\n称为反斜杠,而不是斜杠。而且你的方法不适用于所有键盘、各种文本和所有操作系统。@PierreGardin a)它在法语中只称为反斜杠,在英语中称为反斜杠b)这些都是Java键盘代码,所以不管你插了什么键盘,它们都能工作。c) 您是对的,这不适用于所有操作系统,但也不适用于以字符串形式输入路径——无论哪种方式都必须进行假设。这是一个很好的解决办法
$browser.click "css=input.file" # This is the 'Choose File' button
$browser.type "css=input.file", "/absolute/path/to/file.file"