lettuce webdriver 自动化测试---玩转BDD
行为驱动开发(BDD),依然高大上的矗立在远方,很少被人问津,一方面是BDD的思想不太容易理解,别一方面BDD的资料并不多。中文的资料就更少了。
之前增写过一篇《python BDD 框架之lettuce》 来介绍BDD ,本文将在此基础上通过lettuce 和webdriver来实现自动化测试,感兴趣的测试同学跟我一起装X吧!
下面向读者介绍如何通过lettuce 和 webdriver 结合来编写自动化脚本。
环境配置:
------------------------------------------
前提:有python载发环境,并且安装了pip ,强烈建议在linux下操作。
第一步,安装lettuce
root@machine:~$ [sudo] pip install lettuce
第二步,安装lettuce_webdriver
https://pypi.python.org/pypi/lettuce_webdriver
root@machine:~$ [sudo] pip install lettuce_webdriver
第三步,安装nose
nose继承自unittest,属于第三方的python单元测试框架,且更容易使用,lettuce_webdriver包的运行依赖于nose模块
https://pypi.python.org/pypi/nose/
root@machine:~$ [sudo] pip install nose
---------------------------------
下面以为百度搜索为例,好吧!谁让这个例子能简单明了的讲解问题呢。所以,我们再次以百度搜索为例。
一般的百度搜索自动化脚本是这样的:
# coding = utf-8 from selenium import webdriverbrowser = webdriver.Firefox() browser.get("http://www.baidu.com") browser.find_element_by_id("kw1").send_keys("selenium") browser.find_element_by_id("su1").click() browser.quit()
下面看看BDD是怎么玩的:
创建目录结构如下:
.../test/features/baidu.feature
/step_definitions/setps.py
/support/terrain.py
先来回顾一下我们去写百度搜索脚本的过程:
功能:访问百度场景:搜索selenium我去访问百度的“http://www.badiu.com”通过id 为“kw1”找到输入框并输入“selenium”关键字点击 id为“su1” 按钮然后,我在搜索结果上找到了“seleniumhq.com”的网址最后,我关闭了浏览器
OK,确定了我们要做事情的过程,下面将其转换成BDD 的描述文件。
baidu.feature
Feature: Go to baidu Scenario: search selenium Given I go to "http://www.baidu.com/" When I fill in field with id "kw1" with "selenium"And I click id "su1" with baidu once Then I should see "seleniumhq.org" within 2 secondThen I close browser
setps.py
from lettuce import * from lettuce_webdriver.util import assert_false from lettuce_webdriver.util import AssertContextManager def input_frame(browser, attribute):xpath = "//input[@id='%s']" % attribute elems = browser.find_elements_by_xpath(xpath) return elems[0] if elems else False def click_button(browser,attribute):xpath = "//input[@id='%s']" % attributeelems = browser.find_elements_by_xpath(xpath)return elems[0] if elems else False#定位输入框输入关键字 @step('I fill in field with id "(.*?)" with "(.*?)"') def baidu_text(step,field_name,value):with AssertContextManager(step): text_field = input_frame(world.browser, field_name) text_field.clear() text_field.send_keys(value)#点击“百度一下”按钮 @step('I click id "(.*?)" with baidu once') def baidu_click(step,field_name):with AssertContextManager(step):click_field = click_button(world.browser,field_name)click_field.click()#关闭浏览器 @step('I close browser') def close_browser(step):world.browser.quit()
注意:@step (‘xxx’)读取了baidu.feature 里有关键字,用正则表达式匹配的。 这是BDD的核心点。理解了这一点,就知道BDD是怎么玩的了。
terrain.py
from lettuce import before, world from selenium import webdriver import lettuce_webdriver.webdriver @before.all def setup_browser(): world.browser = webdriver.Friefox()
terrain文件配置浏览器驱动,可以作用于所有测试用例。
在.../test/目录下输入lettuce命令,运行结果如下图
对于当前测试用例来说,添加新的测试场景也非常简单,我们只用修改baidu.feature即可:
Feature: Go to baidu Scenario: search selenium Given I go to "http://www.baidu.com/" When I fill in field with id "kw1" with "selenium" And I click id "su1" with baidu once Then I should see "seleniumhq.org" within 2 secondScenario: search lettuce_webdriver Given I go to "http://www.baidu.com/" When I fill in field with id "kw1" with "lettuce_webdriver" And I click id "su1" with baidu once Then I should see "pypi.python.org" within 2 secondThen I close browser
运行结果如下:
通过lettuce 来编写自动化脚本将是一件非常有趣的事儿。
转载于:https://www.cnblogs.com/fnng/p/3714394.html
lettuce webdriver 自动化测试---玩转BDD相关推荐
- 使用Cucumber+Rspec玩转BDD(7)——测试重构
使用Cucumber+Rspec玩转BDD(7)--测试重构 2009年4月5日 星期日 ### 温故知新 ### 在前面的六个章节中,我们循序渐进地完善了一个用户帐号系统,这样的系统一般都会作为一个 ...
- WebDriver自动化测试框架详解
webDriver自动化测试框架详解 一. 简介 WebDriver API相对于Selenium Remote Control API来说,虽然同样是控制浏览器,但它的编程接口更加简洁 WebDri ...
- 使用Cucumber+Rspec玩转BDD(2)——邮件激活
使用Cucumber+Rspec玩转BDD(2)--邮件激活 2009年3月2日 星期一 ### 温故知新 ### 前面我们已经完成了新用户注册功能的开发,为了方便我们后面的开发工作且不扰乱之前的工作 ...
- pythonweb自动化测试实例_[转载]python webdriver自动化测试实例
python webdriver自动化测试初步印象 以下示例演示启动firefox,浏览google.com,搜索Cheese,等待搜索结果,然后打印出搜索结果页的标题 from selenium i ...
- webdriver自动化测试工具的使用,将chromedriver配置到path环境变量中,并测试是否成功
文章目录 webdriver概述 安装chrome webdriver 1. 查看自己的chrome谷歌浏览器版本 2. 去国内镜像地址下载对应浏览器版本的webdriver 3. 下载好之后解压,将 ...
- WebDriver自动化测试工具(3)---PhantomJS的使用
PhantomJS是一个基于webkit的javascript API.它使用QtWebKit作为它核心浏览器的功能,使用webkit来编译解释执行JavaScript代码.任何你可以在基于webki ...
- python数据匹配_python 匹配@
正则查找相关的方法 | 手把手教你入门Python之八十五 上一篇:正则表达式简介 | 手把手教你入门Python之八十四下一篇:Re.Match类的介绍 | 手把手教你入门Python之八十六 本文 ...
- python文本匹配_python 匹配@
正则查找相关的方法 | 手把手教你入门Python之八十五 上一篇:正则表达式简介 | 手把手教你入门Python之八十四下一篇:Re.Match类的介绍 | 手把手教你入门Python之八十六 本文 ...
- 【软件测试】自动化测试战零基础教程——Python自动化从入门到实战(完结)
整理不易,希望对各位学习软件测试能带来帮助 软件测试知识持续更新 第十章 行为驱动开发 BDD 框架 lettuce 入门 第一节.安装与例子 安装 例子(阶乘) 第二节.lettuce 解析 第三节 ...
最新文章
- 转载几个和职业建议相关的文章
- 网络信息安全之防火墙的设计 (三)
- C#模板设计模式使用和学习心得
- 基于注解的Spring MVC(所需jar包,web.xml配置,Spring文件配置,@Controller,@RequestMapping,@RequestParam,model填参,EL取值)
- python字符串添加_python字符串的增删改查
- 医院设置(信息学奥赛一本通-T1338)
- 可以参考临摹借鉴的中国风新年海报模板
- VSCode中使用Git忽略提交代码设置
- windows系统下搭建私有nuget仓储服务器, 打包程序集并推送到私有nuget仓储服务器...
- logstash 时间获取失败(yyyy-mm-dd失效)
- 使用sklearn处理经纬度的三种距离计算与地图可视化
- 如何用计算机计时,如何为win7计算机设置计时器关闭
- 步步高彭雄:弄潮“互联网+零售”从端到端流程开始
- 七牛云完整详细配置(从零到一)
- 淘宝宝贝的标题,到底要如何优化,不妨进来看看,也许对你有帮助
- 禁欲28天!一宅男居然肝出如此详细Web安全学习笔记,学妹看完直接抽搐了!(第二弹)
- Soft-NMS – Improving Object Detection With One Line of Code
- VMware虚拟机的使用
- git extensions 设置成中文
- Android阅读器放大镜
热门文章
- 《Linux就该这么学》培训笔记_ch01_部署虚拟环境安装Linux系统
- 补充知识:三元运算和逻辑运算
- 【转】Java基础知识整理
- 韩顺平 servlet 笔记 第5讲
- 第1章 ZLG7290B 简介
- Python实现ANSI文件转UTF-8
- How your face shapes your economic chances
- 2011年中国网络游戏市场实际销售收入达到428.5亿元
- cfnet用于嵌入式_做嵌入式驱动的,你一定要挺住!
- mysql table combine_Mysql系列-性能优化神器EXPLAIN使用介绍及分析