Appium+unittest+PageObject实例

摘要:
/usr/bin/envpython#-*-coding:utf-8-*-#@时间:2019-07-0417:22#@作者:zhouyang#@文件:commom_ fun.pyfromappium_advance.page_ object.desired_ capimportappium_desiredfromappium.advance.page _ object.baseViewimportBaseViewfromselenium.common.exceptionimportNoSuchElementExceptionimportloggingfromselenium.webdriver.common.byimportByclassCommom(BaseView):cancelBtn=(按.ID,'droid:ID/button2')skipBtn=(按.ID,'com.tal.caoyan:ID/tv_skip')defcheck_cancelBtn(self):日志记录.info ind_元素(*self.skipBtn)exceptNoSuchElementException:logging.info('ocancelBtn')else:skipBtn.click()if__name__=='__main__':driver=appium_desired()com=Commom(driver)com.check _ cancleBtn()com.check _skipBtn(()ViewCode#!

实例中设计到的文件如下红色标出

Appium+unittest+PageObject实例第1张

Appium+unittest+PageObject实例第2张Appium+unittest+PageObject实例第3张
[loggers]
keys=root,infoLogger

[logger_root]
level=DEBUG
handlers=consoleHandler,fileHandler

[logger_infoLogger]
handlers=consoleHandler,fileHandler
qualname=infoLogger
propagate=0

[handlers]
keys=consoleHandler,fileHandler

[handler_consoleHandler]
class=StreamHandler
level=INFO
formatter=form02
args=(sys.stdout,)

[handler_fileHandler]
class=FileHandler
level=INFO
formatter=form01
args=('runlog_conf.log', 'a')

[formatters]
keys=form01,form02

[formatter_form01]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s

[formatter_form02]
format=%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s
View Code
Appium+unittest+PageObject实例第4张Appium+unittest+PageObject实例第5张
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-07-04 17:17
# @File : baseView.py


class BaseView(object):
    def __init__(self,driver):
        self.driver=driver
    def find_element(self,*loc):
        return self.driver.find_element(*loc)
View Code
Appium+unittest+PageObject实例第6张Appium+unittest+PageObject实例第7张
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-07-04 17:22
# @Author : zhouyang
# @File : commom_fun.py

from appium_advance.page_object.desired_caps import appium_desired
from appium_advance.page_object.baseView import BaseView
from selenium.common.exceptions import NoSuchElementException
import logging
from selenium.webdriver.common.by import By

class Commom(BaseView):
    cancelBtn=(By.ID,'android:id/button2')
    skipBtn=(By.ID,'com.tal.kaoyan:id/tv_skip')

    def check_cancleBtn(self):
        logging.info('===========check cancleBtn==========')
        try:
            cancelBtn = self.driver.find_element(*self.cancelBtn)
        except NoSuchElementException:
            logging.info('no cancelBtn')
        else:
            cancelBtn.click()

    def check_skipBtn(self):
        logging.info('===========check cancelBtn===========')
        try:
            skipBtn = self.driver.find_element(*self.skipBtn)
        except NoSuchElementException:
            logging.info('no cancelBtn')
        else:
            skipBtn.click()

if __name__ == '__main__':
    driver=appium_desired()
    com=Commom(driver)
    com.check_cancleBtn()
    com.check_skipBtn()
View Code
Appium+unittest+PageObject实例第8张Appium+unittest+PageObject实例第9张
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-07-03 11:05
# @Author : zhouyang
# @File : capability_yaml.py
'''
从desired_caps.yaml文件中获取capability数据,登录考研帮app,把日志保存在文件中
'''
from selenium import webdriver
import yaml
import logging
import logging.config


file=open('../yaml/desired_caps.yaml','r')
data=yaml.load(file)

CON_LOG='../log/log.conf'
logging.config.fileConfig(CON_LOG)
logging=logging.getLogger()

def appium_desired():
    desired_caps = {}
    desired_caps['platformName'] = data['platformName']
    desired_caps['platformVerion'] = data['platformVersion']
    desired_caps['deviceName'] = data['deviceName']
    desired_caps['app'] = data['app']
    desired_caps['noReset'] = data['noReset']
    desired_caps['appPackage'] = data['appPackage']
    desired_caps['appActivity'] = data['appActivity']
    desired_caps['unicodeKeyboard'] = data['unicodeKeyboard']
    desired_caps['resetKeyboard'] = data['resetKeyboard']

    logging.info('start info...')

    driver = webdriver.Remote('http://' + str(data['ip']) + ':' + str(data['port']) + '/wd/hub', desired_caps)
    driver.implicitly_wait(8)
    return driver

if __name__ == '__main__':
    appium_desired()
View Code
Appium+unittest+PageObject实例第10张Appium+unittest+PageObject实例第11张
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-07-05 11:42
# @Author : zhouyang
# @File : loginView.py

from appium_advance.page_object.desired_caps import appium_desired
from appium_advance.page_object.commom_fun import Commom
import logging
from selenium.webdriver.common.by import By

class LoginView(Commom):
    username_type=(By.ID,'com.tal.kaoyan:id/login_email_edittext')
    password_type=(By.ID,'com.tal.kaoyan:id/login_password_edittext')
    submit_type=(By.ID,'com.tal.kaoyan:id/login_login_btn')

    def login_action(self,username,password):
        self.check_cancleBtn()
        self.check_skipBtn()
        logging.info('=================login===================')
        logging.info('input username:%s'%username)
        self.driver.find_element(*self.username_type).send_keys(username)

        logging.info('input password:%s' %password)
        self.driver.find_element(*self.password_type).send_keys(password)

        logging.info('click loginBtn')
        self.driver.find_element(*self.submit_type).click()
        logging.info('===============login finish==============')

if __name__ == '__main__':
    driver=appium_desired()
    l=LoginView(driver)
    l.login_action('自学网2018','zxw2018')

        
View Code
Appium+unittest+PageObject实例第12张Appium+unittest+PageObject实例第13张
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-07-05 11:42
# @Author : zhouyang
# @File : loginView.py

from appium_advance.page_object.desired_caps import appium_desired
from appium_advance.page_object.commom_fun import Commom
import logging
from selenium.webdriver.common.by import By

class LoginView(Commom):
    username_type=(By.ID,'com.tal.kaoyan:id/login_email_edittext')
    password_type=(By.ID,'com.tal.kaoyan:id/login_password_edittext')
    submit_type=(By.ID,'com.tal.kaoyan:id/login_login_btn')

    def login_action(self,username,password):
        self.check_cancleBtn()
        self.check_skipBtn()
        logging.info('=================login===================')
        logging.info('input username:%s'%username)
        self.driver.find_element(*self.username_type).send_keys(username)

        logging.info('input password:%s' %password)
        self.driver.find_element(*self.password_type).send_keys(password)

        logging.info('click loginBtn')
        self.driver.find_element(*self.submit_type).click()
        logging.info('===============login finish==============')

if __name__ == '__main__':
    driver=appium_desired()
    l=LoginView(driver)
    l.login_action('自学网2018','zxw2018')

        
View Code
Appium+unittest+PageObject实例第14张Appium+unittest+PageObject实例第15张
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-07-05 16:18
# @Author : zhouyang
# @File : myunit.py

from appium_advance.page_object.desired_caps import appium_desired
import logging
import unittest
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()
View Code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019-07-05 16:24
# @Author : zhouyang
# @File : test_login.py
'''
unittest写的测试用例
'''
import unittest
from appium_advance.unittest.myunit import StartEnd
from appium_advance.page_object.loginView import LoginView
import logging

class Test_Login(StartEnd):
    def test_login_zxw2018(self):
        logging.info('=========test_login_zxw2018=========')
        l=LoginView(self.driver)
        l.login_action('自学网2018','zxw2018')

    def test_login_zxw2017(self):
        logging.info('=========test_login_zxw2017=========')
        l=LoginView(self.driver)
        l.login_action('自学网2017','zxw2017')

    def test_login_error(self):
        logging.info('=========test_login_error=========')
        l=LoginView(self.driver)
        l.login_action('123','456')

if __name__ == '__main__':
    unittest.main()
Appium+unittest+PageObject实例第16张Appium+unittest+PageObject实例第17张
platformName: Android
platformVersion: 4.4.2
deviceName: 127.0.0.1:62001
app: C:UsersAdministratorDesktopkaoyan3.1.0.apk
appPackage: com.tal.kaoyan
appActivity: com.tal.kaoyan.ui.activity.SplashActivity
noReset: False
unicodeKeyboard: True
resetKeyboard: True
ip: 127.0.0.1
port: 4723
View Code

免责声明:文章转载自《Appium+unittest+PageObject实例》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇h5页面利用canvas压缩图片并上传mORMot使用基础 2 数据库连接下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

NET Core3前后端分离开发框架

NET Core前后端分离快速开发框架 https://www.cnblogs.com/coldairarrow/p/11870993.html 引言 时间真快,转眼今年又要过去了。回想今年,依次开源发布了Colder.Fx.Net.AdminLTE(254Star)、Colder.Fx.Core.AdminLTE(335Star)、DotNettySoc...

python unittest控制用例的执行顺序

为什么要进行顺序控制呢?使用过testng的同学就知道,它相对于junit来说有更强大的功能,其中的一个功能就是依赖测试。什么是依赖测试呢?简单的说一下就是,A方法运行时,其中有个变量的取值是B方法的动态产出值。这样就必须先执行B方法。testng的test方法有dependson属性来制定方法的依赖。但是向python的unittet框架,他类似于jun...

selenium+python自动化90-unittest多线程执行用例

前言 假设执行一条脚本(.py)用例一分钟,那么100个脚本需要100分钟,当你的用例达到一千条时需要1000分钟,也就是16个多小时。。。 那么如何并行运行多个.py的脚本,节省时间呢?这就用到多线程了,理论上开2个线程时间节省一半,开5个线程,时间就缩短五倍了。 项目结构 1.项目结构跟之前的设计是一样的: case test开头的.py用例脚本 c...

UI自动化之分层思想pom模式

1.什么是POM页面对象模型(POM)是一种设计模式,用来管理维护一组web元素集的对象库;在POM下,应用程序的每一个页面都有一个对应的page class; 每一个page class维护着该web页的表现层和操作层;page class中的方法命名最好根据其对应的业务场景进行, 例如通常登录后我们需要等待几秒中,我们可以这样命名该方法: waitin...

python从入门到实践第十一章练习题

'''这是11-1和11-2的练习题用于接受城市名称的函数'''"""def get_ciy_name(city,country):#定义一个城市名称的函数有2个形参 city_name = city + ',' + country return city_name.title()#把值返回给函数,用title()的方法把每个单词的第一个字母转...

Python:Python 自动化测试框架 unittest 和 pytest 对比

一、用例编写规则     1.unittest提供了test cases、test suites、test fixtures、test runner相关的类,让测试更加明确、方便、可控。使用unittest编写用例,必须遵守以下规则:   (1)测试文件必须先import unittest   (2)测试类必须继承unittest.TestCase   (...