您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
利用Selenium自動化web測試
作者:網(wǎng)絡轉載 發(fā)布時間:[ 2016/6/15 16:19:26 ] 推薦標簽:Selenium 功能測試 自動化測試

  對于上載/下載窗口,好是處理而不是跳過它們。為了避免 Selenium 的局限性,一種建議是使用 Java 機器人 AutoIt 來處理文件上載和下載問題。AutoIt 被設計來自動化 Window GUI 操作。它可以認識大多數(shù) Window GUI,提供很多 API,并且很容易轉換為 .exe 文件,這樣的文件可以直接運行或者在 Java 代碼中調(diào)用。清單 14 演示了處理文件上載的腳本。這些腳本的步驟是:
  根據(jù)瀏覽器類型確定上載窗口標題。
  激活上載窗口。
  將文件路徑放入編輯框中。
  提交。
  清單 14. 處理上載的 AutoIt 腳本
  ;first make sure the number of arguments passed into the scripts is more than 1
  If $CmdLine[0]<2 Then Exit EndIf
  handleUpload($CmdLine[1],$CmdLine[2])
  ;define a function to handle upload
  Func handleupload($browser, $uploadfile)
  Dim $title                          ;declare a variable
  ;specify the upload window title according to the browser
  If $browser="IE" Then                  ; stands for IE;
  $title="Select file"
  Else                                 ; stands for Firefox
  $title="File upload"
  EndIf
  if WinWait($title,"",4) Then ;wait for window with
  title attribute for 4 seconds;
  WinActivate($title)                  ;active the window;
  ControlSetText($title,"","Edit1",$uploadfile)   ;put the
  file path into the textfield 
  ControlClick($title,"","Button2")                ;click the OK
  or Save button
  Else
  Return False
  EndIf
  EndFunc
  在 Java 代碼中,定義一個函數(shù)來執(zhí)行 AutoIt 編寫的 .exe 文件,并在單擊 browse 之后調(diào)用該函數(shù)。
  清單 15. 執(zhí)行 AutoIt 編寫的 .exe 文件
  public void handleUpload(String browser, String filepath) {
  String execute_file = "D:\scripts\upload.exe";
  String cmd = """ + execute_file + """ + " " + """ + browser + """
  + " " + """ + filepath + """; //with arguments
  try {
  Process p = Runtime.getRuntime().exec(cmd);
  p.waitFor(); //wait for the upload.exe to complete
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
  清單 16 是處理 Internet Explorer 中下載窗口的 AutoIt 腳本。Internet Explorer 和 Firefox 中的下載腳本各不相同。
  清單 16. 處理 Internet Explorer 中下載的 AutoIt 腳本
  If $CmdLine[0]<1 Then Exit EndIf
  handleDownload($CmdLine[1])
  Func handleDownload($SaveAsFileName)
  Dim $download_title="File Download"
  If WinWait($download_title,"",4) Then
  WinActivate($download_title)
  Sleep (1000)
  ControlClick($download_title,"","Button2","")
  Dim $save_title="Save As"
  WinWaitActive($save_title,"",4)
  ControlSetText($save_title,"","Edit1", $saveAsFileName)
  Sleep(1000)
  if FileExists ($SaveAsFileName) Then
  FileDelete($SaveAsFileName)
  EndIf
  ControlClick($save_title, "","Button2","")
  Return TestFileExists($SaveAsFileName)
  Else
  Return False
  EndIf
  EndFunc
  AutoIt 腳本很容易編寫,但是依賴于瀏覽器類型和版本,因為不同的瀏覽器和版本中,窗口標題和窗口控件類是不相同的。
  如何驗證警告/確認/提示信息
  對于由 window.alert() 生成的警告對話框,使用 selenium.getAlert() 來檢索前一操作期間生成的 JavaScript 警告的消息。如果沒有警告,該函數(shù)將會失敗。得到一個警告與手動單擊 OK 的結果相同。
  對于由 window.confirmation() 生成的確認對話框,使用 selenium.getConfirmation() 來檢索前一操作期間生成的 JavaScript 確認對話框的消息。默認情況下,該函數(shù)會返回 true,與手動單擊 OK 的結果相同。這可以通過優(yōu)先執(zhí)行 chooseCancelOnNextConfirmation 命令來改變。
  對于由 window.prompt() 生成的提示對話框,使用 selenium.getPromt() 來檢索前一操作期間生成的 JavaScript 問題提示對話框的消息。提示的成功處理需要優(yōu)先執(zhí)行 answerOnNextPrompt 命令。
  JavaScript 警告在 Selenium 中不會彈出為可見的對話框。處理這些彈出對話框失敗會導致異常,指出沒有未預料到的警告。這會讓測試用例失敗。

上一頁12345下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd