2009-11-10 31 views
8

我使用Selenium RC和Junit框架。我正嘗試使用attachFile()方法上傳文件。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在這裏指定。請給我一個例子,如果可能的話。

+0

任何人那裏回答這個查詢??? plz幫助我... – Saara 2009-11-25 14:17:00

回答

1

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

您需要使用給焦點到文本框中:

selenium.focus(「文本框中定位器」);

這時如果輸入文件是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」); // l

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」); // i

selenium.keyPressNative(「76」); // l

selenium.keyPressNative(「69」); // e

selenium.keyPressNative(「46」); //。

selenium.keyPressNative(「84」); // t

selenium.keyPressNative(「88」); // x

selenium.keyPressNative(「84」); // t

硒。keyPressNative( 「10」); //輸入

selenium.keyReleaseNative(「10」); //輸入

我已經用「Enter」字符結束了sequqnce。有時這不起作用,所以你可能需要點擊按鈕(如果你知道它的元素定位器)。

+0

\被稱爲反斜槓,而不是斜線。而且您的方法不適用於所有鍵盤,各種文本和所有操作系統。 – 2010-08-06 11:07:12

+0

@PierreGardin a)它只在法語中稱爲反斜槓,在英語中稱爲反斜槓b)這些是Java KeyCode,因此無論您插入什麼鍵盤,它們都能正常工作。c)您說得對,與所有操作系統一起工作,但都不是以字符串的形式輸入路徑 - 必須假設任何一種方式。這是一個很好的解決方法 – 2016-06-07 12:59:40

1

「fileLocator」不是一個url,而是一個在Selenium類的javadoc頂部指定的定位器。 它是用於選擇文件的輸入的定位器。

「fieldLocator」是指向您要在表單的輸入字段中設置的文件的url,如您所引用的doc中所指定的。

在Chrome模式下(browserId = * chrome而不是* firefox),按預期工作。它被記錄爲只能使用這個browserId)

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

+0

其實我的應用程序不支持Firefox ...所以這attachFile方法沒有爲我工作...和keyPressNative/keyReleaseNative方法與IE7 ... – Saara 2009-12-01 12:43:57

+0

我認爲這是相反的方式。 – hoymkot 2012-02-23 00:24:40

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

瀏覽器的問題,僅僅是含有硒腳本運行的瀏覽器和代碼顯然是Java中的一個變量。

對於IE,uploadFile.exe是一個auto 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) 

它本質上抓住窗口的標題,最大化它,輸入要上傳的文件,點擊選擇按鈕,然後回到硒,我一直在測試的IE 8也不錯,但我不看看爲什麼任何支持auto的IE瀏覽器都無法處理這個問題。

我見過很多機器人腳本和Firefox的黑客,你啓用JavaScript做額外的事情。這兩個都不需要修改瀏覽器。

我對缺少評論表示歉意,此代碼仍在進行中。

+0

感謝您回來並添加您的解決方案! – 2010-12-22 23:03:46

+0

+1 - 也適用於鍍鉻 – 2012-02-14 16:33:34

1

我的解決方案是在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

1

使用硒/ 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自動填充文本框並關閉。希望這可以幫助某人,因爲這使我瘋狂。

1

您可以在AutoIt中試用此腳本。基本上,它等待文件選擇器窗口。然後進入文件路徑並快速發送輸入。最後檢查是否有任何彈出錯誤消息,如果有讀取它的文本並將exitcode設置爲1,如果未設置退出代碼爲0. 該腳本還可以確保文件選擇器窗口關閉。

該腳本可以通過Aut2Exe轉換爲可執行文件(.exe) - 標記控制檯很重要嗎?複選框,之後,exe文件可以並從java執行(Runtime.getRuntime().exec()).

還有一件重要的事情在單獨的線程中運行點擊文件上傳按鈕。

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

否則硒將掛起等待點擊命令完成,這從來不會發生,因爲文件選擇器窗口已打開並且未關閉。

腳本:

$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 
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" 
相關問題