appium自动化测试框架系列之基类以及公共类的封装
appium自动化测试框架系列之基类以及公共类的封装基于对设计框架时的分析,主要的实现的功能时对移动端中对应场景通过代码自动完成指定的操作,首页以实现基本的操作的类作为基类,放置与baseView文件夹。基类代码如下:#coding=utf-8
# 完成基类的封装
class BaseView(object):
def __init__(self, driver):
self.driver = driver
#寻找元素
def find_element(self, *args,**kwargs):
return self.driver.find_element(*args, **kwargs)
#寻找元素列表
def find_elements(self, *args, **kwargs):
return self.driver.find_elements(*args, **kwargs)
#获取屏幕尺寸
def get_window_size(self):
return self.driver.get_window_size()
#滑动
def swipe(self, start_x, start_y, end_x, end_y, duration):
return self.driver.swipe(start_x, start_y, end_x, end_y, duration)而公共类则作为实现常用的方法的封装而成,放置于common文件夹下,包含了以下3个文件:1.启动脚本:desire_caps.pyimport json
from appium import webdriver
import os
import logging.config
CON_LOG = r'../config/log.conf'
logging.config.fileConfig(CON_LOG)
logging = logging.getLogger()
def appium_desired():
with open("../config/desire_caps", "r") as file:
data = json.load(file)
desired_caps = {}
# print(data['platformName'])
desired_caps['platformName'] = data['platformName']
desired_caps['deviceName'] = data['deviceName']
desired_caps['deviceVersion'] = data['deviceVersion']
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
App_Path = os.path.join(BASE_DIR, 'app', data['appName'])
desired_caps['app'] = App_Path
desired_caps['noReset'] = data['noReset']
desired_caps['unicodeKeyboard'] = data['unicodeKeyboard']
desired_caps['resetKeyboard'] = data['resetKeyboard']
desired_caps['appPackage'] = data['appPackage']
desired_caps['appActivity'] = data['appActivity']
logging.info('start run app...')
driver = webdriver.Remote("http://%s:%s/wd/hub" % (data['host'], data['port']), desired_caps)
driver.implicitly_wait(5)
return driver
if __name__ == '__main__':
appium_desired()
2.公用方法类:common_fun.pyfrom baseViews.baseView import BaseView
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from common.desire_caps import *
import time
import os
import csv
#完成公用的类及其方法
class Common(BaseView):
def set_Btn(self, match, value):
Btn = (match, value)
return Btn
def check_updateBtn(self):
logging.info("===========check updateBtn=======")
btn_name = 'android:id/button2'
update_btn = self.set_Btn(By.ID, btn_name)
try:
element = self.find_element(update_btn, update_btn)
except NoSuchElementException:
logging.info("===not found update button====")
else:
logging.info("===check update button====")
element.click()
def check_skipBtn(self):
logging.info('====check skipBtn====')
btn_name = 'com.tal.kaoyan:id/tv_skip'
btn = self.set_Btn(By.ID, btn_name)
try:
element = self.find_element(btn, btn)
except NoSuchElementException:
logging.info("===not found skip button====")
else:
logging.info("===check skip button====")
element.click()
def get_screenSize(self):
'''
获取屏幕尺寸
:return:
'''
x = self.get_window_size()['width']
y = self.get_window_size()['height']
return (x, y)
def swipeLeft(self):
logging.info('swipeLeft')
l = self.get_screenSize()
y1 = int(l * 0.5)
x1 = int(l * 0.95)
x2 = int(l * 0.25)
self.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()
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)
def check_market_ad(self):
'''检测登录或者注册之后的界面浮窗广告'''
logging.info('=======check_market_ad=============')
self.wemedia = self.set_Btn(By.ID, 'com.tal.kaoyan:id/view_wemedia_cacel')
try:
element = self.driver.find_element(*self.wemedia)
except NoSuchElementException:
pass
else:
logging.info('close market ad')
element.click()
def get_csv_data(self, csv_file, line):
'''
获取csv文件指定行的数据
:param csv_file: csv文件路径
:param line: 数据行数
:return:
'''
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
csv_file_path = os.path.join(BASE_DIR, 'data', csv_file)
with open(csv_file_path, 'r', encoding='utf-8-sig') as file:
reader = csv.reader(file)
for index, row in enumerate(reader, 1):
if index == line:
return row
if __name__ == '__main__':
driver = appium_desired()
com = Common(driver)
com.check_updateBtn()
com.check_skipBtn()
com.swipeLeft()
com.swipeLeft()
com.getScreenShot('startapp')3.测试用例公用类:myunit.pyimport unittest
from common.desire_caps import *
from time import sleep
class StartEnd(unittest.TestCase):
def setUp(self):
logging.info('===setup====')
self.driver = appium_desired()
def tearDown(self):
logging.info('====tearDown====')
sleep(5)
self.driver.close_app()当完成基类以及公用类的封装,下一步我们继续实现业务逻辑的脚本
页:
[1]