Python+Appium App自動化實踐——關鍵字驅動測試框架(1)


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)
"""
if coordinate != None:
try:
return sheet[coordinate]
except Exception as e:
raise e
elif coordinate == None and rowNo is not None and colsNo is not None:
try:
return sheet.cell(row = rowNo, column = colsNo)
except Exception as e:
raise e
else:
raise Exception("Insufficient Coordinates of cell !")

def writeCell(self, sheet, content, coordinate = None, rowNo = None, colsNo = None, style = None):
"""
:函數功能: 向指定sheet中的指定單元格寫入數據
:參數:
sheet: sheet object
content: string/int,所寫內容
coordinate: string,坐標值,比如A1
rowNo: int,行索引號,下標從1開始,1表示第一行...
colsNo: int,列索引號,下標從1開始,1表示第一列...
style: string,所寫內容顏色,red / green
:返回值: 無
"""
if coordinate is not None:
try:
sheet[coordinate].value = content
if style is not None:
sheet[coordinate].font = Font(color = self.RGBDict[style])
self.workbook.save(self.excelFile)
except Exception as e:
raise e
elif coordinate == None and rowNo is not None and colsNo is not None:
try:
sheet.cell(row = rowNo,column = colsNo).value = content
if style:
sheet.cell(row = rowNo,column = colsNo).font = Font(color = self.RGBDict[style])
self.workbook.save(self.excelFile)
except Exception as e:
raise e
else:
raise Exception("Insufficient Coordinates of cell !")

def writeCellCurrentTime(self, sheet, coordinate = None, rowNo = None, colsNo = None,style = None):
"""
:函數功能: 向指定sheet的指定單元格中寫入當前時間
:參數:
sheet: sheet object
coordinate: string,坐標值,比如A1
rowNo: int,行索引號,下標從1開始,1表示第一行...
colsNo: int,列索引號,下標從1開始,1表示第一列...
:返回值: 無
"""
currentTime = time.strftime("%Y-%m-%d %H:%M:%S")
if coordinate is not None:
try:
sheet[coordinate].value = currentTime
self.workbook.save(self.excelFile)
except Exception as e:
raise e
elif coordinate == None and rowNo is not None and colsNo is not None:
try:
sheet.cell(row = rowNo, column = colsNo).value = currentTime
if style:
sheet.cell(row = rowNo,column = colsNo).font = Font(color = self.RGBDict[style])
self.workbook.save(self.excelFile)
except Exception as e:
raise e
else:
raise Exception("Insufficient Coordinates of cell !")

 

 5.3 新建Config包,用於存放各種配置文件

[Desired_caps]
platformName=Android
platformVersion=10
deviceName=ERLDU20429000579
appPackage=com.xsteach.appedu
appActivity=com.xsteach.appedu.StartActivity
unicodeKeyboard=True
autoAcceptAlerts=True
resetKeyboard=True
noReset=True
newCommandTimeout=6000

5.4 在Util包下新建 ObjectMap.py 獲取頁面元素操作對象

#encoding=utf-8
from selenium.webdriver.support.ui import WebDriverWait
element = WebDriverWait(driver, 10).until
#這里是webdriver 隱式等待10秒

# 獲取單個頁面元素對象
def getElement(driver, locationType, locatorExpression):
try:
element = WebDriverWait(driver, 10).until(
lambda x: x.find_element(by=locationType, value = locatorExpression))
return element
except Exception as e:
raise e

# 獲取多個相同頁面元素對象,以list返回
def getElements(driver, locationType, locatorExpression):
try:
elements = WebDriverWait(driver, 10).until(
lambda x:x.find_elements(by=locationType, value = locatorExpression))
return elements
except Exception as e:
raise e
 

 

 

 

  

 

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM