セレンRC>アップロード方法についてはファイルを使用attachFile()

StackOverflow https://stackoverflow.com/questions/1707884

  •  19-09-2019
  •  | 
  •  

質問

を使用していセレン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を指定します。お願いいえば可能です。

役に立ちましたか?

解決 2

このための解決策を手に入れました。Selenium.FocusメソッドとSelenium.KeypressNative/KeyReReasenativeメソッドを使用します。

以下を使用してテキストボックスに焦点を当てる必要があります。

selenium.focus( "テキストボックスロケーター");

次に、入力ファイルがc: tools file.txtの場合、次のような文字を入力する必要があります。

selenium.keydownnative( "16"); //オンにシフトします

selenium.keypressnative( "67"); // Cシフトはそれを行いますc

selenium.keypressnative( "59"); //; Shiftはそれを作成します:(あなたは直接コロンを行うことはできません)

selenium.keyupnative( "16"); //オフにシフトします

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"); //オンにシフトします

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

selenium.keypressnative( "88"); // バツ

selenium.keypressnative( "84"); // t

selenium.keypressnative( "10"); // 入る

selenium.keyreleasenative( "10"); // 入る

「Enter」文字でSequiQNCEを終了しました。これが機能しない場合があるため、ボタンをクリックする必要がある場合があります(要素ロケーターがわかっている場合)。

他のヒント

ことを義務付けられているが、最近問題点を解決してこの

    //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.

のための家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)

では本質的にグラブのウィンドウの最大化で入力をすると、アップロードファイルをクリックし選択ボタンをテーションに戻ったときにセレンまれIE8ものと思いますかなぜ他のIEでサポートされている自動車で_IE図書館できない。

私は見ている多くのロボットスクリプトおよびfirefox hacksす際には、javascriptを有効にしない余分ない。両方の手続きを行うことが求められない変更はブラウザです。

会員の方はログインしてくだ不足のコメントこのコードでの展開が始まります。

「Filelocator」はURLではなく、SeleniumクラスのJavadocの上部で指定されているロケーターです。これは、ファイルの選択に使用される入力のロケーターです。

「FieldLocator」は、引用しているドキュメントで指定されているように、フォームの入力フィールドに設定するファイルを指すURLです。

firefox in chromeモード(browserid = *firefoxの代わりにクロム)では、これは期待どおりに機能します。このbrowseridでのみ動作していることが文書化されています)

たとえば、adcitionfile( "uploadfield"、thread.currentthread()。getContextClassLoader()。getResource( "files/sample.pdf")。toString());

これに対する私の解決策は、RCエミュレーションモードでSelenium-2を使用することです。これにより、レガシーセレン-1テストを維持できますが、ファイルアップロードなどのタスクを実行するために必要な場合はSelenium-2 APIに切り替えます。

Selenium-2は現在ベータ版ですが、比較的安定しているようです。しかし、Selenium-1ができることはすべて、Selenium-2 RCエミュレーションモードによってサポートされているわけではないので、そのようにする前によく考えてください。

これについてはこちら:http://seleniumhq.org/docs/09_webdriver.html#emulating-selenium-rc

Selenium / RSPEC / Internet Explorerの使用私のソリューションは、Windowsマシンに自動車スクリプトを作成することでした

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

次に、これをWindowsマシンで管理者として実行します。 exeファイルを右クリックして、管理者として実行します。

次に、rspecがページを実行します。「[参照]ボタンのID」をクリックします。 Windowsマシンで[参照]ウィンドウが開くと、Autoitはテキストボックスを自動させ、閉じます。これが誰かを助けてくれることを願っています。

このスクリプトを自動車で試すことができます。基本的に、それはファイル選択ウィンドウを待っています。次に、ファイルパスに入り、Enterをクイックで送信します。最後に、ポップアップエラーメッセージがあるかどうかを確認します。テキストを読み取り、Exitcodeを1に設定する場合、非SET Exitコードが0になります。

スクリプトは、aut2exeによって実行可能ファイル(.exe)に変換できます - コンソールをマークすることが重要ですか?チェックボックス、その後、exeはjavaから実行できます (Runtime.getRuntime().exec()).

また、個別のスレッドで[ファイルアップロード]ボタンをクリックすることが重要なことも1つあります。

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

$ sel-> typeと$ sel-> focusを使用してはるかに簡単です。以下は良い記事です。

http://bitsilearn.blogspot.com/

ブラウザとして *firefoxを使用するためにSeleniumセットを使用してファイルを成功しただけです。彼らはまだドキュメントを更新していないと思います。

私は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帰属
所属していません StackOverflow
scroll top