lettuce webdriver 自動化測試---玩轉BDD

來源:互聯網
上載者:User

標籤:style   blog   class   code   java   ext   

 

  行為驅動開發(BDD),依然高大上的矗立在遠方,很少被人問津,一方面是BDD的思想不太容易理解,別一方面BDD的資料並不多。中文的資料就更少了。

     之前增寫過一篇《python BDD 架構之lettuce》 來介紹BDD ,本文將在此基礎上通過lettuce 和webdriver來實現自動化測試,感興趣的測試同學跟我一起裝X吧!

 

下面向讀者介紹如何通過lettuce 和 webdriver 結合來編寫自動化指令碼。

環境配置:

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

前提:有python載發環境,並且安裝了pip ,強烈建議在linux下操作。

第一步,安裝lettuce 

[email protected]:~$ [sudo] pip install lettuce

第二步,安裝lettuce_webdriver

https://pypi.python.org/pypi/lettuce_webdriver

[email protected]:~$ [sudo] pip install lettuce_webdriver

第三步,安裝nose

nose繼承自unittest,屬於第三方的python單元測試架構,且更容易使用,lettuce_webdriver包的運行依賴於nose模組

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

[email protected]:~$ [sudo] pip install nose

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

下面以為百度搜尋為例,好吧!誰讓這個例子能簡單明了的講解問題呢。所以,我們再次以百度搜尋為例。

一般的百度搜尋自動化指令碼是這樣的:

# coding = utf-8from 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 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 裡有關鍵字,用Regex匹配的。 這是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 second     Then I close browser

運行結果如下:

 

通過lettuce 來編寫自動化指令碼將是一件非常有趣的事兒。

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.