cucumber+selenium自動化測試(一)

1.1 BDD介紹

行爲驅動開發(Behavior Driven Development,BDD)簡歷在測試驅動開發的基礎上,並且優化了很多TDD實踐者的良好習慣。BDD可以通過自然語言來描寫自動化測試,增加自動化的可閱讀性.

1.2 cucumber原理

reshen目前有很多BDD的框架,ruby中的cucumber、Python中的Behave、Lettuce以及Freshen等,對於java語言,可以使用cucumber的java把版本。

cucumber是BDD比較著名的框架,官網地址(https://cucumber.io/).cucumber是一個命令行工具。運行時會從普通語言編寫的稱爲特性(feature)的文件裏面讀取說明,接卸需要的場景(scenario),然後執行這些場景達到測試目的。每個場景由步驟(step)組成,cucumber會一步一步執行這些步驟。

image

cucumber測試棧(來自cucumber:行爲驅動指南)

1.3 cucumber關鍵字介紹

cucumber使用Gherkin語言進行編寫,它可以描寫需求、系統設計和模塊等的行爲。它可以作爲自動化驗收測試,自動化系統測試,自動化繼承設計。行爲描述和步驟定義是cucumber程序中重要的組成部分。

1.3.1 行爲描述

我們來看下官網的一張圖:

image

在官網的事例中:我們可以看到綠色的幾個字段,這就是我們需要了解的關鍵字。

用戶行爲描述在.feature文件中,包含以下元素:

Featur(特性):一個feature代表一個功能,相當於testsuit。

Scenario(場景):用於描述一個用例,相當於test case。

step,包含Given,When,Then這些詞,用來定義場景的步驟,對於這些詞可以個男實際中根據語義使用。

Given(給):給定場景所需要的環境,一個前置的條件。

When(當):一個用戶事件,比如點擊,輸入等。

Then(則):定義驗證結果,平常測試中的驗證點,斷言。

@tag:註解(Annotation)

將cucumber跟junit3對應關係

|feature    |test suit    |

|scenario   |test case   |

|given        |setup        |

|when         |test          |

|then           |assert       |

|@tag         |@tag        |

1.3.2 步驟定義

用戶行爲完成後就要開始進行步驟定義,步驟定義可以理解爲用代碼來完成各個步驟的執行的動作,完整的一個步驟定義包含:註解,方法和實現過程。

我們來看下官網的一個例子:

行爲描述:

<iframe id="code-iframe76" frameborder="0" data-code-lang="plain" data-code-str="Feature: Is it Friday yet?
Everybody wants to know when it&#39;s Friday

Scenario: Sunday isn&#39;t Friday
Given today is Sunday
When I ask whether it&#39;s Friday yet
Then I should be told &quot;Nope&quot;" data-start-line="1" src="https://yuedu.baidu.com/static/bookeditor/js/spublishUeditor/dialogs/insertcode/insertcode.html?code-iframe76" class="code-iframe" style="width: 675px; margin: 10px 0px; display: block; height: 209px;"></iframe>

步驟定義:

<iframe id="code-iframe77" frameborder="0" data-code-lang="plain" data-code-str="@Given(&quot;^today is Sunday$&quot;)
public void today_is_Sunday() {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

@When(&quot;^I ask whether it&#39;s Friday yet$&quot;)
public void i_ask_whether_is_s_Friday_yet() {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}

@Then(&quot;^I should be told &amp;quot;([^&amp;quot;]*)&amp;quot;$&quot;)
public void i_should_be_told(String arg1) {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}" data-start-line="1" src="https://yuedu.baidu.com/static/bookeditor/js/spublishUeditor/dialogs/insertcode/insertcode.html?code-iframe77" class="code-iframe" style="width: 675px; margin: 10px 0px; display: block; height: 479px;"></iframe>

說明:步驟定義中通常包含正則表達式,比如given,通常以^匹配開始$匹配結束,當匹配到這個行爲後,將會執行today_is_Sunday()方法。

百度閱讀地址:https://yuedu.baidu.com/ebook/a097d5ffd4bbfd0a79563c1ec5da50e2524dd1dc

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章