框架综合实践(2)-公共模块Common的封装
公共类的思维导图如下:
common_fun.py用于封装公共模块,例如滑动引导页的模块、检测应用是否进入首页等
1.进入应用首页
通过判断首页的某个元素存在,来校验是否成功进入应用首页
2.定义获取屏幕大小函数
3.定义向左滑动的函数
4.定义获取系统时间的函数
5.定义屏幕截图函数
说明:
1.获取当前脚本所在的文件夹:t_path=os.path.dirname(__file__)
2.获取当前脚本所在文件夹的上一个文件夹:os.path.dirname(os.path.dirname(__file__))
3.进入同一目录下的另外一个文件夹:
os.path.dirname(os.path.dirname(__file__))+'/screenshots/%s_%s.png' %(module,time),其中%s_%s.png %(modele,time)表示以传入的模块和当前系统时间命名截图文件
完整的脚本:common_fun.py
前提:已有yaml、log.conf、desired_caps.py、baseView.py文件;common_fun.py继承baseView.py
#!urs/bin/python
#!_*_ coding:UTF-8 _*_
from baseView.baseView import BaseView
from common.desired_caps import appium_desired
from selenium.common.exceptions import NoSuchElementException
import logging
from selenium.webdriver.common.by import By
import time,os
#定义类
class Common(BaseView):
image_button = (By.ID, "com.mydream.wifi:id/tvAsk")
#定义判断已进入应用首页的函数
def check_imagebutton(self):
logging.info("========检测元素========")
try:
element=self.driver.find_elements(*self.image_button)
except NoSuchElementException:
logging.info("========元素不存在========")
else:
# element.click()
logging.info("========元素存在========")
#定义获取屏幕的方法
def get_windows_size(self):
logging.info("========获取屏幕大小========")
x = self.driver.get_window_size()['height'] # 获取屏幕的高度
y = self.driver.get_window_size()['width'] # 获取屏幕的宽度
return (x, y)
#定义向左滑动的方法
def swipleft(self):
logging.info("========滑动引导页========")
gs = self.get_windows_size()
x1 = int(gs[0] * 0.75)
y1 = int(gs[1] * 0.5)
x2 = int(gs[0] * 0.25)
self.driver.swipe(x1, y1, x2, y1, 1000)
#返回以可读字符串表示的当地时间,并设置好返回的格式
def getTime(self):
self.now = time.strftime("%Y-%m-%d %H_%M_%S")
return self.now
#定义屏幕截图的方法
def getScreenShot(self,module):
time=self.getTime()
t_path=os.path.dirname(__file__)
image_file=os.path.dirname(os.path.dirname(__file__))+'/screenshots/%s_%s.png' %(module,time)
logging.info('get %s screenshot' %module)
self.driver.get_screenshot_as_file(image_file)
logging.info("=======首页截图成功=======")
if __name__ == '__main__':
#定义driver初始化
driver=appium_desired()
com=Common(driver)
com.check_imagebutton()
#com.swipleft()
#调用截图,传入具体的模块名称
com.getScreenShot("star APP")
运行后的日志如下: