目录
什么是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):