Selenium(Python)PageObject页面对象
2018-03-03 22:36
429 查看
使用PageObject页面对象的好处是,
当页面元素的位置发生改变时,
只需要去修改Xpath或者ID,
而不用去修改测试用例本身;
本次的思路是:
1、常用方法类
2、页面对象类
3、测试用例类
WebDriverMethod.py:
class SeleniumMethod(object):
# 封装Selenium常用方法
def __init__(self, driver):
self.driver = driver
# 构造函数
def getTitle(self):
# 获取页面标题
return self.driver.title
def clearAndInput(self, location, value):
# 根据xpath定位元素并清除、输入
element = self.driver.find_element_by_xpath(location)
element.clear()
element.send_keys(value)
def click(self, location):
# 根据xpath定位元素并点击
return self.driver.find_element_by_xpath(location).click()
def getText(self, location):
# 根据xpath定位元素并获取文本值
return self.driver.find_element_by_xpath(location).text
BaiduHome.py:
from WebDriverMethod import SeleniumMethod
class BaiduPage(SeleniumMethod):
# 百度页面对象
inputBox = ".//*[@id='kw']"
# 百度输入框
searchBotton = ".//*[@id='su']"
# 百度搜索按钮
responseTitle = "中国_百度搜索"
# 搜索结果页的标题
oneResult = ".//*[@id='1']/h3/a"
# 第一行
oneResultText = "中国_百度百科"
# 第一行的文本
def searchChinese(self):
# 搜索中国
self.clearAndInput(self.inputBox, "中国")
self.click(self.searchBotton)
BaiduTest.py:
import unittest
from time import sleep
from selenium import webdriver
from BaiduHome import BaiduPage
class MyTestCase(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.get("https://www.baidu.com/")
assert self.driver.title, "百度一下,你就知道"
sleep(2)
def test_searchChinese(self):
# 测试用例
homePage = BaiduPage(self.driver)
homePage.searchChinese()
sleep(2)
assert homePage.getTitle(), homePage.responseTitle
# 断言搜索结果页标题
assert homePage.getText(homePage.oneResult), homePage.oneResultText
# 断言搜索结果第一行的文本
def tearDown(self):
self.driver.close()
self.driver.quit()
if __name__ == '__main__':
unittest.main()
相关文章推荐
- selenium 的页面对象模型Page Object
- Selenium2(java)页面对象模型(Page Object) 八
- Python3-Selenium3使用PO设计模式(Page Object)实现简单的页面登录操作
- [译]Selenium Python文档:六、页面对象
- Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)
- Selenium的PO模式(Page Object Model)[python版]
- 页面对象(page object)模式
- Page Object Model (Selenium, Python)(一)
- Page Object Model (Selenium, Python)
- JAVA自动化测试之页面对象模型(Page Object)
- Python+Selenium框架设计--- Page Object Model
- Selenium(Python)页面对象+数据驱动测试框架
- Selenium+Python :WebDriver设计模式( Page Object )
- Page Object Model (Selenium + Python)(三)
- Selenium的PO模式(Page Object Model)|(Selenium Webdriver For Python)
- Selenium的PO模式(Page Object Model)[python版]
- Python - 类(class)的变量 和 对象(object)的变量 详解 及 代码
- ASP.NET 页面对象模型(page)
- Selenium PageFactory页面工厂
- appium笔记七:PageObject页面封装