Search code examples
variablescross-browserruntimeautoit

Choose title- and file name at runtime


I am trying to automate file upload in two browsers but the window name is "File Upload" in Firefox and "Open" in Chrome. I don't want to write two different scripts.

How to choose title- and file name at runtime to achieve cross browser compatibility? I use Selenium and testNG, AutoIt only for file upload.


Solution

  • Set a variable based off the web browser being used, then use that variable. The code below should get you on the right track.

    $FirefoxUpload = "File upload"
    $ChromeUpload = "Open"
    
    if WinExists($FirefoxUpload)
      $UploadWindow = $FirefoxUpload
    elseif WinExists($ChromeUpload)
      $UploadWindow = $ChromeUpload
    else
      $UploadWindow = ""
    endif
    
    if $UploadWindow <> ""
      ControlFocus($UploadWindow,"","Edit1")
      ControlsetText($UploadWindow,"","Edit1","C://file.xls")
      ControlClick($UploadWindow,"","Button1")
    endif