#案例:python+appium的一个复杂案例!
from appium import webdriver
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from appiumUtils import click_select
from appiumUtils import caozuoEleNotScreen
# server 启动参数
desired_caps = {}
# 设备信息
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = '127.0.0.1:62001'
# 指定要操作某app的某UI
desired_caps['appPackage'] = 'com.android.contacts'
desired_caps['appActivity'] = '.activities.PeopleActivity'
desired_caps['noReset']=True
#指定Appium服务器的ip和端口
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
#定位元素“+”,并点击
driver.find_element_by_id("com.android.contacts:id/floating_action_button").click()
time.sleep(3)
#定位元素“姓名”,并输入
driver.find_element_by_xpath("//*[@text='姓名']").send_keys("张三")
#定位元素“电话”,并输入
driver.find_element_by_xpath("//*[@text='电话']").send_keys("13760453683")
#定位元素下列列表“电话类型”,并点击,目的是为了让下列列表展开!
ele_select_phoneType=driver.find_element_by_xpath("//*[@text='手机']")
ele_select_phoneType.click()
#点击展开后的下列列表中的某选项
loc_select=By.CLASS_NAME,"android.widget.ListView"
loc_option=By.XPATH,"//*[@text='助理']"
click_select(driver,loc_select,loc_option)
time.sleep(3)
#定位元素“电子邮件”,并输入
driver.find_element_by_xpath("//*[@text='电子邮件']").send_keys("851286894@qq.com")
#选择“电子邮件”类型
driver.find_element_by_xpath("//*[@text='个人']").click()
loc_select1=By.CLASS_NAME,"android.widget.ListView"
loc_option1=By.XPATH,"//*[@text='工作']"
click_select(driver,loc_select1,loc_option1)
time.sleep(2)
#定位元素“地址”,并输入
loc_addr=By.XPATH,"//*[@text='地址']"
caozuoEleNotScreen(driver,loc_addr,dict="up",caozuoType="input",data="深圳市南山区XXX小区")
#定位元素“公司”,并输入
loc_company=By.XPATH,"//*[@text='公司']"
caozuoEleNotScreen(driver,loc_company,dict="up",caozuoType="input",data="深圳哇哈哈集团")
#定位元素“聊天软件号码”,并输入
loc_qqHaoma=By.XPATH,"//*[@text='聊天工具']"
caozuoEleNotScreen(driver,loc_qqHaoma,dict="up",caozuoType="input",data="851286894")
#选中“聊天软件”
loc_AIM=By.XPATH,"//*[@text='AIM']"
caozuoEleNotScreen(driver,loc_AIM,dict="up",caozuoType="click",data=None)
loc_select2=By.CLASS_NAME,"android.widget.ListView"
loc_option2=By.XPATH,"//*[@text='ICQ']"
click_select(driver,loc_select2,loc_option2)
#定位元素“网站”,并输入
loc_website=By.XPATH,"//*[@text='网站']"
caozuoEleNotScreen(driver,loc_website,dict="up",caozuoType="input",data="http://wahaha.com")
#定位元素“保存”按钮,并点击
driver.find_element_by_class_name("android.widget.ImageButton").click()
#断言:断言逻辑:保存后UI中用户名是"张三"
time.sleep(3)
ele_yonhumin=driver.find_element_by_id("com.android.contacts:id/large_title")
attr_text=ele_yonhumin.text
if attr_text=="张三":
print("成功")
else:
print("失败")
time.sleep(5) #休眠5秒
driver.quit()
工具类
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By
#函数功能:滑动一次(滑动幅度:小0.5屏)!
#形参dict:String,表示滑动的方向。"up":上滑;"down":下滑。"left":左滑。"right":右滑!
def huadonOne(driver,dict="up"):
w = driver.get_window_size()["width"]
h = driver.get_window_size()["height"]
if dict=="up":
start_x = 0.5 * w
start_y = 0.75 * h
end_x = 0.5 * w
end_y = 0.25 * h
driver.swipe(start_x, start_y, end_x, end_y, duration=5000)
elif dict=="down":
start_x = 0.5 * w
start_y = 0.25 * h
end_x = 0.5 * w
end_y = 0.75* h
driver.swipe(start_x, start_y, end_x, end_y, duration=5000);
elif dict=="left":
start_x = 0.75 * w
start_y = 0.5 * h
end_x = 0.25 * w
end_y = 0.5 * h
driver.swipe(start_x, start_y, end_x, end_y, duration=5000);
elif dict=="right":
start_x = 0.25 * w
start_y = 0.5 * h
end_x = 0.75 * w
end_y = 0.5 * h
driver.swipe(start_x, start_y, end_x, end_y, duration=5000);
#函数功能:定位某元素,并操作该元素!
#形参loc:表示你要定位的元素的定位特征!
#形参dict:滑动方向!
#形参caozuoType:指定对该元素如何操作!
#形参data:如果是对该元素输入操作,data表示输入数据!
def caozuoEleNotScreen(driver,loc,dict="up",caozuoType="click",data=None):
while True:
try:
ele= WebDriverWait(driver, 10, 1).until(lambda x: x.find_element(loc[0], loc[1]))
if caozuoType=="click":
ele.click()
elif caozuoType=="input":
ele.send_keys(data)
break
except Exception:
# 上滑
huadonOne(driver, dict)
#返回元素loc的w和h
def getEleWAndH(driver,loc):
# 获取元素“展开后的下列列表”的bounds属性
ele=WebDriverWait(driver, 5, 1).until(lambda x: x.find_element(loc[0], loc[1]))
attr_bounds = ele.get_attribute("bounds") # "[[108,144][333,806]]"
a = attr_bounds.replace("[", "")
b = a.replace("]", ",")
c = b.split(",", 4)
x1 = int(c[0])
y1 = int(c[1])
x2 = int(c[2])
y2 = int(c[3])
w=x2-x1
h=y2-y1
return {"width":w,"height":h}
#函数功能:点击下列列表中的某选项!
def click_select(driver,loc_select,loc_option):
# 定位展开后的下列列表
ele_select= WebDriverWait(driver, 5, 1).until(lambda x:x.find_element(loc_select[0],loc_select[1]));
# 定位元素下列列表“电话类型”中的某选项,并点击
while True:
try:
ele_xuanxian_phoneTypeele = WebDriverWait(driver, 3, 1).until(
lambda x: x.find_element(loc_option[0],loc_option[1]))
ele_xuanxian_phoneTypeele.click()
break
except Exception:
# 上滑这个下列列表
# 获取元素“展开后的下列列表”的bounds属性
attr_bounds = ele_select.get_attribute("bounds") # "[[108,144][333,806]]"
a = attr_bounds.replace("[", "")
b = a.replace("]", ",")
c = b.split(",", 4)
x1 = int(c[0])
y1 = int(c[1])
x2 = int(c[2])
y2 = int(c[3])
w = x2 - x1
h = y2 - y1
# 获取下列列表中滑动的起点和终点
start_x = x1 + 0.5 * w
start_y = y1 + 0.75 * h
end_x = start_x
end_y = y1 + 0.25 * h
driver.swipe(start_x, start_y, end_x, end_y, duration=5000)