干貨| app自動(dòng)化測(cè)試之Andriod微信小程序的自動(dòng)化測(cè)試
隨著微信小程序的功能和生態(tài)日益完善,很多公司的小程序項(xiàng)目頁面結(jié)構(gòu)越來越多,業(yè)務(wù)邏輯也越來越復(fù)雜。如何做好小程序的自動(dòng)化測(cè)試就成為測(cè)試同學(xué)普遍面臨的一大痛點(diǎn)難題。
微信小程序
小程序內(nèi)嵌于微信內(nèi)部,頁面包含 Native 原生元素和 Web 元素,相當(dāng)于混合應(yīng)用。并且,小程序 Web 部分是基于騰訊的 X5 內(nèi)核開發(fā)的,也是特殊的 WebView。那么,對(duì)微信小程序進(jìn)行自動(dòng)化測(cè)試,包括操作原生應(yīng)用、WebView、基于微信官方自動(dòng)化 SDK。
WebView頁面元素獲取
使用元素定位工具:
weditor
weditor安裝方式
pip install weditor
參考文檔:GitHub - alibaba/web-editor: web editor for atx 2?使用 chrome inspect 定位時(shí),解析元素是把頁面解析為了 html 頁面,使用 weditor,則會(huì)把頁面解析為原生頁面,而 Appium 在操作元素時(shí),也是把頁面解析成了原生去操作的(切換 webview 除外)
Mac:
adb shell dumpsys activity top| grep ACTIVITY
Windows:
adb shell dumpsys activity top| findstr ACTIVITY


from time import sleep
from appium import webdriver
class TestDemo:
?
??def setup(self):
? ? ? ?
self.desire_cap= {
? ? ? ??
? ?'automationName': "uiautomator2",
??
? ? ? ? ?"platformName": "Android",
??
? ? ? ? ?"deviceName": "wechat",
? ? ? ?
?? ?"appPackage": "com.tencent.mm",
? ??
? ? ? ?"appActivity": ".ui.LauncherUI",
??
? ? ? ? ?"noReset": "true",
? ?
?? ? ? ?'chromeOptions': {'androidProcess': 'com.tencent.mm:appbrand0'}
? ? ? ?}
??
? ? ?# androidProcess:webview是獨(dú)立進(jìn)程的,導(dǎo)致無法獲取,需要在 chromeOptions 添加 androidProcess 即可
? ? ? ?self.driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",self.desire_cap)
? ? ? ?self.driver.implicitly_wait(20)
??
?def teardown(self):
? ?
?? ?self.driver.quit()
? ?
def test_demo(self):
? ? ??
?# 剛打開微信時(shí),頁面加載時(shí)間未知,
?
?? ? ?# 需要通過find_element觸發(fā)隱式等待,防止后續(xù)操作失敗
? ? ? ?self.driver.find_element_by_xpath("//*[@text='通訊錄']")
? ?
? ?size = self.driver.get_window_size()
? ?
?? ?# 獲取當(dāng)前屏幕的寬、高
? ? ?
??width = size.get("width")
? ? ??
?height = size.get("height")
? ?
?? ?# 滑動(dòng)打開小程序頁面
? ? ??
?self.driver.swipe((width / 2), int((height * 0.2)), (width / 2), (height * 0.8), 2000)
? ?
? ?self.driver.find_element_by_xpath("//*[@resource-id='com.tencent.mm:id/gam' and @text='雪球']").click()
? ?
?? ?sleep(5)
? ? ? ?
print(self.driver.contexts,'第一次打印')
? ? ? ?self.driver.find_element_by_xpath("//*[@content-desc='搜索股票信息/代碼']/..").click()
? ??
? ?self.driver.find_element_by_xpath('//*[@text="請(qǐng)輸入股票名稱/代碼"]').send_keys("阿里巴巴")
? ? ?
??text = self.driver.find_element_by_xpath('//*[@content-desc="阿里巴巴"]')
? ??
? ?assert text
小程序的測(cè)試方法有很多種,因?yàn)槲⑿虐姹窘?jīng)常升級(jí),webview的設(shè)置也會(huì)發(fā)生變化,所以可用的測(cè)試方法可能會(huì)因?yàn)槊總€(gè)版本而不同