1.使用環境
安裝appium 、安卓SDK 、pycharm, 搭建環境此處省略
2.在doc環境下輸入以下命令
2.1 獲取當前啟動app的包名以及當前頁面的activity,用邢帥教育APP為例
adb shell dumpsys window w |findstr \/ |findstr name=
2.2 通過獲取到的包名獲取到啟動app的activity
adb shell dumpsys package com.xsteach.appedu
2.3 獲取設備的連接信息 adb devices
>adb devices
List of devices attached
ERLDU20429000579 device
3. 整個工程目錄如下圖所示
4. 基於關鍵字框架實現
邢帥教育App的啟動、登錄、搜索、退出一次簡單的自動化實踐
5. 新建一個DataAndKeywordForApp 的python工程
5.1.首先新建 Util包,用於實現測試過程調用的工具類方法,例如讀取配置文件,excel表,操作頁面元素,獲取當前時間等。
下面是獲取desired_caps的配置信息的python文件
#coding=utf-8
from Config.VarConfig import desiredcapsFilePath
from Util.ParseConfigFile import ParseConfigFile
def getDesiredcaps():
#獲取desired_caps的配置信息
pc=ParseConfigFile(desiredcapsFilePath)
items=pc.getItemsSection("Desired_caps")
desired_caps={
'platformName':items['platformname'],
'platformVersion':items['platformversion'],
'deviceName': items['devicename'],
'appPackage' :items['apppackage'],
'appActivity': items['appactivity'],
'autoAcceptAlerts':True,
'unicodeKeyboard':True,
'resetKeyboard':True,
'noReset':True,
'newCommandTimeout':6000
}
return desired_caps
if __name__ == '__main__':
print(getDesiredcaps())
5.2. 解析Excel表的python文件 ParseExcel.py
#encoding=utf-8
import openpyxl # 引入openpyxl
from openpyxl.styles import Border, Side, Font,colors
import time
class ParseExcel(object):
def __init__(self):
self.workbook = None
self.excelFile = None
self.font = Font(color = None) # 設置字體的顏色
# 顏色對應的colors值
self.RGBDict = {'red': colors.RED, 'green': colors.GREEN,'blue':colors.BLUE}
def loadWorkBook(self, excelPathAndName):
"""
:函數功能: 將excel文件加載到內存,並獲取其workbook對象
:參數:
excelPathAndName: string, excel文件所在絕對路徑
:返回: workbook對象
"""
try:
self.workbook = openpyxl.load_workbook(excelPathAndName)
except Exception as e:
raise e
self.excelFile = excelPathAndName
return self.workbook
def getSheetByName(self, sheetName):
"""
:函數功能: 通過sheet名獲取sheet對象
:參數:
sheetName: string,sheet名
:返回值: sheet object
"""
try:
sheet = self.workbook[sheetName]
return sheet
except Exception as e:
raise e
def getSheetByIndex(self, sheetIndex):
"""
:函數功能: 通過索引號獲取sheet對象
:參數:
sheetIndex: int,sheet索引號
:返回值: sheet object
"""
try:
sheetname = self.workbook.sheetnames[sheetIndex]
except Exception as e:
raise e
sheet = self.workbook[sheetname]
return sheet
def getRowsNumber(self, sheet):
"""
:函數功能: 獲取sheet中存在數據區域的結束行號
:參數:
sheet: sheet object
:返回值: int,數據區域的結束行號
"""
return sheet.max_row
def getColsNumber(self, sheet):
"""
:函數功能: 獲取sheet中存在數據區域的結束列號
:參數:
sheet: sheet object
:返回值: int,數據區域的結束列號
"""
return sheet.max_column
def getStartRowNumber(self, sheet):
"""
:函數功能: 獲取sheet中有數據區域的起始的行號
:參數:
sheet: sheet object
:返回值: int,數據區域的起始的行號
"""
return sheet.min_row
def getStartColNumber(self, sheet):
"""
:函數功能: 獲取sheet中有數據區域的開始的列號
:參數:
sheet: sheet object
:返回值: int,數據區域的開始的列號
"""
return sheet.min_column
def getRow(self, sheet, rowNo):
"""
:函數功能: 獲取sheet中的行對象
:參數:
sheet: sheet object
rowNo: int,行索引號, 下標從1開始,1表示第一行...
:返回值: object,一行中所有的數據內容組成的tuple對象
"""
try:
rows=[]
for row in sheet.iter_rows():
rows.append(row)
return rows[rowNo - 1]
except Exception as e:
raise e
def getColumn(self, sheet, colNo):
"""
:函數功能: 獲取sheet中的列對象
:參數:
sheet: sheet object
colNo: int,列索引號,下標從1開始,1表示第一列...
:返回值: Object,一列中所有的數據內容組成tuple對象
"""
try:
cols=[]
for col in sheet.iter_cols():
cols.append(col)
return cols[colNo - 1]
except Exception as e:
raise e
def getCellOfValue(self, sheet, coordinate = None, rowNo = None, colsNo = None):
"""
:函數功能: 獲取指定表格中指定單元格的值
:參數:
sheet: sheet object
coordinate: string,坐標值,比如A1
rowNo: int,行索引號,下標從1開始,1表示第一行...
colsNo: int,列索引號,下標從1開始,1表示第一列...
:返回值: string or int,指定單元格的內容
:示例:
getCellOfValue(sheet, coordinate = 'A1')
or
getCellOfValue(sheet, rowNo = 1, colsNo = 2)
"""
if coordinate != None:
try:
return sheet[coordinate].value
except Exception as e:
raise e
elif coordinate is None and rowNo is not None and colsNo is not None:
try:
return sheet.cell(row = rowNo, column = colsNo).value
except Exception as e:
raise e
else:
raise Exception("Insufficient Coordinates of cell !")
def getCellOfObject(self, sheet, coordinate = None, rowNo = None, colsNo = None):
"""
:函數功能: 獲取指定sheet中的指定單元格對象
:參數:
sheet: sheet object
coordinate: string,坐標值,比如A1
rowNo: int,行索引號,下標從1開始,1表示第一行...
colsNo: int,列索引號,下標從1開始,1表示第一列...
:返回值: object,指定單元格對象
:示例:
getCellObject(sheet, coordinate = 'A1')
or
getCellObject(sheet, rowNo = 1, colsNo = 2)
"""