繁体   English   中英

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

[英]Selenium RC > how to upload file using attachFile()

我正在使用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。 请尽可能给我一个例子。

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

    //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");
    }

浏览器只是一个变量,包含运行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是由auto支持_IE库无法处理这个问题。

我已经看过很多机器人脚本和firefox hacks,你可以让javascript做额外的事情。 这两个都不需要修改浏览器。

我为缺乏评论而道歉,此代码仍在进行中。

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

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

selenium.focus(“text box locator”);

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

selenium.keyDownNative( “16”); // SHIFT ON

selenium.keyPressNative( “67”); // c shift使它成为C.

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

selenium.keyUpNative( “16”); // SHIFT OFF

selenium.keyPressNative( “47”); //斜线

selenium.keyPressNative( “84”); // t

selenium.keyPressNative( “79”); // o

selenium.keyPressNative( “79”); // o

selenium.keyPressNative( “76”); //升

selenium.keyPressNative( “83”); // s

selenium.keyPressNative( “47”); //斜线

selenium.keyDownNative( “16”); // SHIFT ON

selenium.keyPressNative( “70”); // f shift使它成为F.

selenium.keyUpNative( “16”); // SHIFT OFF

selenium.keyPressNative( “73”); // 一世

selenium.keyPressNative( “76”); //升

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”); //输入

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

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

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

Firefox处于chrome模式(browserId = * chrome而不是* firefox),这可以正常工作。 它被记录为仅适用于此browserId)

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

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

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

更多相关内容: http//seleniumhq.org/docs/09_webdriver.html#emulating-selenium-rc

使用Selenium / Rspec / Internet Explorer我的解决方案是在我的Windows机器上创建一个AutoIt脚本

WinWaitActive("Choose File to Upload")
Send("c:\tests\school.jpg")
Send("{ENTER}")
run("selectfile2.exe")

然后在Windows机器上以管理员身份运行它。 右键单击exe文件并以admin身份运行。

然后rspec执行page.click“浏览按钮的ID”。 当浏览窗口在Windows机器上打开时,AutoIt会自动填充文本框并关闭。 希望这有助于某人,因为这让我疯了。

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

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

在单独的线程中运行单击文件上载按钮也有一件重要的事情。

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

否则selenium将挂起等待click命令完成,这永远不会发生,因为File Chooser窗口已打开但未关闭。

剧本:

$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

使用$ sel-> type和$ sel-> focus更容易。 以下是一篇好文章。

http://bitsilearn.blogspot.com/

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

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

$browser.click "css=input.file" # This is the 'Choose File' button
$browser.type "css=input.file", "/absolute/path/to/file.file"

暂无
暂无

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

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