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
 

 

 

 

  

 

  

 

posted on 2020-10-26 15:56  淡淡的风景  阅读(587)  评论(2编辑  收藏  举报