python封装app_Python+appinum封装基本的操作

importosfrom appium importwebdriverfrom time importsleep, strftimefrom selenium.webdriver.support.wait importWebDriverWaitfrom selenium.webdriver.support importexpected_conditions as EC,\

expected_conditionsfrom selenium.webdriver.common.by importByfrom selenium.common.exceptions importWebDriverException#from tdop_ui_test.lib.log import *

from lib.log import *

from lib2to3.tests.support importdriverfrom appium.webdriver.webdriver importWebDriver#from public.BaseOperate import getscreen

importGetDriver

u'''封装一些基础操作:滑动、截图、点击页面元素,输入内容等'''

classBaseOperate(object):def __init__(self,driver):

self.driver=driverdefback(self):'''返回键

:return:'''os.popen("adb shell input keyevent 4")defenter_key(self):'''输入enter键'''os.popen("adb shell input keyevent 66")defenter_key_getscreen(self):'''输入enter键并截屏'''os.popen("adb shell input keyevent 66")

self.getscreen()def find_toast(self, message,timeout=7, poll_frequency=0.01):'''判断toast信息'''

try:

element= WebDriverWait(self.driver, 10).until(

EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))printelementreturnTrueexcept:returnFalsedefgetscreen(self):

u"屏幕截图,保存截图到report\screenshot目录下"st=strftime("%Y-%m-%d_%H-%M-%S")#path=os.path.abspath(os.path.join(os.getcwd(), "../.."))

path = os.path.abspath(os.path.join(os.getcwd(), "..")) #获取父级路径的上一级目录路径

filename = path + "\\report\screenshot\%s.png" % st #修改截图文件的存放路径为相对路径

self.driver.get_screenshot_as_file(filename)printfilenamedefclick_by_id(self,ele_id):

u"根据ID点击"

try:

ClickElement= WebDriverWait(self.driver,timeout=15).until(EC.presence_of_element_located((By.ID,ele_id)),message=u'元素加载超时!')

ClickElement.click()exceptException as e:print u"页面元素:%s没有找到,程序错误或请求超时" %ele_id

self.getscreen()

log_error(u"未找到页面元素:%s"%ele_id)

self.driver.quit()defclick_by_class(self,ele_class):

u"根据class_name点击"

try:

ClickElement= WebDriverWait(self.driver,timeout=15).until(EC.presence_of_element_located((By.CLASS_NAME,ele_class)),message=u'元素加载超时!')

ClickElement.click()exceptException as e:print u"页面元素:%s没有找到,程序错误或请求超时" %ele_class

self.getscreen()

log_error(u"未找到页面元素:%s"%ele_class)

self.driver.quit()defclick_by_xpath(self,xp):

u"根据class_name点击"

try:

ClickElement=self.driver.find_element_by_xpath(xp)

ClickElement.click()exceptException as e:print u"页面元素:%s没有找到,程序错误或请求超时" %xp

self.getscreen()

log_error(u"未找到页面元素:%s"%xp)

self.driver.quit()def touch_tap(self,x,y,duration=50):

u"根据坐标点击元素"screen_width= self.driver.get_window_size()['width'] #获取当前屏幕的宽

screen_height = self.driver.get_window_size()['height'] #获取当前屏幕的高

a =(float(x)/screen_width)*screen_width

x1=int(a)

b= (float(y)/screen_height)*screen_height

y1=int(b)

self.driver.tap([(x1,y1),(x1,y1)],duration)defclick_by_text(self,ele_text):

self.driver.find_elements_by_android_uiautomator("new UiSelector().text(\"%s\")"%ele_text)[0].click()#def click_by_text(self,ele_text):#u"根据text点击"#try:#ClickElement = WebDriverWait(self.driver,timeout=15).until(EC.presence_of_element_located((By.NAME,ele_text)),message=u'元素加载超时!')#ClickElement.click()#except Exception as e:#print u"页面元素:%s没有找到,程序错误或请求超时" %ele_text#self.getscreen()#log_error(u"未找到页面元素:%s"%ele_text)#self.driver.quit()

definput_by_id(self,input_id,text):

u"Input,根据ID,input输入内容"

try:

InputElement= WebDriverWait(self.driver,timeout=20).until(EC.presence_of_element_located((By.ID,input_id)),message=u'元素加载超时!')

InputElement.click()

InputElement.clear()

InputElement.send_keys(str(text))exceptException as e:print u"页面元素:%s没有找到,程序错误或请求超时" %input_id

self.getscreen()

log_error(u"未找到页面元素:%s"%input_id)

self.driver.quit()definput_by_id_unclear(self,input_id,text):

u"Input,根据ID,input输入内容"

try:

InputElement= WebDriverWait(self.driver,timeout=20).until(EC.presence_of_element_located((By.ID,input_id)),message=u'元素加载超时!')

InputElement.send_keys(str(text))exceptException as e:print u"页面元素:%s没有找到,程序错误或请求超时" %input_id

self.getscreen()

log_error(u"未找到页面元素:%s"%input_id)

self.driver.quit()defget_text(self,ele):

u"根据元素ID,获取text"sleep(3)

source=self.driver.page_sourceif ele insource:

text_1=WebDriverWait(self.driver,timeout=15).until(EC.presence_of_element_located((By.ID,ele)),message=u'元素加载超时!').get_attribute('text')else:print u"页面元素:%s没有找到,程序错误或请求超时" %ele

self.getscreen()

log_error(ele)

self.driver.quit()returntext_1defget_text1(self,xp):

u"根据元素xpth,获取text"sleep(3)

source=self.driver.page_sourcetry:

text_1=WebDriverWait(self.driver,timeout=15).until(EC.presence_of_element_located((By.XPATH,xp)),message=u'元素加载超时!').get_attribute('text')exceptException as e:print u"页面元素:%s没有找到,程序错误或请求超时" %xp

self.getscreen()

log_error(xp)

self.driver.quit()returntext_1def find_item(self, ele,timeout = 3):'''用于检查页面是否存在某元素'''count=0try:while count

source=self.driver.page_sourceif ele insource:returnTrueelse:

count+= 1sleep(1)returnFalseexceptException as e:print u"页面内容获取失败,程序错误或请求超时"self.getscreen()

log_error(u"页面内容获取失败")def find_source(self): #方法已修改,待验证

'''获取页面的所有页面元素'''

try:

source=self.driver.page_sourceexceptException as e:print u"页面内容获取失败,程序错误或请求超时"self.getscreen()

log_error(u"页面内容获取失败")returnsourcedeffind_by_scroll(self, ele_text):'''滑屏查找指定元素的方法'''

try:

self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "'+ ele_text + '")')print u"滑屏查找的页面元素:%s已找到" %ele_textexceptException as e:print u"滑屏查找的页面元素:%s没有找到,程序错误或请求超时" %ele_text

log_error(u"滑屏未找到页面元素:%s" %ele_text)#self.getscreen()

defgetSize(self):

u"获取屏幕大小"x= self.driver.get_window_size()['width']

y= self.driver.get_window_size()['height']return(x, y)#屏幕向上滑动

def swipeUp(self,t = 600):

l=self.getSize()

x1= int(l[0] * 0.5) #x坐标

y1 = int(l[1] * 0.75) #起始y坐标

y2 = int(l[1] * 0.25) #终点y坐标

self.driver.swipe(x1, y1, x1, y2,t) #t 表示滑屏的时间,5代巴枪默认为600ms,7代巴枪需要根据实测调整参数

#屏幕向右滑动

def swipRight(self,t = 600):'''屏幕向右滑动'''l=self.getSize()

x1=int(l[0]*0.05)

y1=int(l[1]*0.5)

x2=int(l[0]*0.75)

self.driver.swipe(x1,y1,x2,y1,t)def swipeDown(self,t = 600):'''屏幕向下滑动'''l=self.getSize()

x1= int(l[0] * 0.5) #x坐标

y1 = int(l[1] * 0.25) #起始y坐标

y2 = int(l[1] * 0.75) #终点y坐标

self.driver.swipe(x1, y1, x1, y2,t)#屏幕向左滑动

def swipLeft(self,t = 600):'''屏幕向左滑动'''l=self.getSize()

x1= int(l[0] * 0.75)

y1= int(l[1] * 0.5)

x2= int(l[0] * 0.05)

self.driver.swipe(x1, y1, x2, y1, t)#点屏幕左侧向上滑动

def swipeUp_left(self, t=1200):

l=self.getSize()

x1= int(l[0] * 0.15) #x坐标

y1 = int(l[1] * 0.75) #起始y坐标

y2 = int(l[1] * 0.50) #终点y坐标

self.driver.swipe(x1, y1, x1, y2, t) #t 表示滑屏的时间,5代巴枪默认为600ms,7代巴枪需要根据实测调整参数

#向下滑屏,滑屏区间的坐标位可调

def swipeDown_Adjustable(self,X1 = 0.5 ,Y1 = 0.5 ,Y2 = 0.8 ,t = 600): #X为横坐标的系数,Y为纵坐标的系数,t为滑屏时间,单位为ms

'''屏幕向下滑动'''l=self.getSize()

x1= int(l[0] * X1) #x坐标

y1 = int(l[1] * Y1) #起始y坐标

y2 = int(l[1] * Y2) #终点y坐标

self.driver.swipe(x1, y1, x1, y2,t)#向上滑屏,滑屏区间设置为坐标位可调

def swipeUp_Adjustable(self, X1 = 0.5, Y1 = 0.5, Y2 = 0.25, t = 600): #X为横坐标的系数,Y为纵坐标的系数,t为滑屏时间,单位为ms

'''屏幕向上滑动'''l=self.getSize()

x1= int(l[0] * X1) #x坐标

y1 = int(l[1] * Y1) #起始y坐标

y2 = int(l[1] * Y2) #终点y坐标

self.driver.swipe(x1, y1, x1, y2, t) #t 表示滑屏的时间,5代巴枪默认为600ms

defplog(self,text):printtext

log_info(text)defpwlog(self,text):printtext

log_warn(text)def long_press_text(self,ele_text,duration = 2500):'''根据text元素定位来长按操作'''

try:

ClickElement= WebDriverWait(self.driver,timeout=10).until(EC.presence_of_element_located((By.NAME,ele_text)),message=u'元素加载超时!')

elx= ClickElement.location.get('x')

ely= ClickElement.location.get('y')

self.driver.swipe(elx, ely, elx, ely, duration)exceptException as e:print u"页面元素:%s没有找到,程序错误或请求超时" %ele_text

self.getscreen()

log_error(u"未找到页面元素:%s"%ele_text)deffind_toast1(self,message):try:

element= WebDriverWait(self.driver,timeout=3).until(expected_conditions.presence_of_element_located((By.PARTIAL_LINK_TEXT, message)))returnTrueexcept:returnFalseif __name__ == '__main__':

st=strftime("%Y-%m-%d_%H-%M-%S")

path=os.path.abspath(os.path.dirname(os.getcwd())) #获取当前文件父级路径

f=path+"\\report\screenshot\%s.png"%st

filename=r"\\".join(f.split("\\"))

driver= GetDriver.appdriver() #用于测试图片截图输出

test_pic_shot =BaseOperate(driver)#test_pic_shot.getscreen()

#print filename

#x=int(test_pic_shot.getSize()[0]*0.5)

#y= int(test_pic_shot.getSize()[1]*0.75)

#print x,y

#print f

a = test_pic_shot.find_toast(u"查无此件")printa

driver.quit()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值