Selenium RC > how to upload file using attachFile()

后端 未结 8 779
谎友^
谎友^ 2021-02-04 14:02

I am using Selenium RC with Junit framework. I am trying to upload a file using attachFile() method.

attachFile: (Information collected from selenium API http:/         


        
8条回答
  •  醉梦人生
    2021-02-04 14:32

    This is an old question but I recently solved the problem doing this

        //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 is just a variable containing what browser the Selenium script is running and the code is obviously in java.

    For IE, uploadFile.exe is an auto it script that looks like this.

    
    #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)
    
    

    It essentially grabs the title of the window, maximizes it, inputs the file to be uploaded, clicks the select button and goes back to Selenium, I've tested it in IE 8 fine, but I don't see why any IE that is supported by auto it's _IE library wouldn't be able to handle this.

    I've seen a lot of robot scripts and firefox hacks where you enable javascript to do extra things. Both of these require no modification of the browser.

    I apologize for lack of comments, this code is still in progress.

提交回复
热议问题