![be6965270b3a00bd9f164040348fb8f4.png](https://i-blog.csdnimg.cn/blog_migrate/d87e3a06dd749e032c98974d95af0364.jpeg)
接着我们来继续学习Selenium/Appium框架都会用到的一个设计模式:Page Object
其旨在将元素定位和元素操作分层,只接触测试内容,不写基础内容,便于后续对自动化测试用例体系的维护
OK~来看如下用法:
一、Page Object
1.只写测试内容,不写基础内容
2.不需要直接接触Selenium
3.把元素定位和元素操作分层,这样当元素发生变化时,只需维护page层的元素定位,不用关心哪些测试用例使用了这些元素
4.在编写测试用例时,也不用关心元素时如何定位的
5.继承父类后注意driver和逻辑、元素共享
该例子:百度搜索selenium,我们的关系是:BasePage -> BaiduPage -> TestBaidu
1.base_page.py:
# coding = utf8
import os
os.path.abspath(".")
class BasePage:
"""基础Page层,封装一些常用方法
"""
def __init__(self, driver):
self.driver = driver
# 打开页面,如果为None,则继承子类的url,否则用url
def open_url(self, url = None):
if url is None:
self.driver.get(self.url)
else:
self.driver.get(url)
# id定位
def by_id(self, id):
return self.driver.find_element_by_id(id)
# class定位
def by_class(self, classname):
return self.driver.find_element_by_class_name(classname)
# XPath定位
def by_xpath(self, xpath):
return self.driver.find_element_by_xpath(xpath)
# css定位
def by_css(self, css):
return self.driver.find_element_by_css_selector(css)
# 获取title
def get_title(self):
return self.driver.title
# 获取页面text,仅使用xpath定位
def get_text(self, xpath):
return self.by_xpath(xpath).text
# 执行JavaScript脚本
def js(self, script):
self.driver.execute_script(script)
2.baidu_page.py:
# coding = utf8
import os
os.path.abspath(".")
from base_page import BasePage
class BaiduPage(BasePage):
"""百度Page层,百度页面封装操作到的元素
Args:
BasePage (BasePage): Parent class, Inherit it and use the base function
"""
url = "https://www.baidu.com"
def search_input(self, search_key):
self.by_id("kw").send_keys(search_key)
def search_button(self):
self.by_id("su").click()
3.test_baidu.py:
# coding = utf8
import os
os.path.abspath(".")
from baidu_page import BaiduPage
import unittest
from selenium import webdriver
from time import sleep
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
def test_search_key_case(self):
page = BaiduPage(self.driver)
page.open_url()
page.search_input("selenium")
page.search_button()
sleep(2)
self.assertEqual(page.get_title(), "selenium_百度搜索")
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == "__main__":
unittest.main(verbosity = 2)
二、Poium
1.简化了BasePage层元素的定义
2.安装:pip3 install poium
该例子:百度搜索selenium,我们的关系的:BaiduPage -> TestBaidu
1.baidu_page.py:
# coding = utf8
from poium import Page, PageElement
class BaiduPage(Page):
"""百度Page层,百度页面封装操作到到元素
"""
search_input = PageElement(id_ = "kw")
search_button = PageElement(id_ = "su")
2.test_baidu.py:
# coding = utf8
import os
os.path.abspath(".")
import unittest
from baidu_page import BaiduPage
from selenium import webdriver
from time import sleep
class TestBaidu(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
def test_baidu_search_key_case(self):
page = BaiduPage(self.driver)
page.get("https://www.baidu.com")
page.search_input = "selenium"
page.search_button.click()
sleep(2)
self.assertEqual(page.get_title, "selenium_百度搜索")
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if __name__ == "__main__":
unittest.main(verbosity = 2)
三、Poium的更多用法:
(poium是基于seleniim/appium的Page Object的测试库)
1.支持的定位方法
elem_id = PageElement(id_ = "id")
elem_name = PageElement(name = "name")
elem_class = PageElement(class_name = "class")
elem_tag = PageElement(tag = "input")
elem_link_text = PageElement(link_text = "this_is_link")
elem_partial_link_text = PageElement(partial_link_text = "is_link")
elem_xpath = PageElement(xpath = "//*[@id = 'kk']")
elem_css = PageElement(css = "#id")
2.设置元素超时时间
search_input = PageElement(id_ = "kw", timeout = 5)
3.设置元素描述
search_input = PageElement(id_ = "kw", timeout = 5, describe = "搜索输入值")
(describe并无实际意义,是方便元素多时查看)
4.定位一组元素
search_result = PageElements(xpath = "//div/h3/a")
(Poium极大简化了Page层的定义,还提供了很多API,如PageSelect类简化了下拉框的处理等) -- 常用Very Good!
项目结构:
![7209c1b943176280428ccffd97d453e2.png](https://i-blog.csdnimg.cn/blog_migrate/f1cec32a2ee47e55f7e533fc3ebd2654.png)
下一章我们开始学习PyTest!
关注 + 收藏 + 点赞哦,谢谢啦~
![eedab30e9be428adc18b054464142cb8.png](https://i-blog.csdnimg.cn/blog_migrate/3bac45ab67d13869a6b3b2de22185a16.jpeg)