lettuce webdriver 自动化测试---玩转BDD
?
行為驅(qū)動開發(fā)(BDD),依然高大上的矗立在遠(yuǎn)方,很少被人問津,一方面是BDD的思想不太容易理解,別一方面BDD的資料并不多。中文的資料就更少了。
? ? ?之前增寫過一篇《python?BDD?框架之lettuce》?來介紹BDD?,本文將在此基礎(chǔ)上通過lettuce?和webdriver來實現(xiàn)自動化測試,感興趣的測試同學(xué)跟我一起裝X吧!
?
下面向讀者介紹如何通過lettuce?和?webdriver?結(jié)合來編寫自動化腳本。
環(huán)境配置:
------------------------------------------
前提:有python載發(fā)環(huán)境,并且安裝了pip?,強(qiáng)烈建議在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是怎么玩的:
?
創(chuàng)建目錄結(jié)構(gòu)如下:
.../test/features/baidu.feature
? ? ? ? ? /step_definitions/setps.py
? ? ? ? ? /support/terrain.py
先來回顧一下我們?nèi)懓俣人阉髂_本的過程:
功能:訪問百度場景:搜索selenium我去訪問百度的“http://www.badiu.com”通過id 為“kw1”找到輸入框并輸入“selenium”關(guān)鍵字點擊 id為“su1” 按鈕然后,我在搜索結(jié)果上找到了“seleniumhq.com”的網(wǎng)址最后,我關(guān)閉了瀏覽器OK,確定了我們要做事情的過程,下面將其轉(zhuǎn)換成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 browsersetps.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#定位輸入框輸入關(guān)鍵字 @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()#關(guān)閉瀏覽器 @step('I close browser') def close_browser(step):world.browser.quit()注意:@step?(‘xxx’)讀取了baidu.feature?里有關(guān)鍵字,用正則表達(dá)式匹配的。?這是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文件配置瀏覽器驅(qū)動,可以作用于所有測試用例。
在.../test/目錄下輸入lettuce命令,運行結(jié)果如下圖
?
對于當(dāng)前測試用例來說,添加新的測試場景也非常簡單,我們只用修改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運行結(jié)果如下:
?
通過lettuce?來編寫自動化腳本將是一件非常有趣的事兒。
?
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/fnng/p/3714394.html
總結(jié)
以上是生活随笔為你收集整理的lettuce webdriver 自动化测试---玩转BDD的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于8086CPU微处理器的汇编学习之P
- 下一篇: linux 系统出现Give root