gpt4 book ai didi

javascript - Selenium RC > 如何使用 attachFile() 上传文件

转载 作者:数据小太阳 更新时间:2023-10-29 04:12:08 29 4
gpt4 key购买 nike

我正在使用带有 Junit 框架的 Selenium RC。我正在尝试使用 attachFile() 方法上传文件。

attachFile: (Information collected from selenium API http://release.seleniumhq.org/selenium-remote-control/1.0-beta-2/doc/java/com/thoughtworks/selenium/Selenium.html#attachFile(java.lang.String,%20java.lang.String))

void attachFile(java.lang.String fieldLocator,
java.lang.String fileLocator)

Sets a file input (upload) field to the file listed in fileLocator

Parameters:
fieldLocator - an element locator
fileLocator - a URL pointing to the specified file. Before the file can be set
in the input field (fieldLocator), Selenium RC may need to transfer the file to
the local machine before attaching the file in a web page form. This is common in
selenium grid configurations where the RC server driving the browser is not the
same machine that started the test. Supported Browsers: Firefox ("*chrome") only.

谁能告诉我如何定义“fileLocator”。我没有得到要在此处指定的 URL。如果可能,请给我一个例子。

最佳答案

这是一个老问题,但我最近解决了这个问题

    //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 是一个自动脚本,如下所示。


#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,我已经在 IE 8 中对其进行了测试,但我不明白为什么自动支持的 IE 它的 _IE 库无法处理此问题。

我见过很多机器人脚本和 firefox hack,您可以在其中启用 javascript 来做额外的事情。这两者都不需要修改浏览器。

对于缺少评论,我深表歉意,此代码仍在进行中。

关于javascript - Selenium RC > 如何使用 attachFile() 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1707884/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com