一.目的
最近接到一個測試任務windos版本的自助收銀客戶端測試工作,由于是個長期的迭代項目就考慮把穩定功能納入自動化測試框架
二.框架調研,選用winAppDriver
image
三.python3.8+win10+winAppDriver +appium 搭建windows 桌面應用的UI自動化環境
1.安裝python3.8 windows版本:https://www.python.org/getit/,安裝pip同時升級pip的版本
2.pip安裝 selenium,appium
3.安裝 winAppDriver :
下載地址:https://github.com/Microsoft/WinAppDriver/releases
WinAppDriver(Windows Application Driver)是一個類似Selenium的UI自動化測試服務系統要求: Windows10或Windows Server 2016支持應用程序: UWP, WPF, WinForms, Win32
4.設置開發模式,讓程序可以連接上WinAppDrive的服務
image
5.inspect.exe 組件識別工具檢查
系統一般自帶, 路徑C:\Program Files (x86)\Windows Kits\10\bin\x64,如果沒有安裝一個windowSDK;https://download.microsoft.com/download/4/d/2/4d2b7011-606a-467e-99b4-99550bf24ffc/windowssdk/winsdksetup.exe
image
四.樣例實戰,啟動微信桌面版本,監控指定的群,有@自己和@all的時候自動回復信息
import time,os,sys
import unittest
from appiumimport webdriver
from selenium.webdriverimport ActionChains
from selenium.webdriver.common.keysimport Keys
class NotepadTests(unittest.TestCase):
@classmethod
def setUpClass(self):
#啟動WinAppDriver.exe
os.system(r'@start /d "C:\Program Files (x86)\Windows Application Driver\" WinAppDriver.exe')
desired_caps = {}
#微信的安裝路徑
desired_caps['app'] =r"C:\tools\WeChat\WeChat.exe"
self.driver = webdriver.Remote(
command_executor='http://127.0.0.1:4723',
desired_capabilities=desired_caps)
#self.driver.maximize_window()
@classmethod
def tearDownClass(self):
self.driver.quit()
#關閉WinAppDriver.exe
os.system(' @taskkill /f /im WinAppDriver.exe')#@tasklist|findstr /i WinAppDriver.exe &&
def test_edit(self):
new_msg=False
self.driver.find_element_by_name('財務自由').click()#“財務自由”群的名字
dd=self.driver.find_elements_by_xpath("http://*[@LocalizedControlType='列表項目']")
for din dd:
print(d.get_attribute('Name'))
#@黃 自己的微信名字
if '@黃' in d.get_attribute('Name')or '@所有人' in d.get_attribute('Name'):
new_msg=True
break
if new_msg:
self.driver.find_element_by_name('輸入').send_keys('我是機器人助理,主人暫時不在線')
time.sleep(3)
ActionChains(self.driver).send_keys(Keys.ENTER).perform()
time.sleep(3)
ActionChains(self.driver).send_keys(Keys.ENTER).perform()
self.driver.get_screenshot_as_file('1.png')#截圖
if __name__ =="__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(NotepadTests)
unittest.TextTestRunner(verbosity=2).run(suite)
image