Appium+python實現微信小程序自動化的實踐與探索
關於這個呢,可以噠,真的可以噠,現在小程序自動化的工具不多,這個可用。
但是,我還是希望大家和我一起研究一下,微信小程序自動化框架:Minium + 微信開發者工具 (一),大家可以去我的首頁找下相關文章,畢竟是微信自己的開發的工具,怎麼說呢,微信的親兒子吧。
1.Appium的安裝
Appium是開源的自動化測試框架,主要用於iOS,Android以及Windows apps等移動平臺的自動化測試。
新版的Appium做了優化,安裝比以前簡單很多了。
首先呢,環境依賴安裝好
1.安裝JDK:在JAVA官網http://www.oracle.com/technetwork/java/javase/downloads/index.html下載JDK的安裝包,推薦8版本以上的。
2.安裝SDK:雙擊SDK的安裝文件,使用默認值一路下一步,直到完成安裝,配置好adb環境變量,這個環境變量應該不用教吧,自行百度;確保OK運行adb如下,不然運行appium會報錯的,兄弟。
連接真機後,運行adb devices,要找到自己的設備哦,運行顯示如下:如果找不到,手機要打開調試;
3.下面安裝Appium桌面版本appium-desktop-Setup-1.2.1.exe ,找不到資源的小夥伴,在我的首頁資源裏面也有。
安裝完成後打開,點擊start。然後點擊長得像搜索的控件,我們先看下是不是OK的。
填入參數如下,什麼手機版本,信息啊,devicename就是剛剛adb devices運行得到的設備id:
{
"platformName": "Android",
"platformVersion": "9",
"deviceName": "a57c5aae",
"appPackage": "com.tencent.mm",
"appActivity": "com.tencent.mm.ui.LauncherUI",
"udid": "a57c5aae",
"noReset": true,
"automationName": "uiautomator2"
}
點擊startsession,如果一切OK的話那麼,手機提示安裝一些東西,記得點擊允許哦。
可以得到手機的畫面,並且啓動了微信。一切OK,環境沒有問題。
2.安裝python以及依賴Appium-python-Client
官網下載安裝最新的python,並且添加環境變量;
添加pip環境變量
CMD執行pip install Appium-python-Client
這個簡單理解就是python和appium連接的工具啦
安裝完成後,我們就可以通過python腳本來控制appium啦;
3.測試腳本的編寫
上面我們打開的這個窗口就可以關閉啦,上面的說明appium和手機連接沒有問題;現在我們通過python腳本連接appium再連接手機是不是很簡單;可以查看元素的信息啊,比如說xpah啊等等,還可以查看name啊等等
這個窗口記得保留,別把服務都關了,還玩個毛線啊
測試用例以及腳本使用工具PyCharm,是一種Python IDE,帶有一整套可以幫助用戶在使用Python語言開發時提高其效率的工具。
我們先看一個簡單的例子,連接手機,打開微信,點擊進入小程序,點擊登錄按鈕...............:
對了我這邊用的是xpath來定位元素的;
# -*- coding:utf-8 -*-
# Author:Jin Fei
# -*- coding:utf-8 -*-
# Author:Jin Fei
import unittest
from appium import webdriver
import time,os
from Mini.Function.common import *
class MyTests(unittest.TestCase):
# 測試開始前執行的方法,測試開始前的準備
def setUp(self):
desired_caps = {
'platformName': 'Android',
'platformVersion': '9',
'deviceName': 'a57c5aae',
'appPackage': 'com.tencent.mm',
'appActivity': 'com.tencent.mm.ui.LauncherUI',
'automationName': 'Uiautomator2',
# 'udid': 'a57c5aae',
# 'resetKeyboard': True,
'noReset': True,
"automationName": "uiautomator2"
}
self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps) # 連接Appium
self.driver.implicitly_wait(8)
def test001_miniprogram_login(self, t=500):
"""登錄並進入家電列表頁面"""
time.sleep(3)
window = self.driver.get_window_size()
x1 = window['width'] * 0.5 # 起始x座標
y1 = window['height'] * 0.2 # y1座標,滑動起始點
y2 = window['height'] * 0.7 # y2座標,滑動末尾點
self.driver.swipe(x1,y1,x1,y2,t) # 頁面下拉
time.sleep(2)
self.driver.find_element_by_id('com.tencent.mm:id/ka').click() # 點擊進入小程序
time.sleep(2)
add_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.widget.Button/android.view.View[3]/android.view.View[3]"
self.driver.find_element_by_xpath(add_xpath).click()
time.sleep(2)
login1_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.view.View[2]/android.view.View[2]/android.view.View"
self.driver.find_element_by_xpath(login1_xpath).click()
time.sleep(2)
inputbox_account_xpath = "/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.RelativeLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[1]/android.widget.FrameLayout/android.view.ViewGroup/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.webkit.WebView/android.view.View[2]/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View"
self.driver.find_element_by_xpath(inputbox_account_xpath).click()
time.sleep(2)
#點開數字鍵盤
self.driver.tap([(230, 2023),(230, 2023)],100)
time.sleep(2)
# 測試結束後執行的方法
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()
4.我這邊先解釋一下我的目錄和路徑
當前小程序測試腳本路徑如下:
- 自定義函數Function如下:
# -*- coding:utf-8 -*-
# Author:Jin Fei
import unittest
from appium import webdriver
import time,os
class Function:
def isElementExist(self,element):
#判斷元素
flag=True
driver=self.driver
try:
driver.find_element_by_xpath(element)
return flag
except:
flag=False
return flag
- Case如下,代碼上面有例子:
- Run腳本集結case運行並生成報告:前輩們寫的HTMLTestRunner腳本太長了,我重新起了一個博客裏面直接複製作爲第三方模塊使用。
# -*- coding:utf-8 -*-
# Author:Jin Fei
import unittest,time
import os
import sys
sys.path.append(os.path.abspath(os.path.dirname(os.getcwd())))
from Mini.packages.HTMLTestRunner import HTMLTestRunner
test_dir = '../case'
discover = unittest.defaultTestLoader.discover(test_dir, pattern='test*.py')
if __name__ == '__main__':
now = time.strftime("%Y-%M-%D %H_%M_%S")
filename = 'result.html'
fp = open(filename,'wb')
runner = HTMLTestRunner(stream=fp,
title='Test report Mini',
description='用例執行情況:')
runner.run(discover)
fp.close()
4.測試報告
- 測試報告是html的格式,summary格式如下,測試報告生成的話需要下載一個模塊HTML TestRunner