Appium移动端自动化测试之PageObject设计模式

一、先来看一下整体appium po的架构图设计

二、我们先看PO文件中base_page类的实现:

#coding=utf-8

'''
po设计模式:page object 页面对象
所有用到的页面都定义成一个类,继承自基础的Page类
把页面中用到的元素定义成方法
把页面上一些操作定义成方法
'''

# 基础类 用于所有页面的继承
class Action(object):
    #初始化
    def __init__(self,driver):
        self.driver = driver

    #重新
    def by_id(self,loc):
        try:
            return self.driver.find_element_by_id(loc)
        except Exception as e:
            print('未找到 {0}'.format(e))

    def by_name(self, loc):
        try:
            return self.driver.find_element_by_name(loc)
        except Exception as e:
            print('未找到 {0}'.format(e))

    def by_xpath(self, loc):
        try:
            return self.driver.find_element_by_xpath(loc)
        except Exception as e:
            print('未找到 {0}'.format(e))

我们对selenium的元素定位进行二次封装设计,对id/xpath定位进行改写。

三、我们对登录页面进行页面对象设计

#coding=utf-8
'''
airen登陆页面对象设计
'''
from  Appium.PO import base_page
import time

class Airen_Login_Page(base_page.Action):

    add_button_my=("com.juyang.mall:id/rb_Mine")         #点击我的
    clear_content=("com.juyang.mall:id/edit_Tel")        #清空
    input_username=("com.juyang.mall:id/edit_Tel")       #输入账户
    input_password=("com.juyang.mall:id/edit_Pwd")       #输入密码
    click_login_button=("com.juyang.mall:id/tv_Login")   #点击登陆按钮
    login_result_page_text=(u"商城")                      #验证返回商城首页

    def click_my_button(self):
        '''封装点击我的方法'''
        self.by_id(self.add_button_my).click()
        time.sleep(5)

    def clear_login_content(self):
        '''封装情空方法'''
        self.by_id(self.clear_content).clear()

    def input_text_username(self,username):
        '''封装输入用户名方法'''
        self.by_id(self.input_username).send_keys(username)
        time.sleep(3)

    def input_text_password(self,password):
        '''封装输入密码方法'''
        self.by_id(self.input_password).send_keys(password)

    def click_loginbtn(self):
        '''封装点击登陆方法'''
        self.by_id(self.click_login_button).click()
        time.sleep(5)

    def get_finish_button_text(self):
        '''登陆成功后获取首页商城信息'''
        return self.by_name(self.login_result_page_text).text

    def login_airen_appproject(self,user,passwd):
        '''登陆流程封装'''
        self.click_my_button()
        self.clear_login_content()
        self.input_text_username(user)
        self.input_text_password(passwd)
        self.click_loginbtn()

# def add_button_link(self):
     #     self.find_element(self.add_button_loc).click()
     #     time.sleep(3)           #等待3秒,等待登录弹窗加载完成
     #
     #
     # def run_case(self,value):
     #     self.find_element(self.edittext_loc).send_keys(value)
     #     time.sleep(5)
     #     self.find_element(self.finish_button_loc).click()
     #     time.sleep(2)
     #
     #
     # def get_finish_button_text(self):
     #     return self.find_element(self.edittext_loc).text
     #

# driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) #链接app
# sleep(4)
# driver.find_element(By.ID,'com.juyang.mall:id/rb_Mine').click()  #点击我的
# sleep(4)
# driver.find_element(By.ID,'com.juyang.mall:id/edit_Tel').clear()
# driver.find_element(By.ID,'com.juyang.mall:id/edit_Tel').send_keys("18665100958")
# driver.find_element(By.ID,'com.juyang.mall:id/edit_Pwd').send_keys("123456")
# driver.find_element(By.ID,"com.juyang.mall:id/tv_Login").click()
# sleep(8)

1.把所有用到的元素都定义成一个方法。

2.每一个操作步骤都封装为一个方法。

四、testCase文件中测试用例的实现如下:

#coding=utf-8

from Appium.pages.loginpage import Airen_Login_Page
from Appium.PO.base_page import Action
import unittest
import time
from appium import webdriver

class TestAirenLogin(unittest.TestCase):

    def setUp(self):
        desired_caps = {'platformName': 'Android',
                        'deviceName': '127.0.0.1:62001',
                        'platformVersion': '3.8.3.1',
                        'appPackage': 'com.juyang.mall',
                        'appActivity': 'com.shanjian.juyang.activity.home.Activity_Home'
                        }
        desired_caps["unicodeKeyboard"] = "True"  #使用unicode编码方式发送字符串
        desired_caps["resetKeyboard"] = "True"   #将键盘隐藏起来
        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        # self.driver.wait_activity('.activity.other.Activity_In', 10)  # 等待app首页出现
        self.verificationErrors = u'商城'

    def tearDown(self):
        time.sleep(4)
        self.driver.close_app()

    def test_aiRenLogin(self):
        '''PO设计登陆功能实战'''
        aiRen = Airen_Login_Page(self.driver)
        aiRen.login_airen_appproject('18665100958','123456')
        #断言:实际结果  预期结果  错误信息
        self.assertEqual(self.verificationErrors,aiRen.get_finish_button_text(),msg=u'验证失败!')

if __name__ == '__main__':
    unittest.main()

对整个登录模块进行测试用例编写。

五、总执行文件去调用测试用例,并输出测试报告

#coding=utf-8

import unitTests,os,time
from Public import HTMLTestRunner

cur_path = os.path.dirname(os.path.realpath(__file__))
case_path = os.path.join(cur_path,'test_Cases')
report_path = os.path.join(cur_path,'Result')

if __name__ == '__main__':
    testlist = unitTests.defaultTestLoader.discover(case_path, pattern='test*.py')
    now_time = time.strftime('%Y_%m_%d_%H_%M_%S')
    report_name = report_path + '\\' + now_time + 'result.html'
    fp = HTMLTestRunner.HTMLTestRunner(stream=open(report_name,'wb'),
                                       title='appium-po设计模式自动化测试报告',
                                       description='window7 夜神模拟器艾人针灸')
    fp.run(testlist)
posted @ 2018-08-20 15:08  IT测试老兵  阅读(2990)  评论(1编辑  收藏  举报
作者:测试老兵
出处:https://www.cnblogs.com/fighter007/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。