pyapp的封装和内置函数

from lib.core.pyse import Pyse
from appium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from appium.webdriver.common.mobileby import MobileBy

class PyApp(Pyse):
def __init__(self,driver):
# 这才是真正的driver
self.d = driver
# 这里的webdriver.Remote()只为了单纯的提示文案,用于封装pyapp才使用,当pyapp写完要注释掉
self.d = webdriver.Remote()

def get_element(self, css):
'''
Judge element positioning way, and returns the element.
'''
if "=>" not in css:
raise NameError("Positioning syntax errors, lack of '=>'.")

by = css.split("=>")[0]
value = css.split("=>")[1]

if by == "id":
element = self.d.find_element_by_id(value)
elif by == "name":
element = self.d.find_element_by_name(value)
elif by == "class":
element = self.d.find_element_by_class_name(value)
elif by == "link_text":
element = self.d.find_element_by_link_text(value)
elif by == "xpath":
element = self.d.find_element_by_xpath(value)
elif by == "css":
element = self.d.find_element_by_css_selector(value)
elif by == "android":
element = self.d.find_element_by_android_uiautomator(value)
elif by == "desc":
element = self.d.find_element_by_accessibility_id(value)
elif by == "ios":
pass
else:
raise NameError(
"Please enter the correct targeting elements,'id','name','class','link_text','xpath','css','android','desc'.")
return element

def get_elements(self, css):
'''
Judge element positioning way, and returns the element.
'''
if "=>" not in css:
raise NameError("Positioning syntax errors, lack of '=>'.")

by = css.split("=>")[0]
value = css.split("=>")[1]

if by == "id":
element = self.d.find_elements_by_id(value)
elif by == "name":
element = self.d.find_elements_by_name(value)
elif by == "class":
element = self.d.find_elements_by_class_name(value)
elif by == "link_text":
element = self.d.find_elements_by_link_text(value)
elif by == "xpath":
element = self.d.find_elements_by_xpath(value)
elif by == "css":
element = self.d.find_elements_by_css_selector(value)
elif by == "android":
element = self.d.find_elements_by_android_uiautomator(value)
elif by == "desc":
element = self.d.find_elements_by_accessibility_id(value)
elif by == "ios":
pass
else:
raise NameError(
"Please enter the correct targeting elements,'id','name','class','link_text','xpath','css','android','desc'.")
return element

def element_wait(self, css, secs=5):
'''
Waiting for an element to display.

Usage:
driver.element_wait("css=>#el",10)
'''
if "=>" not in css:
raise NameError("Positioning syntax errors, lack of '=>'.")

by = css.split("=>")[0]
value = css.split("=>")[1]

if by == "id":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.ID, value)))
elif by == "name":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.NAME, value)))
elif by == "class":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.CLASS_NAME, value)))
elif by == "link_text":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.LINK_TEXT, value)))
elif by == "xpath":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.XPATH, value)))
elif by == "css":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.CSS_SELECTOR, value)))
elif by == "android":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.ANDROID_UIAUTOMATOR, value)))
elif by == "desc":
WebDriverWait(self.d, secs, 1).until(EC.presence_of_element_located((MobileBy.ACCESSIBILITY_ID, value)))
else:
raise NameError(
"Please enter the correct targeting elements,'id','name','class','link_text','xpath','css','android','desc'.")

def swipe_up(self,time=300): # 向上滑
size = self.get_windows_size
x = size.get('width')
y = size.get('height')
start_x = x * 0.5
start_y = y * 0.8
end_x = x * 0.5
end_y = y * 0.2
self.d.swipe(start_x,start_y,end_x,end_y,time)

def swipe_down(self,time=300): # 向下滑
size = self.get_windows_size
x = size.get('width')
y = size.get('height')
start_x = x * 0.5
start_y = y * 0.2
end_x = x * 0.5
end_y = y * 0.8
self.d.swipe(start_x,start_y,end_x,end_y,time)

def swipe_left(self,time=300): # 向左滑
size = self.get_windows_size
x = size.get('width')
y = size.get('height')
start_x = x * 0.8
start_y = y * 0.5
end_x = x * 0.2
end_y = y * 0.5
self.d.swipe(start_x,start_y,end_x,end_y,time)

def swipe_right(self,time=300): # 向右滑
size = self.get_windows_size
x = size.get('width')
y = size.get('height')
start_x = x * 0.2
start_y = y * 0.5
end_x = x * 0.8
end_y = y * 0.5
self.d.swipe(start_x,start_y,end_x,end_y,time)

# 点击code,回车之类的code
def press_keycode(self,code):
self.d.press_keycode(code)

# 是否安装了app,返回True or False
def is_install_app(self,package):
return self.d.is_app_installed(package)

# 关闭app
def close_app(self):
self.d.close_app()

# 退出驱动
def quit(self):
self.d.quit()

# 安装app
def install_app(self,path):
self.d.install_app(path)

# 卸载app
def remove_app(self,package):
self.d.remove_app(package)

# 返回app当中全类型[原生,h5]
@property
def contexts(self):
return self.d.contexts

# 返回当前的context
@property
def context(self):
return self.d.current_context

def native_app(self):
# 获取全部contexts
app = self.contexts
if len(app) > 1:
self.d.switch_to.context(app[0])

def hybrid_app(self):
# 获取全部contexts
app = self.contexts
if len(app) == 2:
self.d.switch_to.context(app[-1])

def open_webview_or_native(self):
# 获取全部contexts
app = self.contexts
for context in app:
# self.context(当前的,调用上面的方法) 和 列表中的context进行循环对比,如果不相等,则切换到新的context
if self.context != context:
self.d.switch_to.context(context)

# 相当于noreset=False,类似于重新安装app
def reset(self):
self.d.reset()

# 如果sendkeys无法向文本框发送内容
def set_value(self,element,value):
self.d.set_value(element,value)

队列
import queue
q = queue.Queue()
q.put('appium1') # 往队列里面放obj
q.put('appium2')
print(q.qsize()) # q.qsize()相当于队列长度
print(q.get()) # 从队列里面取obj
print(q.get())

yaml,通过它来写我们的配置文件,文件名以.yml为后缀,pip install pyyaml,import yaml,appConfig.yml的内容如下:

# 字典是通过:,:后面有一个空格,如果字典里还有嵌套,换行后再tab一下
devices_type: Android
tester:
noReset: True
unicodeKeyboard: True
appPackage: com.android.browser
appActivity: .BrowserActivity
resetKeyboard: True
devices:
android:
# 列表通过 -,-后面有一个空格,层级通过缩进,多个设备,这种格式比较方便
- platformName: Android
platformVersion: 4.2.2
ip: 127.0.0.1
port: 62001
devicesName: 69cb759b

- platformName: Android
platformVersion: 4.2.2
ip: 127.0.0.1
port: 62001
devicesName: 69cb759b

# subprocess模块,基于进程的,一个程序运行完了,另一个进程继续运行,不影响
subprocess.getoutput(cmd),接收字符串格式的命令,执行命令并返回执行结果
subprocess.Popen(args,stdin=None, stdout=None, stderr=None, shell=False)
args:要执行的shell命令,可以是字符串,也可以是命令各个参数组成的序列,当该参数的值是一个字符串时,该命令的解释过程是与平台相关的,因此通常建议将args参数作为一个序列传递
stdin, stdout, stderr: 分别表示程序标准输入、输出、错误句柄
shell: 该参数用于标识是否使用shell作为要执行的程序,如果shell值为True,则建议将args参数作为一个字符串传递而不要作为一个序列传递

posted @ 2018-08-25 17:20  laosun0204  阅读(478)  评论(0编辑  收藏  举报