POM及POM设计原理
POM(page object model)
页面对象模型,主要应用于
UI
自动化测试框架的搭建,主流设计模式之
一,页面对象模型:结合面向对象编程思路:把项目的每个页面当做一个对象进行编程
python
基础:什么对象?
python
中对象
=
属性
+
行为 通过类定义
=
具有相同属性
+
相同行为对象集合00
POM
一版分为四层
第一层
:basepage
层
:
描述每个页面相同的属性及行为
第二层:
pageobject
层
(
每个的独有特征及独有的行为
)
第三层:
testcase
层
(
用例层,描述项目业务流程
)
第四层:
testdata(
数据层
)
from
appium
import
webdriver
caps
= {}
caps
[
"platformName"
] =
"Android"
caps
[
"deviceName"
] =
"127.0.0.1:62001"
caps
[
"appPackage"
] =
"com.tencent.mobileqq"
caps
[
"appActivity"
] =
"com.tencent.mobileqq.activity.LoginActivity"
driver
=
webdriver
.
Remote
(
"http://localhost:4723/wd/hub"
,
caps
)
driver
.
implicitly_wait
(
30
)
#
导航界面
el2
=
driver
.
find_element_by_id
(
"com.tencent.mobileqq:id/btn_login"
)
el2
.
click
()
#
登录界面
el3
=
driver
.
find_element_by_accessibility_id
(
"
请输入
QQ
号码或手机或邮箱
"
)
el3
.
clear
()
el3
.
send_keys
(
"766603163"
)
el4
=
driver
.
find_element_by_accessibility_id
(
"
密码 安全
"
)
el4
.
clear
()
el4
.
send_keys
(
"lly19891024lly"
)
el5
=
driver
.
find_element_by_accessibility_id
(
"
登 录
"
)
el5
.
click
()
po
模型操作
basepage(
封装公共的属性和行为
)
class
BasePages
:
def
__init__
(
self
,
driver
):
self
.
driver
=
driver
#
元素定位
def
locator
(
self
,
*
loc
):
return
self
.
driver
.
find_element
(
*
loc
)
#
清空
def
clear
(
self
,
*
loc
):
self
.
locator
(
*
loc
).
clear
()
#
输入
def
input
(
self
,
test
,
*
loc
):
self
.
locator
(
*
loc
).
send_keys
(
test
)
#
点击
def
click
(
self
,
*
loc
):
self
.
locator
(
*
loc
).
click
()
#
滑动
(
上下左右滑动
)
def
swipe
(
self
,
start_x
,
start_y
,
end_x
,
end_y
,
duration
=
0
):
#
获取屏幕的尺寸
window_size
=
self
.
driver
.
get_window_size
()
x
=
window_size
[
"width"
]
y
=
window_size
[
"height"
]
self
.
driver
.
swipe
(
start_x
=
x
*
start_x
,
start_y
=
y
*
start_y
,
end_x
=
x
*
end_x
,
end_y
=
y
*
end_y
,
duration
=
duration
)
导航的代码
业务页代码
daohang_page.py(
导航模块
)
from
day02
.
base
.
basepage
import
BasePages
from
appium
.
webdriver
.
common
.
mobileby
import
MobileBy
#
导航页面
= base
层属性赫行为
+
当前界面的特有的属性行为
class
DaoHangPage
(
BasePages
):
def
__init__
(
self
,
driver
):
BasePages
.
__init__
(
self
,
driver
)
def
click_login
(
self
):
self
.
click
(
MobileBy
.
ID
,
"com.tencent.mobileqq:id/btn_login"
)
login_page.py(
登录模块
)
from
day02
.
base
.
basepage
import
BasePages
from
appium
.
webdriver
.
common
.
mobileby
import
MobileBy
#
导航页面
= base
层属性赫行为
+
当前界面的特有的属性行为
class
DaoHangPage
(
BasePages
):
def
__init__
(
self
,
driver
):
BasePages
.
__init__
(
self
,
driver
)
def
click_login
(
self
):
self
.
click
(
MobileBy
.
ID
,
"com.tencent.mobileqq:id/btn_login"
)
单元测试模块
from
day02
.
pageobjects
.
daohang_page
import
DaoHangPage
from
day02
.
pageobjects
.
login_page
import
LoginPage
from
appium
import
webdriver
import
pytest
import
time
class
TestClass
():
@classmethod
def
setup_class
(
cls
)
->
None
:
caps
= {}
caps
[
"platformName"
] =
"Android"
caps
[
"deviceName"
] =
"127.0.0.1:62001"
caps
[
"appPackage"
] =
"com.tencent.mobileqq"
caps
[
"appActivity"
] =
"com.tencent.mobileqq.activity.LoginActivity"
cls
.
driver
=
webdriver
.
Remote
(
"http://localhost:4723/wd/hub"
,
caps
)
cls
.
driver
.
implicitly_wait
(
30
)
def
test_01_daohang
(
self
):
daohang
=
DaoHangPage
(
self
.
driver
)
daohang
.
click_login
()
def
test_02
(
self
):
login
=
LoginPage
(
self
.
driver
)
login
.
clear_name
()
login
.
input_name
(
"766603163"
)
login
.
clear_pass
()
login
.
input_pass
(
"lly19891024lly"
)
login
.
click_dl_button
()
@classmethod
def
teardown_class
(
cls
)
->
None
:
time
.
sleep
(
20
)
cls
.
driver
.
quit
()
if
__name__
==
'__main__'
:
pytest
.
main
()