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

  對(duì)于上載/下載窗口,好是處理而不是跳過(guò)它們。為了避免 Selenium 的局限性,一種建議是使用 Java 機(jī)器人 AutoIt 來(lái)處理文件上載和下載問(wèn)題。AutoIt 被設(shè)計(jì)來(lái)自動(dòng)化 Window GUI 操作。它可以認(rèn)識(shí)大多數(shù) Window GUI,提供很多 API,并且很容易轉(zhuǎn)換為 .exe 文件,這樣的文件可以直接運(yùn)行或者在 Java 代碼中調(diào)用。清單 14 演示了處理文件上載的腳本。這些腳本的步驟是:
  根據(jù)瀏覽器類型確定上載窗口標(biāo)題。
  激活上載窗口。
  將文件路徑放入編輯框中。
  提交。
  清單 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 代碼中,定義一個(gè)函數(shù)來(lái)執(zhí)行 AutoIt 編寫(xiě)的 .exe 文件,并在單擊 browse 之后調(diào)用該函數(shù)。
  清單 15. 執(zhí)行 AutoIt 編寫(xiě)的 .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 腳本很容易編寫(xiě),但是依賴于瀏覽器類型和版本,因?yàn)椴煌臑g覽器和版本中,窗口標(biāo)題和窗口控件類是不相同的。
  如何驗(yàn)證警告/確認(rèn)/提示信息
  對(duì)于由 window.alert() 生成的警告對(duì)話框,使用 selenium.getAlert() 來(lái)檢索前一操作期間生成的 JavaScript 警告的消息。如果沒(méi)有警告,該函數(shù)將會(huì)失敗。得到一個(gè)警告與手動(dòng)單擊 OK 的結(jié)果相同。
  對(duì)于由 window.confirmation() 生成的確認(rèn)對(duì)話框,使用 selenium.getConfirmation() 來(lái)檢索前一操作期間生成的 JavaScript 確認(rèn)對(duì)話框的消息。默認(rèn)情況下,該函數(shù)會(huì)返回 true,與手動(dòng)單擊 OK 的結(jié)果相同。這可以通過(guò)優(yōu)先執(zhí)行 chooseCancelOnNextConfirmation 命令來(lái)改變。
  對(duì)于由 window.prompt() 生成的提示對(duì)話框,使用 selenium.getPromt() 來(lái)檢索前一操作期間生成的 JavaScript 問(wèn)題提示對(duì)話框的消息。提示的成功處理需要優(yōu)先執(zhí)行 answerOnNextPrompt 命令。
  JavaScript 警告在 Selenium 中不會(huì)彈出為可見(jiàn)的對(duì)話框。處理這些彈出對(duì)話框失敗會(huì)導(dǎo)致異常,指出沒(méi)有未預(yù)料到的警告。這會(huì)讓測(cè)試用例失敗。

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