App自动化测试笔记(十):PO模式

目录

什么是PO?

优点

案例练习

抽取前置代码

优点

步骤

结构

代码

base_driver.py代码

test_setting.py代码

抽取page

好处

步骤

结构

代码

base_driver.py代码

display_page.py代码

more_page.py代码

test_setting.py代码

抽出find_element

好处

步骤

结构

代码

base_driver.py代码

base_find.py代码

display_page.py代码

more_page.py代码

test_setting.py代码

增加WebDriverWait和默认时间

好处

步骤

抽取动作到base_find

步骤

代码

base_find.py代码

display_page.py中代码

more.py中代码

将不同的元素放在不同的page中

需求

步骤

结构

代码

setting_page.py中代码

display_page中代码

more.py中代码

network.py中代码

page的统一入口

需求

步骤

page包改造后结构

代码

page.py代码

test_setting.py改造后代码

总结

工作中的步骤

模块在项目中的角色

简单的写


什么是PO?

  --页面和脚本分离
   --是一种设计模式

优点

   --减少冗余,提高复用性
   --降低耦合度
   --降低维护成本

案例练习

"""
更多-移动网络-首选网络类型-点击2g
更多-移动网络-首选网络类型-点击3g
显示-搜索按钮-输入hello-点击返回
"""
from appium import webdriver
import pytest
import time

class TestSetting:
    def setup(self):
        desired_caps = dict()
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '5.1'
        desired_caps['deviceName'] = '192.168.56.101:5555'
        desired_caps['appPackage'] = 'com.android.settings'
        desired_caps['appActivity'] = '.Settings'
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(5)
    def teardown(self):
        self.driver.quit()
//更多-移动网络-首选网络类型-点击2g
    def test_setting01(self):
        self.driver.find_element_by_xpath("//*[@text = '更多']").click()
        self.driver.find_element_by_xpath("//*[@text = '移动网络']").click()
        self.driver.find_element_by_xpath("//*[@text = '首选网络类型']").click()
        self.driver.find_element_by_xpath("//*[@text = '3G']").click()
//更多-移动网络-首选网络类型-点击3g
    def test_setting02(self):
        self.driver.find_element_by_xpath("//*[@text = '更多']").click()
        self.driver.find_element_by_xpath("//*[@text = '移动网络']").click()
        self.driver.find_element_by_xpath("//*[@text = '首选网络类型']").click()
        self.driver.find_element_by_xpath("//*[@text = '2G']").click()
//显示-搜索按钮-输入hello-点击返回
    def test_setting03(self):
        self.driver.find_element_by_xpath("//*[@text = '显示']").click()
        self.driver.find_element_by_id("com.android.settings:id/search").click()
        self.driver.find_element_by_id("android:id/search_src_text").send_keys("hello")

抽取前置代码

优点

可以复用

步骤

1、在项目下新建一个base模块
2、再base模块中新建一个base_driver.py
3、将“前置代码”放在文件的init_driver函数中
4、再test_display.py中的setup调用前置代码,并获取self.driver

结构

- base
--base_driver.py
-scripts
--test_setting.py
-pytest.ini

代码

base_driver.py代码

from appium import webdriver

def init_driver():
    desired_caps = dict()
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '5.1'
    desired_caps['deviceName'] = '192.168.56.101:5555'
    desired_caps['appPackage'] = 'com.android.settings'
    desired_caps['appActivity'] = '.Settings'
    return webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

test_setting.py代码

import os, sys
sys.path.append(os.getcwd())
from base.base_driver import init_driver

"""
更多-移动网络-首选网络类型-点击2g
更多-移动网络-首选网络类型-点击3g
显示-搜索按钮-输入hello-点击返回
"""
class TestSetting:
    def setup(self):
        self.driver = init_driver()
    def teardown(self):
        self.driver.quit()
    def test_setting01(self):
        self.driver.find_element_by_xpath("//*[@text = '更多']").click()
        self.driver.find_element_by_xpath("//*[@text = '移动网络']").click()
        self.driver.find_element_by_xpath("//*[@text = '首选网络类型']").click()
        self.driver.find_element_by_xpath("//*[@text = '3G']").click()
    def test_setting02(self):
        self.driver.find_element_by_xpath("//*[@text = '更多']").click()
        self.driver.find_element_by_xpath("//*[@text = '移动网络']").click()
        self.driver.find_element_by_xpath("//*[@text = '首选网络类型']").click()
        self.driver.find_element_by_xpath("//*[@text = '2G']").click()
    def test_setting03(self):
        self.driver.find_element_by_xpath("//*[@text = '显示']").click()
        self.driver.find_element_by_id("com.android.settings:id/search").click()
        self.driver.find_element_by_id("android:id/search_src_text").send_keys("hello")

在运行过程中出现ModuleNotFoundError: No module named 'base',大体是因为python解释器在寻找模块的时候没有在我当前项目路径下找,所以没有找到。

解决办法:
在你的from base.base_driver import base_driver这句话上面加上:
import os, sys
sys.path.append(os.getcwd())

抽取page

好处

代码复用(多个脚本可能会用到相同的动作特征,只需要在page中写一份)
如果ui有更变,去找page
如果动作的顺序有更变,去找scripts

步骤

1、把之前test中的"self.driver.find_element_by_xpath("//*[@text = '更多']").click()"操作,放在page中
2、在scripts中调用
3、注意:每一个脚本,一定只会有一个driver对象
4、发现,在page中,需要使用到diver对象
5、将test在setup中创建的driver对象,通过page的init进行传递(一定是传递,不是重新调用init的方法)

结构

- base
-- base_driver.py
- page
-- display_page.py
-- more_page.py
- scripts
-- test_setting.py
- pytest.ini

代码

base_driver.py代码

from appium import webdriver

def init_driver():
    desired_caps = dict()
    desired_caps['platformName'] = 'Android'
    desired_caps['platformVersion'] = '5.1'
    desired_caps['deviceName'] = '192.168.56.101:5555'
    desired_caps['appPackage'] = 'com.android.settings'
    desired_caps['appActivity'] = '.Settings'
    return webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

display_page.py代码

class Display():
    def __init__(self,driver):
        self.driver = driver
    def page_display(self):
        self.driver.find_element_by_xpath("//*[@text = '显示']").click()
    def page_lookup(self):
        self.driver.find_element_by_id("com.android.settings:id/search").click()
    def page_send_keys(self):
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值