使用第三方工具AutoIt,實現文件上傳

使用第三方工具AutoIt,實現文件上傳

用於測試的HTML代碼:

<html>
    <head>
        <title>上傳文件</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body>
        <form enctype="multipart/form-data" action="parse_file.jsp" method="post">
            <p>Browse for a file to upload: </p>
            <input id="file" name="file" type="file">
            <br>
            <br>
            <input id="filesubmit" value="SUBMIT" type="submit">
        </form>
    </body>
</html>

目的:實現操作有一些webdriver無法操作的文件上傳對象。

AutoIt工具的使用説明

  1. 下載安裝工具

             訪問:https://www.autoitscript.com/site/sutoit/downloads/網址,下載工具包並進行安裝。

        2.   編輯操作文件上傳框體的AutoIt腳本

1)“開始”→“所有程序”→“AutoIt v3”→“SciTE”→“SciTE”命令,啓動AutoIt的文本編輯器。

2)在編輯器中輸入如下腳本:

                   #include <Constants.au3>

                   Send("e:\test.txt")   #使用鍵盤輸入待上傳文件的路徑

                   Send("{ENTER}")    #發送一個回車鍵

                   Send("{ENTER}")    #再次發送一個回車鍵

Enter鍵,主要是解決某些操作系統默認的輸入法是中文輸入法,輸入“e:\test.txt”以後,必須按一下Enter鍵才能將輸入的內容寫入路徑輸入框中,再按一

Enter鍵,就等價於單擊文件打開窗體的“打開”按鈕。

3)將AutoIt腳本保存為文件名為“test.au3”的文件並存放在指定磁盤上。

                   如下圖所示:

                       

sentry vite 實現構建自動上傳 sourcemap_html

4)“開始”→“所有程序”→“AutoIt v3”→“Compile script to.exe(x64)”(根據自己的操作系統位數選擇正確的位數),調出將AutoIt腳本轉換成exe文件的界面。

5)在Source路徑框中選擇上面保存的AutoIt腳本“test.au3”文件, Destination處選擇.exe單選項,並在接下來的輸入框中設置好生成exe文件的保存路徑,其他默認即可。

6)單擊Convert按鈕,將會把AutoIt腳本“test.au3”文件轉換成“test.exe”可執行文件。

             如下圖所示:

                 

sentry vite 實現構建自動上傳 sourcemap_AutoIt_02

  

實例代碼:

#encoding=utf-8
from selenium import webdriver
import unittest
import time, os
import traceback
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException

class TestDemo(unittest.TestCase):

    def setUp(self):
        # 啓動瀏覽器
        self.driver = webdriver.Ie(executable_path = "d:\\IEDriverServer")

    def test_uploadFileByAutoIt(self):
        url = "http://127.0.0.1/test_upload_file.html"
        # 訪問自定義網頁
        self.driver.get(url)
        try:
            # 創建一個顯示等待對象
            wait = WebDriverWait(self.driver, 10, 0.2)
            # 顯示等待判斷被測試頁面上的上傳文件按鈕是否處於可被點擊狀態
            wait.until(EC.element_to_be_clickable((By.ID, 'file')))
        except TimeoutException, e:
            # 捕獲TimeoutException異常
            print traceback.print_exc()
        except NoSuchElementException, e:
            # 捕獲NoSuchElementException異常
            print traceback.print_exc()
        except Exception, e:
            # 捕獲其他異常
            print traceback.print_exc()
        else:
            # 查找頁面上ID屬性值為“file”的文件上傳框,
            # 並點擊調出選擇文件上傳框
            self.driver.find_element_by_id("file").click()
            # 通過Python提供的os模塊的system方法執行生成的test.exe文件,實現文件上傳
            os.system("D:\AutoIt3\SciTE\\test.exe")
            # 由於AutoIt腳本轉換後的可執行文件test.exe可能執行速度比較慢,這裏等待5秒,以確保test.exe腳本執行成功
            time.sleep(5)
            # 找到頁面上ID屬性值為“filesubmit”的文件提交按鈕對象
            fileSubmitButton = self.driver.find_element_by_id("filesubmit")
            # 單擊提交按鈕,完成文件上傳操作
            fileSubmitButton.click()
            # 因為文件上傳需要時間,所以這裏可以添加顯示等待場景,判斷文件上傳成功後,頁面是否跳轉到文件上傳成功的頁面。
            # 通過EC.title_is()方法判斷跳轉後的頁面的Title值是否符合期望,如果匹配將繼續執行後續代碼
            #wait.until(EC.title_is(u"文件上傳成功"))
            time.sleep(2)
    def tearDown(self):
        # 退出IE瀏覽器
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()