使用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()

最新文章

  1. 记录一次MongoDB3.0.6版本wiredtiger与MMAPv1引擎的写入耗时对比
  2. Quartz作业调度框架及时间表达式的含义和语法
  3. Hibernate,一对一外键单向 记录。Timestamp 的一个坑。
  4. linux上安装php+gd扩展
  5. Windows下RCNN的使用
  6. jquery面试需要看的基本东西
  7. 基于 unity ngui 上的滚动加载__UiVirtual
  8. ios App 打包
  9. 430单片机之定时器A功能的大致介绍
  10. springboot 集成shiro
  11. C# 获取 ipv4的方法
  12. Adobe XD 介绍
  13. Scrapy at a glance预览
  14. Linux基础命令---lp打印文件
  15. Nand Flash 驱动框架
  16. kali拿取路由器pin码
  17. powershell玩转litedb数据库-第二版
  18. YUICompressor的安装及使用(一)
  19. TP的di
  20. C# 获取listview中选中一行的值

热门文章

  1. HTMLFormElement获取表单里面所有的值然后以json形式返回
  2. iOS应用软件沙盒sandbox相关知识(整理)
  3. WinCE下SQLCE数据库开发(VS,VB.net,VC++)
  4. .net core运行环境搭建 linux + windows
  5. Shell脚本之Crontab的格式
  6. Linux -- date 日期命令
  7. 暂存,本人博客有bug,正在全力修复。
  8. 几行代码实现iOS摇一摇功能
  9. hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)
  10. JAVA 中的文件读取