1. 程式人生 > >【Web自動化測試——代碼篇八】常用方法——上傳/下載文件

【Web自動化測試——代碼篇八】常用方法——上傳/下載文件

.com instance timeunit 鍵盤 .sh exception lse NPU 組合

上傳文件

對於Web頁面的上傳功能實現一般有一下倆種方式:

  • 普通上傳:將本地文件的路徑作為一個值放在input標簽中,通過form表單將這個值提交給服務器(不做介紹send_keys方法)。
  • AutoIt上傳:利用模擬鍵盤按鍵,鼠標移動和窗口/控件的組合來實現自動化任務。

下面我們實際操作一下來講解AutoIt上傳文件的過程:
1、安裝AutoIt(下載網址:https://www.autoitscript.com/site/autoit/downloads/)
2、打開AutoIt Windows Info工具(我的電腦是64位的)
技術分享圖片
3、用鼠標左鍵點擊彈出窗口的Finder Tool,此時鼠標將變成一個小風扇形狀的圖標
技術分享圖片


4、按住Finder Tool不松,將其拖動到需要識別的控件上
窗口的title為“文件上傳”,標題的class為“#32770”
技術分享圖片
文件名輸入框的class為“Edit”,Instance為“1”,所以classnameNN為“ComboBox1”
技術分享圖片
打開按鈕的class為“Button”,Instance為“1”,所以classnameNN為“Button1”
技術分享圖片
5、根據AutoIt Windows Info所識別到的控件信息打開SciTE Script Editor編輯器,編寫AutoIt腳本。

;ControlFocus("title","text",controlID) ComboBox1=Combobox instance 1
ControlFocus("文件上傳", "" , "Edit1")

;Wait 10 Seconds for the Upload window to appear
WinWait("[CLASS:#32770]", "", 10)

;Set the file name text on the Edit field
ControlSetText("文件上傳", "", "Edit1", "alert.html")
Sleep(2000)

;Click on the open button
ControlClick("文件上傳", "", "Button1")

6、保存文件後,打開Compile Script to.exe工具,將其生成為.exe可執行文件
技術分享圖片

註意點

  • 不同瀏覽器的窗口title有可能不一樣
    技術分享圖片
    技術分享圖片
    技術分享圖片
  • 文件名輸入框不要定位到下拉框上去
    技術分享圖片
    技術分享圖片
  • 註意上傳路徑的分隔符是單斜杠還是雙斜杠
    技術分享圖片
  • exe執行多長時間,執行是否出錯,自動化腳本都無法得知(不在可控範圍內)

**代碼時間 **

Java

 1 package JavaTest;
 2 
 3 import java.io.IOException;
 4 import java.util.NoSuchElementException;
 5 import java.util.concurrent.TimeUnit;
6 import org.openqa.selenium.By; 7 import org.openqa.selenium.WebDriver; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 10 public class Test { 11 public static void main(String[] arg) throws InterruptedException, IOException 12 { 13 WebDriver driver = new FirefoxDriver(); 14 15 // 設置隱示等待時長:10秒; 16 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 17 driver.get("http://www.jq22.com/yanshi5310"); 18 19 try { 20 driver.switchTo().frame("iframe"); 21 driver.findElement(By.xpath("//*[@id=‘upload_form‘]/div[1]/div[2]")).click(); //打開上傳窗口 22 Runtime.getRuntime().exec("C:\\Users\\xxx\\Desktop\\upfile.exe"); // 調用upfile.exe上傳程序 23 } 24 catch(NoSuchElementException e) 25 { 26 System.out.println(e.getMessage()); 27 } 28 finally 29 { 30 driver.close(); 31 } 32 } 33 }

Python

 1 from selenium import webdriver
 2 from selenium.webdriver.common.by import By
 3 import os
 4 
 5 # 啟動Firefox瀏覽器
 6 driver = webdriver.Firefox()
 7 
 8 # 隱式等待10S,打開網址(可直接通過frame的id和name定位)
 9 driver.implicitly_wait(10)
10 driver.get("http://www.jq22.com/yanshi5310")
11 
12 try:
13     driver.switch_to.frame("iframe")
14     driver.find_element(By.XPATH, "//*[@id=‘upload_form‘]/div[1]/div[2]").click()  # 打開上傳窗口
15     os.system("C:\\Users\\xxx\\Desktop\\upfile.exe") # 調用upfile.exe上傳程序
16 except Exception as e:
17     print(e.args[0])
18 finally:
19     driver.close()

Ruby

 1 class Baidu
 2   require rubygems
 3   require selenium-webdriver
 4 
 5   # 打開firefox並輸入網址
 6   driver = Selenium::WebDriver.for :firefox
 7 
 8   # 設置隱式等待時間10S
 9   driver.manage.timeouts.implicit_wait = 10
10   driver.navigate.to "http://www.jq22.com/yanshi5310"
11 
12   begin
13     driver.switch_to.frame(iframe)
14     driver.find_element(:xpath => "//*[@id=‘upload_form‘]/div[1]/div[2]").click   # 打開上傳窗口
15     exec("C:\\Users\\xxx\\Desktop\\upfile.exe")
16   rescue => e
17     puts e.message # 顯示報錯信息
18   ensure
19     driver.close
20   end
21 end

下載文件

話不多說,<( ̄︶ ̄)↗[GO!]
FireFox about:config詳細介紹:https://www.cnblogs.com/abcd19880817/p/7210711.html
技術分享圖片

**代碼時間 **

Java

FirefoxProfile fp = new FirefoxProfile();
// 為0是下載到瀏覽器默認下載路徑,1是“我的下載”;2是自定義
fp.setPreference("browser.download.folderList", 2);
// 是否顯示開始
fp.setPreference("browser.download.manager.showWhenStarting", false);
// 指定所下載文件的目錄
fp.setPreference("browser.download.dir", "d:\\");
// 下載文件類型
fp.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/octet-stream");

Python

fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList", 2)
fp.set_preference("browser.download.manager.showWhenStarting", False)
# 指定所下載文件的目錄。os.getcwd()函數不需要傳遞參數,用於返回當前的目錄
fp.set_preference("browser.download.dir",os.getcwd())
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream")

Ruby

profile = Selenium::WebDriver::Firefox::Profile.new
profile[browser.download.folderList] = 2
profile[browser.download.manager.showWhenStarting] = false

driver = Selenium::WebDriver.for :firefox, :profile => profile

【Web自動化測試——代碼篇八】常用方法——上傳/下載文件