Selenium(Python)PageObject页面对象
2024-08-27 16:22:37
使用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()
最新文章
- 记录一次MongoDB3.0.6版本wiredtiger与MMAPv1引擎的写入耗时对比
- Quartz作业调度框架及时间表达式的含义和语法
- Hibernate,一对一外键单向 记录。Timestamp 的一个坑。
- linux上安装php+gd扩展
- Windows下RCNN的使用
- jquery面试需要看的基本东西
- 基于 unity ngui 上的滚动加载__UiVirtual
- ios App 打包
- 430单片机之定时器A功能的大致介绍
- springboot 集成shiro
- C# 获取 ipv4的方法
- Adobe XD 介绍
- Scrapy at a glance预览
- Linux基础命令---lp打印文件
- Nand Flash 驱动框架
- kali拿取路由器pin码
- powershell玩转litedb数据库-第二版
- YUICompressor的安装及使用(一)
- TP的di
- C# 获取listview中选中一行的值
热门文章
- HTMLFormElement获取表单里面所有的值然后以json形式返回
- iOS应用软件沙盒sandbox相关知识(整理)
- WinCE下SQLCE数据库开发(VS,VB.net,VC++)
- .net core运行环境搭建 linux + windows
- Shell脚本之Crontab的格式
- Linux -- date 日期命令
- 暂存,本人博客有bug,正在全力修复。
- 几行代码实现iOS摇一摇功能
- hdu 1394 Minimum Inversion Number(逆序数对) : 树状数组 O(nlogn)
- JAVA 中的文件读取