9 Appium关键字模型实战

关键字模型和PO模型的根本思想都是面向对象的思想,简单理解为就是封装,关键字模型的封装更为彻底,关键字模型搭建好之后,基本不需要写代码了,直接写用例的配置文件就可以了。

关键字模型的搭建不需要重新搭建,在    8.Appium PO模型实战 https://blog.csdn.net/MATLAB_matlab/article/details/105833808)      的基础上搭建即可。

详情请点击 https://study.163.com/course/introduction/1209833813.htm 查看

在PO模型的基础之上,新建一个auto_case文件夹,

里面的case文件下存放用例脚本,一个用例就是一个py文件;logs存放日志;report存放报告、Screenshots存放截图、case_main.py是关键字模型执行入口。

用例文件示例:

test_fabu.py

#测试点击发布按钮是否能够进入发布页面
gohome|
click|shouye|fabu
sleep|5
ScreenShot|这个是第一条用例的截图
check|fabupage|caogaoxiang

用例脚本以 | 分割方法名称和参数

gohome |在代码中封装为返回主页

click | shouye|fabu   click是封装的点击方法,后面的是PO模型中page.ini中的参数,通过这两个参数去page.ini中找到该元素

sleep|5  是封装的等待方法, 等待5s

ScreenShot|这个是第一条用例的截图   ScreenShot是封装的截图方法,后面的参数是截图说明,会把该说明放到截图的名称上

check|fabupage|caogaoxiang   check是封装的检查函数,检查元素是否存在,后面的两个参数就是在page.ini中定位元素的

 

case_main.py

import os
import logging
import traceback
import sys
import time
import re
from PO.po import pageobject
import HTMLTestRunner
import unittest

po=pageobject()
from public import file

import time
from datetime import datetime

PRJPATH=os.getcwd()

class APP():
    def __init__(self,path_name):
        if not os.path.exists(path_name):
            logging.error("path name is error")
            exit(1)
        #elif os.path.isdir(pathname):
        if os.path.isdir(path_name):
            files=file.get_files(path_name)
            self.filename=file.get_new_file()
            run_suit(self,files,self.filename)
        else:
            text="please enter a vaild path "+path_name
            logging.error(text)
        return




def run_suit(app,cases,html_file=None):
    logging.info("run_suit")
    suite=unittest.TestSuite()
    for i in cases:
        suite.addTest(CaseTest(app,i))

    if html_file is None:
        html_file=os.getcwd()+'/report.html'
    fp=open(html_file,'wb')
    title='Test Report'
    HTMLTestRunner.HTMLTestRunner(stream=fp,title=title).run(suite)


class CaseTest(unittest.TestCase):
    def __init__(self,app,case):
        self.app=app
        self.case=case
        case_name=os.path.basename(case)
        cn=case_name.replace('.','_')
        setattr(self,cn,self.run_test)
        unittest.TestCase.__init__(self,cn)

    @classmethod
    def setUpClass(cls):
        logging.info('This is setUpClass')
    def setUp(self):
        logging.info("Set up")


    def run_test(self): #执行用例的主要入口
        logging.info("run test "+self.case)
        ret,key=self.replay(self.case)
        if not ret:
            raise Exception('Error:{0}'.format(key))



    def replay(self,case):
        logging.info("start case: "+case)
        #path=file.get_new_file(case)
        path=file.get_new_dir(case)
        proc,filename=file.start_logcat(path)
        ret=True
        key=''
        steps=file.read_case(case)
        for s in steps:
            if s[0]=='gohome':
                logging.info("gohome方法")
                po.go_home()

            elif s[0]=='click':
                logging.info("这个是click方法")
                element=po.get_element.get_element(s[1],s[2])
                time.sleep(3)
                element.click()
                time.sleep(2)

            elif s[0]=='sleep':
                logging.info("这个是sleep方法")
                time.sleep(int(s[1]))

            elif s[0]=='check':
                logging.info("这个是check方法")
                element=po.get_element.get_element(s[1],s[2])
                if element is None:
                    ret=False
                    key="结果验证失败"
                    logging.error("用例执行失败")
            elif s[0]=='ScreenShot':
                logging.info("开始截图")
                case_name=case.split('\\')[-1].split('.')[0]  #把用例的名称取出
                step_name=str(s[0])
                text1=str(s[1])
                text=case_name+"_"+step_name+"_"+text1
                dt = datetime.now()
                now_time = dt.strftime("%Y_%m_%d_%H_%M_%S")
                imgae_file=PRJPATH+"\\Screenshots\\%s_%s"%(now_time,text)
                logging.info("截图路径:")
                logging.info(imgae_file)
                po.driver.get_screenshot_as_file(imgae_file)


            else:
                logging.error("没有找到用例中的方法,请检查,封装该方法")
                ret=False
                key="没有找到用例中的方法,请检查,封装该方法"

        file.end_logcat(proc)
        return ret,key



if __name__=="__main__":
    pathname=os.path.join(file.PRJPATH,'case')
    logging.info(pathname)
    app=APP(pathname)

详情请点击 https://study.163.com/course/introduction/1209833813.htm 查看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术提高效率

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值