python控制genymotion,利用计算器实现计算
1、环境搭建
Python+Appium 环境
--》安装node.js
--》安装Python
--》安装Appium-Python-Client
pip install Appium-Python-Client
-->安装appium
2、利用appium获取元素
{ "platformName": "Android", "deviceName": "192.168.231.101:5555", "platformVersion": "9", "appPackage": "com.android.calculator2", "appActivity": "com.android.calculator2.Calculator", "noReset": true }
单次连接操作
from appium import webdriver #定义字典,保存设备信息 device={} #设备种类 device['deviceName']='192.168.231.101:5555' #设备平台 device['platformName']='Android' #移动设备操作系统版本 device['platformVersion']='9' #运行程序包名 device['appPackage']='com.android.calculator2' #程序启动页名称 device['appActivity']='com.android.calculator2.Calculator' #设置不重新安装APP device['noReset']=True #启动连接设备 driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",device) data=[['12','+','22','34'],['23','-','1','22'],['3','*','4','12'],['12','/','3','4']] #将运算符,格式化 for i in range(len(data)): if data[i][1]=='+': data[i][1]='add' elif data[i][1]=='-': data[i][1]='sub' elif data[i][1]=='*': data[i][1]='mul' else: data[i][1]='div' # for i in data: for j in range(0,len(i)-1): num = i[j] if j!=1 : for a in num : driver.find_element_by_id("com.android.calculator2:id/digit_"+a).click() else: driver.find_element_by_id("com.android.calculator2:id/op_"+i[1]).click() driver.find_element_by_id("com.android.calculator2:id/eq").click() res=driver.find_element_by_id("com.android.calculator2:id/result").text if res==str(i[3]): print("通过") else: print("失败") print('actually=',res,'expected',i[3]) driver.quit()
3、参数化
from appium import webdriver #定义字典,保存设备信息 device={} #设备种类 device['deviceName']='192.168.231.101:5555' #设备平台 device['platformName']='Android' #移动设备操作系统版本 device['platformVersion']='9' #运行程序包名 device['appPackage']='com.android.calculator2' #程序启动页名称 device['appActivity']='com.android.calculator2.Calculator' #设置不重新安装APP device['noReset']=True #启动连接设备 driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",device) data=[['12','+','22','34'],['23','-','1','22'],['3','*','4','12'],['12','/','3','4']] #将运算符,格式化 for i in range(len(data)): if data[i][1]=='+': data[i][1]='add' elif data[i][1]=='-': data[i][1]='sub' elif data[i][1]=='*': data[i][1]='mul' else: data[i][1]='div' # for i in data: for j in range(0,len(i)-1): num = i[j] if j!=1 : for a in num : driver.find_element_by_id("com.android.calculator2:id/digit_"+a).click() else: driver.find_element_by_id("com.android.calculator2:id/op_"+i[1]).click() driver.find_element_by_id("com.android.calculator2:id/eq").click() res=driver.find_element_by_id("com.android.calculator2:id/result").text if res==str(i[3]): print("通过") else: print("失败") print('actually=',res,'expected',i[3]) driver.quit()
读取文件进行参数化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from appium import webdriver #定义字典,保存设备信息 device={} #设备种类 device['deviceName']='192.168.231.101:5555' #设备平台 device['platformName']='Android' #移动设备操作系统版本 device['platformVersion']='9' #运行程序包名 device['appPackage']='com.android.calculator2' #程序启动页名称 device['appActivity']='com.android.calculator2.Calculator' #设置不重新安装APP device['noReset']=True #启动连接设备 driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",device) #读取文件 file=open('E:\cal.txt','r') data =[] for rows in file: strs = rows.split() data.append(strs) #将运算符,格式化 for i in range(len(data)): if data[i][1]=='+': data[i][1]='add' elif data[i][1]=='-': data[i][1]='sub' elif data[i][1]=='*': data[i][1]='mul' else: data[i][1]='div' # for i in data: for j in range(0,len(i)-1): num = i[j] if j!=1 : for a in num : driver.find_element_by_id("com.android.calculator2:id/digit_"+a).click() else: driver.find_element_by_id("com.android.calculator2:id/op_"+i[1]).click() driver.find_element_by_id("com.android.calculator2:id/eq").click() res=driver.find_element_by_id("com.android.calculator2:id/result").text if res==str(i[3]): print("通过") else: print("失败") print('actually=',res,'expected',i[3]) driver.quit()
读取Excel进行参数化
![](https://img2020.cnblogs.com/blog/1993414/202101/1993414-20210127143650499-921064921.png)
import pandas
data = pandas.read_excel("e:\cal2.xls", sheet_name="Sheet1",names=["num1","op","num2","expected"])
data = data.values.tolist()
完整代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from appium import webdriver import pandas #定义字典,保存设备信息 device={} #设备种类 device['deviceName']='192.168.231.101:5555' #设备平台 device['platformName']='Android' #移动设备操作系统版本 device['platformVersion']='9' #运行程序包名 device['appPackage']='com.android.calculator2' #程序启动页名称 device['appActivity']='com.android.calculator2.Calculator' #设置不重新安装APP device['noReset']=True #启动连接设备 driver=webdriver.Remote("http://127.0.0.1:4723/wd/hub",device) #读取文件 data = pandas.read_excel("e:\cal2.xls", sheet_name="Sheet1",names=["num1","op","num2","expected"]) data = data.values.tolist() #将运算符,格式化 for i in range(len(data)): if data[i][1]=='+': data[i][1]='add' elif data[i][1]=='-': data[i][1]='sub' elif data[i][1]=='*': data[i][1]='mul' else: data[i][1]='div' # for i in data: for j in range(0,len(i)-1): num = i[j] num = str(num) if j!=1 : for a in num : driver.find_element_by_id("com.android.calculator2:id/digit_"+a).click() else: driver.find_element_by_id("com.android.calculator2:id/op_"+i[1]).click() driver.find_element_by_id("com.android.calculator2:id/eq").click() res=driver.find_element_by_id("com.android.calculator2:id/result").text if res==str(i[3]): print("通过") else: print("失败") print('actually=',res,'expected',i[3]) driver.quit()