1.基本框架
所有公司的框架大不相同,上图只是基本框架
2.yaml语言
YAML 是一种简洁的非标记语言。YAML以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁易读。
由于实现简单,解析成本很低,YAML特别适合在脚本语言中使用。列一下现有的语言实现:Ruby,Java,Perl,Python,PHP,JavaScript等。
YAML 是专门用来写配置文件的语言,非常简洁和强大
yaml文件
platformName: Android
platformVersion: 5.1.1
deviceName: 127.0.0.1:62001
appPackage: com.tal.kaoyan
appActivity: .ui.activity.SplashActivity
noReset: True
ip: 127.0.0.1
port: 4723
logger.conf文件
[loggers]
keys=root,infoLogger,errorLogger
[logger_root]
lever=DEBUG
handlers=consoleHandler
[logger_infoLogger]
handlers=fileHandler,consoleHandler
qualname=infoLogger
propagate=0
[logger_errorLogger]
handlers=fileHandler,consoleHandler
qualname=errorLogger
propagate=0
[handlers]
keys=fileHandler,consoleHandler
[handler_consoleHandler]
class=StreamHandler
level=ERROR
fromatter=form01
args=(sys.stderr,)
[handler_fileHandler]
class=FileHandler
level=INFO
formatter=form02
args=('../logs/testlog.log','a')
[formatters]
keys=form01,form02
[formatter_form01]
form01=%(asctime)s - %(levelname)s -%(pathname)s-%(lineno)d - %(message)s
[formatter_form02]
form02=%(asctime)s - %(levelname)s -%(pathname)s
调用yaml配置
from appium import webdriver
from time import sleep
from appium.webdriver.common.touch_action import TouchAction
import yaml
import os
import logging
import logging.config
def appium_desired():
filelpath=os.path.dirname(os.path.dirname(__file__))
config_file=open(filelpath+'/config/logger.conf',encoding='utf-8')
logging.config.fileConfig(config_file)
logger=logging.getLogger('errorLogger')
logger.error(12344566)
with open(filelpath+'/config/desired_caps.yaml','r',encoding='utf-8') as yaml_file:
data=yaml.load(yaml_file)
caps = {}
caps["platformName"] = data['platformName']
caps["platformVersion"] = data['platformVersion']
caps["deviceName"] = data['deviceName']
caps["appPackage"] = data['appPackage']
caps["appActivity"] = data['appActivity']
caps["noReset"]=data["noReset"]
driver=webdriver.Remote('http://'+str(data["ip"])+":"+str(data["port"])+"/wd/hub",caps)
return driver,logger
if __name__=='__main__':
appium_desired()
common_fun.py公共方法类
from baseView.baseView import BaseView
from selenium.webdriver.common.by import By
import os
import time
class Common(BaseView):
skiptBtn=(By.ID,'com.tal.kaoyan:id/tv_skip')
cancelBtn=(By.ID,'android:id/button2')
def check_cancelBtn(self):
try:
cancelBtn=self.ele_is_visibility(self.cancelBtn)
except:
self.logger.info('没有取消升级按钮')
else:
cancelBtn.click()
def check_skiptBtn(self):
try:
skiptBtn=self.ele_is_visibility(self.cancelBtn)
except:
self.logger.info('没有跳过按钮')
else:
skiptBtn.click()
def get_size(self):
x = self.driver.get_window_size()['width']
y = self.driver.get_window_size()['height']
return x, y
def swipeLeft(self):
size = self.get_size()
x1 = int(size[0] * 0.9)
y = int(size[1] * 0.5)
x2 = int(size[0] * 0.2)
self.driver.swipe(x1, y, x2, y, 1000)
def getTime(self):
return time.strftime("%Y-%m-%d %H_%M_%S")
def getScrennShot(self,text):
time=self.getTime()
image_file=os.path.dirname(os.path.dirname(__file__))+'/screenshots/%s_%s.png' %(text,time)
self.logger.info('get %s screenshot' %text)
self.driver.get_screenshot_as_file(image_file)
if __name__=='__main__':
common=Common()
common.check_cancelBtn()
common.check_skiptBtn()
common.getScrennShot('登录页面截图')
报告模块
from appium import webdriver
from time import sleep
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from appium.webdriver.common.touch_action import TouchAction
from HTMLTestRunnerCN import HTMLTestRunner
import unittest
import warnings
class TestSettings(unittest.TestCase):
def setUp(self):
# 忽略警告
warnings.simplefilter("ignore",ResourceWarning)
caps = {}
caps["platformName"] = "Android"
caps["platformVersion"] = "5.1.1"
caps["deviceName"] = "127.0.0.1:62001"
caps["appPackage"] = "com.android.settings"
caps["appActivity"] = ".Settings"
caps["notReset"]=True
self.driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',caps)
self.driver.implicitly_wait(10)
def test_settings(self):
def get_size():
x=self.driver.get_window_size()['width']
y=self.driver.get_window_size()['height']
return x,y
def swipeUP():
size = get_size()
x=int(size[0]*0.5)
y1 = int(size[1] * 0.9)
y2 = int(size[1] * 0.3)
self.driver.swipe(x,y1,x,y2,1000)
while True:
try:
# 定位安全
ele=self.driver.find_element_by_xpath('//android.widget.TextView[@text=\"安全\"]')
except:
# 向上滑屏找到‘安全’
swipeUP()
else:
sleep(2)
ele.click()
break
# 点击图案,进入九宫格界面
ele=WebDriverWait(self.driver,10).until(EC.presence_of_element_located((By.ID,'android:id/summary')))
ele.click()
sleep(2)
x,y=get_size()
TouchAction(self.driver).press(x=x*0.16,y=y*0.35).wait(1000)\
.move_to(x=x*0.5,y=y*0.35).wait(1000)\
.move_to(x=x*0.8,y=y*0.53).wait(1000)\
.move_to(x=x*0.8,y=y*0.72).wait(1000).release().perform()
def tearDown(self):
self.driver.quit()
if __name__=='__main__':
# unittest.main()
suite=unittest.TestSuite()
suite.addTest(TestSettings("test_settings"))
fp=open('report.html','wb')
runner=HTMLTestRunner(stream=fp,title="测试报告",description='测试用例执行结果')
runner.run(suite)
fp.close()
baseView.py
from common.desired_caps import appium_desired
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class BaseView(object):
def __init__(self):
self.driver,self.logger=appium_desired()
self.wait=WebDriverWait(self.driver,10)
# 元素是否可见(单个元素)
def ele_is_visibility(self, eletuple):
try:
ele = self.wait.until(EC.visibility_of_element_located(eletuple))
return ele
except:
self.logger.error(u'ele_is_visibility:未找到元素 %s' % eletuple)
# 返回一组数据(list)
def get_eles(self, eletuple):
try:
ele = self.wait.until(EC.visibility_of_all_elements_located(eletuple))
return ele
except:
self.logger.error(u'ele_is_presence:未找到元素 %s ' % eletuple)
程序入口
import unittest
from HTMLTestRunnerCN import HTMLTestRunner
import os
path=os.path.dirname(os.path.dirname(__file__))+'/test_case'
discover=unittest.defaultTestLoader.discover(path,pattern='test*.py')
if __name__=="__main__":
fp = open('report.html', 'wb')
runner = HTMLTestRunner(stream=fp, title="测试报告", description='测试用例执行结果')
runner.run(discover)
fp.close()