9

我正在使用 Selenium RC 和 Junit 框架。我正在尝试使用 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。如果可能的话,请给我一个例子。

4

8 回答 8

4

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

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

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

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

于 2010-06-18T13:18:24.177 回答
1

我得到了解决方案,使用 selenium.focus 方法和 selenium.keyPressNative/keyReleaseNative 方法。

您需要使用以下方法将焦点放在文本框上:

selenium.focus("文本框定位器");

然后,如果您的输入文件是 C:\tools\File.txt 您需要输入如下字母:

selenium.keyDownNative("16"); //移开

selenium.keyPressNative("67"); // c shift 变为 C

selenium.keyPressNative("59"); // ; Shift 使它:(你不能直接做冒号)

selenium.keyUpNative("16"); // 关闭

selenium.keyPressNative("47"); // 斜线

selenium.keyPressNative("84"); // 吨

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

selenium.keyUpNative("16"); // 关闭

selenium.keyPressNative("73"); // 一世

selenium.keyPressNative("76"); // l

selenium.keyPressNative("69"); // e

selenium.keyPressNative("46"); // .

selenium.keyPressNative("84"); // 吨

selenium.keyPressNative("88"); // X

selenium.keyPressNative("84"); // 吨

selenium.keyPressNative("10"); // 进入

selenium.keyReleaseNative("10"); // 进入

我已经用“Enter”字符结束了序列。有时这不起作用,因此您可能需要单击按钮(如果您知道它的元素定位器)。

于 2009-11-30T09:48:32.987 回答
1

“fileLocator”不是 url,而是 Selenium 类的 javadoc 顶部指定的定位器。它是用于选择文件的输入的定位器。

“fieldLocator”是一个 url,指向您要在表单的输入字段中设置的文件,如您引用的文档中指定的那样。

在 chrome 模式下使用 Firefox(browserId=*chrome 而不是 *firefox),这可以按预期工作。它被记录为仅使用此 browserId)

例如: attachFile("uploadField", Thread.currentThread().getContextClassLoader().getResource("files/sample.pdf").toString());

于 2009-11-30T15:11:27.617 回答
1

我对此的解决方案是在 RC 仿真模式下使用 Selenium-2。这允许您保留旧的 Selenium-1 测试,但在需要执行文件上传等任务时切换到 Selenium-2 api。

Selenium-2 目前处于 Beta 阶段,但似乎相对稳定。但是 Selenium-2 RC 仿真模式并不支持 Selenium-1 可以做的所有事情,所以在你这样做之前要三思而后行。

更多信息在这里: http ://seleniumhq.org/docs/09_webdriver.html#emulating-selenium-rc

于 2011-01-25T20:22:06.647 回答
1

使用 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 会自动填充文本框并关闭它。希望这对某人有所帮助,因为这让我发疯了。

于 2011-05-18T18:46:46.550 回答
1

你可以在 AutoIt 中尝试这个脚本。基本上,它等待文件选择器窗口。然后输入文件路径并快速发送回车。最后检查是否有任何弹出错误消息,如果有任何读取文本并将退出代码设置为 1,如果未设置退出代码为 0。脚本还确保文件选择器窗口关闭。

脚本可以通过 Aut2Exe 转换为可执行文件 (.exe) - 标记控制台很重要吗?复选框,之后exe可以从java执行(Runtime.getRuntime().exec()).

在单独的线程中单击文件上传按钮运行还有一件事很重要。

new Thread() {
  public voi run() {
    browser.click([LOCALTOR]).
 }
}.start();

否则 selenium 将挂起等待单击命令完成,这永远不会发生,因为文件选择器窗口已打开而不是关闭。

剧本:

$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
于 2011-10-28T13:58:21.857 回答
0

使用 $sel->type 和 $sel->focus 更容易。下面是一篇不错的文章。

http://bitsilearn.blogspot.com/

于 2010-03-21T09:12:04.667 回答
0

我刚刚成功上传了使用 Selenium 设置为使用 *firefox 作为浏览器的文件。我猜他们还没有更新文档。

我正在使用 Ruby 客户端,所以它是这样让它工作的

$browser.click "css=input.file" # This is the 'Choose File' button
$browser.type "css=input.file", "/absolute/path/to/file.file"
于 2012-02-17T20:06:44.213 回答