Python2+appinum基础操作封装

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:29   1942   0

#-*- coding:utf-8 -*-
#author='Shichao-Dong'

import os
from appium import webdriver
from time import sleep, strftime
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC,\
expected_conditions
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
#from tdop_ui_test.lib.log import *
from lib.log import *
from lib2to3.tests.support import driver
from appium.webdriver.webdriver import WebDriver
# from public.BaseOperate import getscreen
import GetDriver
from distutils.cmd import Command


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

#-*- coding:utf-8 -*-
#author='Shichao-Dong'

import os
from appium import webdriver
from time import sleep, strftime
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC,\
expected_conditions
from selenium.webdriver.common.by import By
from selenium.common.exceptions import WebDriverException
#from tdop_ui_test.lib.log import *
from lib.log import *
from lib2to3.tests.support import driver
from appium.webdriver.webdriver import WebDriver
# from public.BaseOperate import getscreen
import GetDriver


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

class BaseOperate(object):
def __init__(self,driver):
self.driver = driver


def back(self):
'''
返回键
:return:
'''
os.popen("adb shell input keyevent 4")

def enter_key(self):
'''输入enter键'''
os.popen("adb shell input keyevent 66")

def enter_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)))
print element
return True
except:
return False
def getscreen(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)
print filename


def click_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()
except Exception as e:
print u"页面元素:%s没有找到,程序错误或请求超时" %ele_id
self.getscreen()
log_error(u"未找到页面元素:%s"%ele_id)
self.driver.quit()


def click_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()
except Exception as e:
print u"页面元素:%s没有找到,程序错误或请求超时" %ele_class
self.getscreen()
log_error(u"未找到页面元素:%s"%ele_class)
self.driver.quit()


def click_by_xpath(self,xp):
u"根据class_name点击"
try:
ClickElement = self.driver.find_element_by_xpath(xp)
ClickElement.click()
except Exception 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)





def click_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没有找到,程序错询

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP