行为驱动开发(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 webdriver browser = 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 second
Then 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']" % attribute
elems = 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 second Scenario: 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 second
Then I close browser

运行结果如下:

通过lettuce 来编写自动化脚本将是一件非常有趣的事儿。

lettuce webdriver 自动化测试---玩转BDD的更多相关文章

  1. Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考

    Selenium 2.0 WebDriver 自动化测试 使用教程 实例教程 API快速参考 //System.setProperty("webdriver.firefox.bin" ...

  2. 转来的——python webdriver自动化测试初步印象——转来的

    python webdriver自动化测试初步印象 以下示例演示启动firefox,浏览google.com,搜索Cheese,等待搜索结果,然后打印出搜索结果页的标题 from selenium i ...

  3. AutoIt实现Webdriver自动化测试文件上传

    在运用WebDriver进行自动化测试时,由于WebDriver自身的限制,对于上传文件时Windows弹出的文件选择窗口无法控制,通过在网上查找资料锁定使用AutoIt来控制文件上传窗口. Auto ...

  4. Python WebDriver自动化测试

    转载来自: http://www.cnblogs.com/fnng/p/3160606.html Webdriver Selenium 是 ThroughtWorks 一个强大的基于浏览器的开源自动化 ...

  5. selenium webdriver自动化测试

    selenium家族介绍           Selenium IDE:Selenium IDE是嵌入到Firefox浏览器中的一个插件,实现简单的浏览器操作的录制与回放功能.   Selenium ...

  6. 关于Webdriver自动化测试时,页面数据与数据库id不一致的处理方式,需要使用鼠标事件

    有时候Web页面需要通过onmouseout事件去动态的获取数据库的数据,在使用Webdriver进行自动化测试的时候,对于页面显示的数据,其在数据库可能会存在一个id或者code,但是id或者cod ...

  7. WebDriver自动化测试工具(1)---环境搭建

    Webdriver是一个前端自动化测试工具,可以模拟用户点击链接,填写表单,点击按钮等操作,下面介绍其使用 一.下载WebdriverC#类库以及对应浏览器驱动 http://www.selenium ...

  8. python webdriver 自动化测试练习 1-- 在线调查

    __author__ = 'Mickey0s' # coding:utf8 from selenium import webdriver from selenium.webdriver.common. ...

  9. Selenium Webdriver 自动化测试开发常见问题(C#版)

    转一篇文章,有修改,出处http://www.7dtest.com/site/blog-2880-203.html 1:Selenium中对浏览器的操作 首先生成一个Web对象 IWebDriver ...

随机推荐

  1. raspberry pi 如何汉化显示中文

    1 树莓派初装系统之后,首次启动会出现“raspi-config”工具,如下图:(若不是初次启动,在命令模式下,请输入 sudo raspi-config 命令,即可调出此界面.若在图形桌面下,打开桌 ...

  2. (转)unity中基于alpha通道的shadow volume实现

    转自:http://blog.163.com/wmk_2000_ren/blog/static/138846192201019114117466/ 实现呢,Aras大神已经给出了, http://fo ...

  3. OpenCV相机标定和姿态更新

    原帖地址: http://blog.csdn.net/aptx704610875/article/details/48914043 http://blog.csdn.net/aptx704610875 ...

  4. The web application [] appears to have started a thread named [Abandoned connection cleanup thread] com.mysql.jdbc.AbandonedConnectionCleanupThread

    01-Jul-2016 14:25:30.937 WARNING [localhost-startStop-1] org.apache.catalina.loader.WebappClassLoade ...

  5. Java的自动装箱和拆箱的简单讲解

     装箱就是把基础类型封装成一个类.比如把int封装成Integer,这时你就不能把他当成一个数了,而是一个类了,对他的操作就需要用它的方法了. 拆箱就是把类转换成基础类型.比如你算个加法什么的是不能用 ...

  6. Windows共享内存示例

    共享内存主要是通过映射机制实现的. Windows 下进程的地址空间在逻辑上是相互隔离的,但在物理上却是重叠的.所谓的重叠是指同一块内存区域可能被多个进程同时使用.当调用 CreateFileMapp ...

  7. Scala 深入浅出实战经典 第47讲:Scala多重界定代码实战及其在Spark中的应用

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  8. 最近的shell脚本(updating)

    1.批量替换 sed -i 's/class="table"/class="table table-hover"/g' `grep 'class="t ...

  9. AWVS漏洞测试-03节-添加扫描项目

    http://localhost:9660 我们要扫描这个页面 点击左上角的New Scan,在Scan Single哪里输入要扫描的网站地址,可以是本地地址 然后选择下一步 Next 这里我们可以配 ...

  10. BTrace入门教程

    bin版:https://kenai.com/projects/btrace/downloads/directory/releases 源码:https://github.com/btraceio/b ...