環境搭建
python3
Java JDK
.netFramework
nodejs
android SDK
appium
Appium-Python-Client(pip install Appium-Python-Client)
連接設備
cmd打開命令行窗口
輸入adb connect 127.0.0.1:62001連接模擬器
輸入adb shell dumpsys window windows | findstr "Current"獲取當前包名
啓動appium
常用元素定位
driver.find_element_by_id
driver.find_element_by_class
driver.find_element_by_name
driver.find_element_by_xpath(//*[@text=‘text屬性’])
編寫登錄腳本
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from appium import webdriver
import unittest
import time
desired_caps = {
'platformName': 'Android',
'platfromVersion': '5.1',
'deviceName': '127.0.0.1:62001',
'appPackge': 'com.xxxx.artstation',
'appActivity': 'com.xxxx.artstation.main.login.activity.LoginActivity'
}
# TestCase類,所有測試用例繼承的基本類
class LoginTest(unittest.TestCase):
# 測試前執行的初始化工作
def setUp(self):
self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
# 測試用例執行後的善後工作。如關閉數據庫連接,退出應用。無論寫在哪,最後一個執行
def tearDown(self):
self.driver.quit()
# 測試用例,必須以test開頭
def test_login(self):
self.driver.find_element_by_id('com.xxxx.artstation:id/tv_sure').click()
time.sleep(3)
# 輸入賬號密碼
self.driver.find_element_by_id(
'com.xxxx.artstation:id/clear_edittext_username').send_keys('158xxxxxxxx')
self.driver.find_element_by_id(
'com.xxxx.artstation:id/clear_edittext_password').send_keys('123456')
# 點擊登錄按鈕
self.driver.find_element_by_id(
'com.xxxx.artstation:id/tv_login').click
time.sleep(3)
自動生成測試報告
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import HTMLTestRunner
import unittest
from testcase import test_login
if __name__ == '__main__':
# 實例化測試套件,定義一個測試容器
suite = unittest.TestSuite()
# 加載測試用例
suite.addTest(test_login.LoginTest('test_login'))
# 使用discover方法批量加載運行測試用例
# suite= unittest.defaultTestLoader.discover('../testcase','test_*.py')
# runner = unittest.TextTestRunner()
# 定義測試報告存放路徑和報告名稱
with open('HTMLReropt.html', 'wb')as f:
runner = HTMLTestRunner.HTMLTestRunner(
stream=f,
verbosity=2,
title='XX登錄自動化測試報告',
description='執行人:嘻嘻'
)
runner.run(suite)
# 關閉測試報告
f.close()
測試報告截圖