軟件測試是確認軟件質量的重要流程之一,一般藉由撰寫自動化測試程序來實現。視窗程式的自動化測試原理也很容易,其利用螢幕像素點作為定位參考,並以程式碼使滑鼠和鍵盤做動,如點擊或輸入文字等動作,使其可模擬人類操作視窗程式,最後自動化執行上千次或上萬次,以確保程式的可靠性。
本文以一個簡易完成後的連結數據庫的視窗程式為範例,並利用pyautogui簡易編寫的部分自動化程式碼來做說明,其中包含連接和斷開資料庫,以及添加數據於datagridview表格的動作。其中幾個重點摘要如下,對應視頻做動的完整源碼,可參考下方代碼段。
1.動作之間的時間需要預留時間間隔緩衝,避免程式邏輯雖然沒有問題,但測試過程中會出現非預期錯誤。
2.建議加上"pyautogui.FAILSAFE = True",方便用於代碼debug的程序中斷測試。
3.pyperclip的用途為補足無法輸入中文字,可再自行搜尋其用法,增加視窗測試的彈性。
---------------------------------------------------
import pyautogui
import pyperclip
#故障保險
pyautogui.PAUSE = 1
#當故障安全模式
pyautogui.FAILSAFE = True
for i in range(2):
# button location "connect DB"=>(1446,129)
pyautogui.moveTo(1446, 129, 0.5)
pyautogui.click(button='left')
# button location "disconnect DB"=>(1618,127)
pyautogui.moveTo(1618, 127, 0.5)
pyautogui.click(button='left')
# button location "connect DB"=>(1446,129)
pyautogui.moveTo(1446, 129, 0.5)
pyautogui.click(button='left')
# button location "add data"=>(1451,611)
pyautogui.moveTo(1451, 611, 1)
pyautogui.click(button='left')
# nondata and renew requirement confirm add button=>(2163, 449)
pyautogui.click(x=2163, y=449, button='left')
# txtbox location "品號"=>(1509,216)
pyautogui.click(x=1509, y=216, button='left')
pyautogui.write('989B', interval=0.1)
# txtbox location "品名"=>(1510,288)
pyautogui.click(x=1510, y=288, button='left')
pyperclip.copy('肯亞 AAA')
pyautogui.hotkey('Ctrl', 'v')
# txtbox location "價格"=>(1515,358)
pyautogui.click(x=1515, y=358, button='left')
pyautogui.write('1000', interval=0.1)
# txtbox location "客戶"=>(1511,428)
pyautogui.click(x=1511, y=428, button='left')
pyautogui.write('P33', interval=0.1)
# button location "add data"=>(1451,611)
pyautogui.moveTo(1451, 611, 1)
pyautogui.click(button='left')
# add data confirm "取消"button=>(2111, 452)
pyautogui.click(x=2111, y=452, button='left')
# button location "add data"=>(1451,611)
pyautogui.moveTo(1451, 611, 1)
pyautogui.click(button='left')
# add data confirm "確定"button=>(2010, 451)
pyautogui.click(x=2010, y=451, button='left')
# "成功新增一筆資訊" button=>(2095 448)
pyautogui.click(x=2095, y=448, button='left')
# txtbox location "品號"=>(1509,216)
pyautogui.click(x=1509, y=216, button='left')
pyautogui.hotkey('Ctrl', 'a')
pyautogui.press('backspace')
# txtbox location "品名"=>(1510,288)
pyautogui.click(x=1510, y=288, button='left')
pyautogui.hotkey('Ctrl', 'a')
pyautogui.press('backspace')
# txtbox location "價格"=>(1515,358)
pyautogui.click(x=1515, y=358, button='left')
pyautogui.hotkey('Ctrl', 'a')
pyautogui.press('backspace')
# txtbox location "客戶"=>(1511,428)
pyautogui.click(x=1511, y=428, button='left')
pyautogui.hotkey('Ctrl', 'a')
pyautogui.press('backspace')
# button location "disconnect DB"=>(1618,127)
pyautogui.moveTo(1618, 127, 1)
pyautogui.click(button='left')
---------------------------------------------------
Reference:
沒有留言:
張貼留言