手机:华为荣耀V9
# coding:utf-8
from appium import webdriver
import time
import random
def str_splice(x):
str1 = "com.android.calculator2:id/" + x
return str1
#生成1~5位数的随机数
def number_element():
element_list = []
for i in range(1, random.randint(2, 6)):
element_list.append(str_splice(number_list[random.randint(0, 10)]))
for i in range(0, len(element_list)):
driver.find_element_by_id(element_list[i]).click()
element_list.clear()
#运算符号随机选择
def symbol_element():
driver.find_element_by_id(str_splice(symbol_list[random.randint(0, 3)])).click()
#操作顺序:随机数 + 运算符号 + 随机数 + “=” + “清空”
def formula():
number_element()
symbol_element()
number_element()
driver.find_element_by_id("com.android.calculator2:id/eq").click()
driver.find_element_by_id("com.android.calculator2:id/op_clr").click()
#配置appium驱动
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['deviceName'] = 'SALDU17303000215'
desired_caps['platformVersion'] = '9'
desired_caps['appPackage'] = 'com.android.calculator2'
desired_caps['appActivity'] = 'com.android.calculator2.Calculator'
desired_caps['autoGrantPermissions'] = True
desired_caps['noReset'] = True
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
#设置数字列表
number_list = ["digit_0", "digit_1", "digit_2", "digit_3",
"digit_4", "digit_5", "digit_6", "digit_7",
"digit_8", "digit_9", "dec_point"]
#设置运算符号列表
symbol_list = ["op_div", "op_mul", "op_sub", "op_add"]
#测试20次公式
for i in range(0, 20):
formula()
time.sleep(10)
driver.quit()