日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

lettuce webdriver 自动化测试---玩转BDD

發(fā)布時(shí)間:2025/4/5 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 lettuce webdriver 自动化测试---玩转BDD 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

  行為驅(qū)動(dòng)開發(fā)(BDD),依然高大上的矗立在遠(yuǎn)方,很少被人問(wèn)津,一方面是BDD的思想不太容易理解,別一方面BDD的資料并不多。中文的資料就更少了。

? ? ?之前增寫過(guò)一篇《python?BDD?框架之lettuce》?來(lái)介紹BDD?,本文將在此基礎(chǔ)上通過(guò)lettuce?webdriver來(lái)實(shí)現(xiàn)自動(dòng)化測(cè)試,感興趣的測(cè)試同學(xué)跟我一起裝X吧!

?

下面向讀者介紹如何通過(guò)lettuce?和?webdriver?結(jié)合來(lái)編寫自動(dòng)化腳本。

環(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單元測(cè)試框架,且更容易使用,lettuce_webdriver包的運(yùn)行依賴于nose模塊

https://pypi.python.org/pypi/nose/

root@machine:~$?[sudo]?pip?install?nose

---------------------------------

下面以為百度搜索為例,好吧!誰(shuí)讓這個(gè)例子能簡(jiǎn)單明了的講解問(wèn)題呢。所以,我們?cè)俅我园俣人阉鳛槔?/span>

一般的百度搜索自動(dòng)化腳本是這樣的:

# 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

先來(lái)回顧一下我們?nèi)懓俣人阉髂_本的過(guò)程:

功能:訪問(wèn)百度場(chǎng)景:搜索selenium我去訪問(wèn)百度的“http://www.badiu.com”通過(guò)id 為“kw1”找到輸入框并輸入“selenium”關(guān)鍵字點(diǎn)擊 id為“su1” 按鈕然后,我在搜索結(jié)果上找到了“seleniumhq.com”的網(wǎng)址最后,我關(guān)閉了瀏覽器

OK,確定了我們要做事情的過(guò)程,下面將其轉(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 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#定位輸入框輸入關(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)#點(diǎn)擊“百度一下”按鈕 @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的核心點(diǎn)。理解了這一點(diǎn),就知道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ū)動(dòng),可以作用于所有測(cè)試用例。

.../test/目錄下輸入lettuce命令,運(yùn)行結(jié)果如下圖

?

對(duì)于當(dāng)前測(cè)試用例來(lái)說(shuō),添加新的測(cè)試場(chǎng)景也非常簡(jiǎn)單,我們只用修改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

運(yùn)行結(jié)果如下:

?

通過(guò)lettuce?來(lái)編寫自動(dòng)化腳本將是一件非常有趣的事兒。

?

?

?

?

?

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/fnng/p/3714394.html

總結(jié)

以上是生活随笔為你收集整理的lettuce webdriver 自动化测试---玩转BDD的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。