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 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
我去访问百度的“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
运行结果如下:
使用强大的 Mockito 测试框架来测试你的代码 本文讲的是使用强大的 Mockito 测试框架来测试你的代码,单元测试的思路是在不涉及依赖关系的情况下测试代码(隔离性),所以测试代码与其他类或者系统的关系应该尽量被消除。一个可行的消除方法是替换掉依赖类(测试替换),也就是说我们可以使用替身来替换掉真正的依赖对象。
相关文章
- 腾讯WeTest即将亮相MTSC 2022中国互联网测试开发大会
- 2022年第 12期《python接口web自动化+测试开发》课程,9月17号开学!
- 技术分享 | 接口自动化测试如何进行认证?
- Spartan-6开发案例使用手册——嵌入式AD模块/接口基础测试(上)
- 某远控软件渗透测试辅助工具/使用
- 在培训机构也学不到的Robot Framework自动化测试企业级实战教程
- 黑群晖引导文件grub.cfg中参数对硬盘顺序的测试与分析
- springboot测试中主程序中能加载bean测试中无法加载bean
- 基于Sikuli GUI图像识别框架的PC客户端自动化测试实践
- 移动端APP自动化测试框架-UiAutomator2基础
- python自动化测试—Python自动化框架及工具
- 自动化测试如何区分用例集合
- 测试之路 pytest接口自动化框架扩展-MS数据解析
- 【图文教程】怎么使用postman测试文件上传
- 前端自动化测试
- 接口测试第3讲:Python测试框架——pytest
- 接口测试第8讲:基于unittest框架requests自动化测试上
- 接口测试第10讲:基pytest框架requests自动化测试
- app自动化测试之Appium问题分析及定位
- API接口自动化测试框架搭建之需求整理、详细设计和框架设计
- 接口自动化测试多场景实践 - 上篇
- 2023年十款开源测试开发工具推荐(自动化、性能、造数据、流量复制)
- 腾讯出品小程序自动化测试框架【Minium】系列(五)API详解(中)
- 微服务API测试的十大最佳技巧(API测试技巧)
- 软件测试|web自动化测试神器playwright教程(六)
- 深入测试Oracle,实现全面性安全(oracle测试)
- Linux ATS:极致性能的自动化测试引擎(linuxats)
- Twitter宣布推出面向商户的导购模块 正进行早期测试
- 为什么测试对云原生流水线不再足够
- 使用Linux测试磁盘IO,优化系统性能(linux测试磁盘io)
- Mysql性能测试:一款可靠的自动化工具.(mysql性能测试软件)
- App测试技术利用Mysql助力质量保障(app测试mysql)
- 战华为压DoCoMo,软银携中兴意展开4.5GHz的5G外场测试