# coding:utf-8
import csv #導入csv庫,可以讀取csv文件
from appium import webdriver
import unittest # 單元測試
from time import sleep # 等待時間
import time # 時間方法
import os # 文件、目錄方法
import sys # 引用系統函數
from HTMLTestRunner import HTMLTestRunner
# 定義路徑變量,放在python環境變量中。相對路徑用此方法本文使用的絕對路徑
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__name__)))
sys.path.insert(0,BASE_PATH)
class Test_case(unittest.TestCase):
def setUp(self):
print('開始測試')
def tearDown(self):
sleep(2)
self.driver.quit()
print('完成測試')
def test_login(self):
'''登陸測試'''
# 先獲取app的設備信息安裝後再執行測試用例測試
desired_cups = {} # 定義一個數據字典
desired_cups['device'] = 'android' # 安卓驅動
desired_cups['platformName'] = 'Android' # 設備平臺
desired_cups['platformVersion'] = '6.0' # 設備系統版本
desired_cups['deviceName'] = 'PLK-AL10C00B389' # 設備名稱
desired_cups['app'] = "D:\\迅雷下載\\文件名稱.apk" # 指定你測試apk文件,自己需要修改
desired_cups['noReset'] = True # 不需要重新安裝apk
# 要讀取的scv文件路徑
my_file = 'D:\\項目名稱_test\\app\\app_login.csv'
# csv.reader()讀取csv文件,
# Python3.X用open,Python2.X用file,'r'爲讀取
# open(file,'r')中'r'爲讀取權限,w爲寫入,還有rb,wd等涉及到編碼的讀寫屬性
data = csv.reader(open(my_file, 'r'))
# for循環將讀取到的csv文件的內容一行行循環,這裏定義了user變量(可自定義)
# user[0]表示csv文件的第一列,user[1]表示第二列,user[N]表示第N列
# for循環有個缺點,就是一旦遇到錯誤,循環就停止,所以用try,except保證循環執行完
for user in data:
# 啓動app先打開界面,可以每次都重新打開app,或者每次都在輸入框項做清空操作,步驟需要修改,
目前可以實現登錄測試生成報告。
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_cups)
sleep(3)
self.driver.find_element_by_xpath(
"//android.widget.TabWidget[@resource-id='android:id/tabs']/android.widget.RelativeLayout[4]").click()
sleep(3)
self.driver.find_element_by_id("com.presaint.mhexpress:id/btn_login").click()
# self.driver.find_element_by_id('input1').clear()
self.driver.find_element_by_id("com.presaint.mhexpress:id/et_username").send_keys(user[0])
# self.driver.find_element_by_id('input2').clear()
self.driver.find_element_by_id("com.presaint.mhexpress:id/et_password").send_keys(user[1])
# 單擊提交
self.driver.find_element_by_id("com.presaint.mhexpress:id/btn_login").click()
sleep(3)
print('\n' + '測試項:' + user[2])
self.driver.get_screenshot_as_file(path + user[3] + ".png")
try:
assert self.driver.find_element_by_xpath(user[4]).text
try:
error_message = self.driver.find_element_by_xpath(user[4]).text
self.assertEqual(error_message, user[5])
print('提示信息正確!預期值與實際值一致:')
print('預期值:' + user[5])
print('實際值:' + error_message)
except:
print('提示信息錯誤!預期值與實際值不符:')
print('預期值:' + user[5])
print('實際值:' + error_message)
except:
print('提示信息類型錯誤,請確認元素名稱是否正確!')
if __name__ == '__main__':
# 定義腳本標題,加u爲了防止中文亂碼
report_title = u'登陸模塊測試報告'
# 定義腳本內容,加u爲了防止中文亂碼
desc = u'登陸模塊測試報告詳情:'
# 定義date爲日期,time爲時間
date = time.strftime("%Y%m%d")
time = time.strftime("%Y%m%d%H%M%S")
# 定義path爲文件路徑,目錄級別,可根據實際情況自定義修改
path = 'D:/項目名稱_test/' + date + "/login/" + time + "/"
# 定義報告文件路徑和名字,路徑爲前面定義的path,名字爲report(可自定義),格式爲.html
report_path = path + "report.html"
# 判斷是否定義的路徑目錄存在,不能存在則創建
if not os.path.exists(path):
os.makedirs(path)
else:
pass
# 定義一個測試容器
test_suite = unittest.TestSuite()
# 將測試用例添加到容器
test_suite.addTest(Test_case("test_login"))
# 將運行結果保存到report,名字爲定義的路徑和文件名,運行腳本
with open(report_path, 'wb') as report:
runner = HTMLTestRunner(stream=report, title=report_title, description=desc)
runner.run(test_suite)
# 關閉report,腳本結束
report.close()
測試報告如下: