python webdriver 简单框架
来源:互联网 发布:vs软件下载 编辑:程序博客网 时间:2024/06/07 16:22
以百度搜索为例
一、首先写百度的元素定位类,后期可以用excel表代替,命名为locators.py
# -*- coding: utf-8 -*-from selenium.webdriver.common.by import Byclass BaiduLocator(object): Search_Input = (By.ID,"kw") Search_Submit_Button = (By.ID,"su")#other locator ....class BaiduUserLocator(object): """Baidu User Locator""" pass
二、 写关于百度页面的操作类,命名为page.py
# -*- coding: utf-8 -*-from locators import *import sysreload(sys)sys.setdefaultencoding('utf8')class BasePage(object): """基类""" def __init__(self,deriver): self.driver = driverclass BaiduSearchPage(BasePage): """百度搜索,所有页面都继承于BasePage""" def input_search(self): #输入关键词 search_input = self.driver.find_element(*BaiduLocator.Search_Input) search_input.clear() search_input.send_keys("selenium") def click_su(self): #点击搜索 su_button = self.driver.find_element(*BaiduLocator.Search_Submit_Button).click() def is_title_match(self): #断言 return "selenium" in self.driver.title
三、引入unittest测试框架,生成测试用例,此文件命名为baidusearch.py
# -*- coding: utf-8 -*-from selenium import webdriverimport page,unittest,timereload(sys)sys.setdefaultencoding('utf8')class Baidu(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() #选择浏览器,根据需要自己选择 #self.driver = webdriver.Firefox() self.driver.implicitly_wait(30) self.base_url = "http://www.baidu.com" self.driver.get(self.base_url) self.verificationErrors = [] self.accept_next_alert = True def test_baidu_search(self): '''baidu search test''' search_page = page.BaiduSearchPage(self.driver) #初始化百度页面 search_page.input_search() #输入关键词 time.sleep(1) #等待1秒钟 search_page.click_su() #点击搜索 self.assertTrue(search_page.is_title_match(),"selenium is not in title") #断言 def tearDown(self): self.driver.quit() #清环境if __name__ == "__main__": unittest.main()
后面的如果需要继续测试就是不断的完善这3个文件的过程,其实locator可以用excel来管理,可以参考selenium ide的三列来做
至此结束,关于传入参数的问题,我在另外的章节中讲,需要重写unittest框架,网上也有很多类似教程
– 文章结束了:随便写的,不写太多,免得看到太复杂了,真实项目操作的时候,大多时间花在页面元素定位上,
1、想一下一个元素没有id,没有name,怎么来定位
2、现在很多web框架中列表元素的id是动态的怎么定位
– 欢迎转载,注明出处
– by leikai 2016-03
0 0
- python webdriver 简单框架
- Selenium-Webdriver(python) (六) --- Unittest框架分析
- selenium-webdriver(python) (十六) --unittest 框架
- python + webdriver
- webdriver--Python
- selenium-webdriver(python) (七)-----多层框架或窗口的定位
- webdriver基础python版(一)简单的浏览器操作
- webdriver基础python版(二)简单的元素定位
- python webdriver简单实例:爬取网页图片
- 轻松自动化---selenium-webdriver(python) (三) 简单元素的定位
- 【python】用selenium webdriver做简单的表格提交
- python爬虫webdriver.Chrome 数据可视化简单案例matplotlib
- python简单通用框架
- WebDriver的简单使用
- selenium2 webdriver 简单使用
- selenium2 webdriver 简单使用
- selenium WebDriver简单使用
- selenium-webdriver(python) (一)
- 深入浅出UML类图
- C++ STL 容器、迭代器、适配器
- 对ByteArrayInputStream/ByteArrayOutputStream类的应用里的transform函数的复用
- MFC——10.网络编程基础
- Android开源项目分类汇总(三) ViewPager 、Gallery、GridView、ImageView、ProgressBar
- python webdriver 简单框架
- 多线程: 自定义操作(模仿 SDWebImage)
- Android让dialog充满整个屏幕
- TOJ 2839.Dream Counting
- 微信公众号开发--微信JS-SDK分享到朋友圈和分享给朋友
- FFMPEG视音频编解码零基础学习方法
- 关于Quartus II 编译后的资源占用显示为0或者显示明显不对的说明
- Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用
- 文章标题