Selenium RC > how to upload file using attachFile()

后端 未结 8 778
谎友^
谎友^ 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:31

    You may try this script in AutoIt. Basically, it awaits for file chooser window. Then enters file path and send enter quick. At the end checks if there was any popup error message, if any reads it text and set exitcode to 1, if non set exit code to 0. The script also ensures that file chooser window is closed.

    The script can be converted to executable (.exe) by Aut2Exe - it's important to mark console? checkbox, After that exe can and executed from java (Runtime.getRuntime().exec()).

    There is also one thing important to run click on file upload button in separate thread.

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

    Otherwise selenium will hang awaiting for click command finish, which never happens because File Chooser windows was opened and not closed.

    The script:

    $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
    

提交回复
热议问题