Appium之HTMLtestRunner_PY3+Unittest简单框架示例

简介: 先看下生成的测试报告:测试报告一、代码结构分析代码结构二、配置文件driver_configure.py:#coding:UTF-8"""@@Time:2018.

先看下生成的测试报告:

img_e9cb292a2d22fdba39138373e8fada43.png
测试报告

一、代码结构分析

img_cf3c8108a11bf0b90797936640f8109f.png
代码结构

二、配置文件

driver_configure.py:

#coding:UTF-8

"""

@@Time:2018.06.05  15:30

@Author:keitwo

@E-mail:

@FinleName:

"""

from appium import webdriver

class driver_configure():

    def get_driver(self):

        """"获取driver"""

        try:

            self.desired_caps = {}

            self.desired_caps['platformName'] = 'Android'  # 设备系统

            self.desired_caps['platformVersion'] = '4.4.2'  # 设备系统版本

            self.desired_caps['deviceName'] = '127.0.0.1:62001'  # 设备名称

            self.desired_caps['appPackage'] = 'com.jianshu.haruki'  # 测试app包名

            self.desired_caps['appActivity'] = 'com.baiji.jianshu.ui.splash.SplashScreenActivity'  # 测试appActivity

            self.desired_caps['noReset'] = 'true'

            # desired_caps['app'] = 'D:\\work\\appium\\jianshu.apk',

            self.driver = webdriver.Remote('http://localhost:4723/wd/hub', self.desired_caps)  # 启动app

            return self.driver

        except Exception as e:

            raise e

三、测试用例

test_login.py:

#coding:UTF-8

"""

@@Time:2018.06.05  15:30

@Author:keitwo

@E-mail:

@FinleName:

"""

import time,unittest

from appium import webdriver

from appium_test.config import driver_configure

index = 1  #和截图有关

class loginTest(unittest.TestCase):

    @classmethod

    def setUpClass(cls):

        # desired_caps = {}

        # desired_caps['platformName'] = 'Android'  # 设备系统

        # desired_caps['platformVersion'] = '4.4.2'  # 设备系统版本

        # desired_caps['deviceName'] = '127.0.0.1:62001'  # 设备名称

        # desired_caps['appPackage'] = 'com.jianshu.haruki'  # 测试app包名

        # desired_caps['appActivity'] = 'com.baiji.jianshu.ui.splash.SplashScreenActivity'  # 测试appActivity

        # desired_caps['noReset'] = 'true'

        # #desired_caps['app'] = 'D:\\work\\appium\\jianshu.apk',

        # cls.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)  # 启动app

        dconfigur = driver_configure.driver_configure()

        cls.driver = dconfigur.get_driver()

    @classmethod

    def tearDownClass(cls):

        global index

        cls.screenshot(index)

        index += 1

        cls.driver.quit()

    png_file = "D:\\work\\appium_test\\report\\"  # 图片存放地址,这个地址要想创建好

    def screenshot(self, index):  # 需要写这个方法才能实现截图

        #timestr = time.strftime('%Y%m%d', time.localtime(time.time()))  # 精确到秒会无法截图,要和htmltestrunner.py文件格式一致

        timestr = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))

        img_name = timestr + '_' + str(index) + '.png'  # 图片以时间+第几次截图命名

        self.driver.get_screenshot_as_file('%s%s' % (self.png_file, img_name))  # 图片保存在定义路径中

        return img_name

    def test_login(self):

        """登录简书APP-测试用例"""

        driver = self.driver

        # 休眠五秒等待页面加载完成

        time.sleep(3)

        driver.find_element_by_id("com.jianshu.haruki:id/tab_more").click()

        time.sleep(2)

        driver.find_element_by_id("com.jianshu.haruki:id/item_user").click()

        time.sleep(3)

        self.screenshot(index)

        driver.find_element_by_id("com.jianshu.haruki:id/et_account").send_keys("xxxxxxxxx")

        time.sleep(3)

        driver.find_element_by_id("com.jianshu.haruki:id/et_password").send_keys("xxxxxx")

        time.sleep(3)

        driver.find_element_by_id("com.jianshu.haruki:id/btn_login").click()

        time.sleep(2)

        name = driver.find_element_by_id('com.jianshu.haruki:id/text_user_name').text

        time.sleep(3)

        self.screenshot(index)

        try:  # 添加断言,若昵称不正确,则打印错误信息

            assert 'keitwo' in name

            print('loginUser is right')

        except AssertionError as e:

            driver.get_screenshot_as_file('D:\\work\\appium\\report\\' + time.strftime("%Y-%m-%d %H_%M_%S") +'.png')

            print('loginUser is Error')

test_logout.py:

#coding:UTF-8

"""

@@Time:2018.06.05  15:30

@Author:keitwo

@E-mail:

@FinleName:

"""

from appium import webdriver

import unittest,time

from appium_test.config import driver_configure

class logoutTest(unittest.TestCase):

    @classmethod

    def setUpClass(cls):

        dconfigur = driver_configure.driver_configure()

        cls.driver = dconfigur.get_driver()

    @classmethod

    def tearDownClass(cls):

        time.sleep(3)

        cls.driver.quit()

    def test_logout(self):

        """退出简书APP-测试用例"""

        driver = self.driver

        driver.find_element_by_id("com.jianshu.haruki:id/iv_setting").click()

        time.sleep(2)

        driver = self.swipeUp()

        time.sleep(2)

        driver.get_screenshot_as_file('D:\\work\\appium\\report\\' + time.strftime("%Y-%m-%d %H_%M_%S") + '.png')

        driver.find_element_by_id("com.jianshu.haruki:id/tv_logout").click()

        time.sleep(2)

        driver.switch_to_alert()  #定位弹框

        driver.find_element_by_name(u'是').click()

    def getSize(self):  # 获取屏幕宽和高

        x = self.driver.get_window_size()['width']

        y = self.driver.get_window_size()['height']

        return (x, y)

    def swipeUp(self, t):  # 向上滑动

        l = self.getSize()

        x1 = int(l[0] * 0.5)

        y1 = int(l[1] * 0.75)

        y2 = int(l[1] * 0.25)

        self.driver.swipe(x1, y1, x1, y2, t)

四、执行脚本文件

runtest.py:

#coding:UTF-8

"""

@@Time:2018.06.05  15:30

@Author:keitwo

@E-mail:

@FinleName:

#######      使用时请先打开APPIUM再执行测试###########

"""

import unittest,HTMLTestRunner_PY3,time

def creatsuite():

testunit = unittest.TestSuite()

# 设置测试文件查找的目录

    test_dir ='D:\\work\\appium_test\\test_case'

    # 定义 discover 方法的参数

    discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_*.py', top_level_dir=None)

#discover = unittest.defaultTestLoader.discover(test_dir, pattern='test_login.py', top_level_dir=None)

    #discover 方法筛选出来的用例,循环添加到测试套件中

    for test_suitein discover:

testunit.addTests(test_suite)

print(testunit)

return testunit

alltestnames = creatsuite()

if __name__ =='__main__':

report_title =u'简书app Appium自动化测试报告'

  desc =u'版本号V1.0  
 作者: keitwo'

    now = time.strftime("%Y-%m-%d %H_%M_%S")

report_file ='D:\\work\\appium_test\\report\\' + now +'result.html'

    with open(report_file, 'wb')as report:

runner = HTMLTestRunner_PY3.HTMLTestRunner(stream=report, title=report_title, description=u'这是一个简书APP测试用例Demo!')

runner.run(alltestnames)

五、代码程序功能说明

README.md:

##环境

APPIUM1.7 + Python3.4.4

##说明

以上为简单的appium自动化测试脚框架,能够自动生成测试报告(bootstrap风格),理论上可以满足绝大部分需求。

## TestCase文件夹夹

放置测试用例,执行时将会找出该文件夹下以test_开头的测试用例,并依次执行

## HTMLTestRunner_PY3.py

用于生成自动化报告

## report

最终的测试报告目录

##简书地址

http://www.jianshu.com

后续还可以增加断言、错误截图并把截图显示在测试报告中去。

目录
相关文章
|
7月前
|
测试技术 Python
Appium自动化框架从0到1之 执行测试用例& 生成测试报告&发送邮件
Appium自动化框架从0到1之 执行测试用例& 生成测试报告&发送邮件
111 1
|
测试技术 Python
Appium自动化框架从0到1之 执行测试用例& 生成测试报告&发送邮件
Appium自动化框架从0到1之 执行测试用例& 生成测试报告&发送邮件
171 0
|
测试技术
Appium自动化框架从0到1之 测试用例封装
Appium自动化框架从0到1之 测试用例封装
93 0
|
数据安全/隐私保护
Appium自动化框架从0到1之 业务模块封装(登录页面业务操作)
Appium自动化框架从0到1之 业务模块封装(登录页面业务操作)
111 0
Appium自动化框架从0到1之 业务模块封装(登录页面业务操作)
Appium自动化框架从0到1之 基类的封装
Appium自动化框架从0到1之 基类的封装
102 0
|
测试技术
Appium自动化框架从0到1之 公共方法的封装
Appium自动化框架从0到1之 公共方法的封装
91 0
Appium自动化框架从0到1之 日志文件配置(log.conf)
Appium自动化框架从0到1之 日志文件配置(log.conf)
121 0
Appium自动化框架从0到1之Driver驱动的封装
Appium自动化框架从0到1之Driver驱动的封装
197 0
Appium自动化框架从0到1之 Driver配置封装
Appium自动化框架从0到1之 Driver配置封装
131 0
|
测试技术 Android开发
Appium自动化框架从0到1之 框架结构组成
Appium自动化框架从0到1之 框架结构组成
113 0
Appium自动化框架从0到1之 框架结构组成