關於cucumber的feature里用法的一些總結


@RunWith(Cucumber.class)

Cucumber will default to looking for feature files under the same package as RunCucksTest. You can also change the location(s) it will look for feature files by supplying the "features" option with the @CucumberOptions annotation (in RunCucksTest). 

 

2.1 Feature 簡述

         Feature可以定義為項目的獨立單元或功能。舉一個很常見的社交網站的例子。此產品/項目的功能如何?幾個基本特征可以確定為:從社交網站創建和刪除用戶、社交網站的用戶登錄功能、在社交網站上分享照片或視頻、發送朋友請求、注銷

         到目前為止,當談論Cucumber時,被測產品的每個獨立功能都可以被稱為一個Feature。這是最佳實踐,當開始測試,在導出測試腳本之前,應該確定要測試的功能。

         Feature通常包含要為該Feature測試的場景列表。用戶存儲功能的文件,要測試的功能和場景的描述稱為功能文件。

         Gherkins中的測試功能的關鍵字是“Feature”。建議的最佳做法是,在feature文件中的feature標題下面寫入feature的小描述。這將滿足良好的文檔的需要。

2.2 Feature Files

         寫入Cucumber測試的文件稱為Featurefiles。對於每個被測功能,建議應該有一個單獨的feature file。feature file的擴展名必須為“.feature”。可以根據需要創建任意數量的feature file。為了具有有組織的結構,每個feature應當具有一個feature file。例如:

        

用於feature的名稱,featurefile的命名約定取決於個人的選擇。在Cucumber中沒有關於名字的基本規則。一個簡單的feature file由以下關鍵字/部分組成:

Feature:待測功能的名稱。

Description(可選):描述測試中的功能。

Scenario:什么是測試場景。

Given:在執行測試步驟之前的先決條件。

When:為了執行下一步驟,應該匹配的特定條件。

Then:如果滿足WHEN中提到的條件,應該會發生什么。

 

* AND關鍵字是用於顯示兩個條件之間的連接詞。AND可用於任何其他關鍵字,如GIVENWHENTHEN

         在Feature file中沒有寫入邏輯細節。

2.3 Steps定義

         已經准備好定義的測試場景的FeatureFile。然而,工作沒有完成,Cucumber並不知道如何執行Feature File中描述特定場景所對應的代碼。

         這就需要一個中間步驟---Step定義文件。Step定義文件存儲在FeatureFile定義場景的每個step映射上,而該Feature File帶有可執行的功能代碼。所以,當Cucumber執行Feature File中提到的場景Step時,它掃描Step定義文件並計算出要調用哪個函數。

Step定義文件的例子

 

[java]  view plain  copy
 
  1. public void goToCsdn() {   
  2. driver = new FirefoxDriver();  
  3.  driver.navigate().to("https://passport.csdn.net/account/login?ref=toolbar");  
  4.  }   
  5.  @When "^user logs in using Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$"   
  6.  public void I_enter_Username_as_and_Password_as(String arg1, String arg2) {   
  7.  driver.findElement(By.id("username")).sendKeys(arg1);   
  8.  driver.findElement(By.id("password")).sendKeys(arg2);   
  9.  driver.findElement(By.className("logging")).click();   
  10.  }   
  11.  @Then"^login should be unsuccessful$"   
  12.  public void validateRelogin() {   
  13.  if(driver.getCurrentUrl().equalsIgnoreCase("http://my.csdn.net/my/mycsdn")){   
  14.  System.out.println("Test Pass");   
  15.  }   
  16.  else {  
  17.  System.out.println("Test Failed");   
  18.  }   
  19.  driver.close();   
  20.  }<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">   </strong>  

 

         因此,對於每個函數,想要執行每個測試Step(即GIVEN / THEN / WHEN)中的任何代碼,都可以在Step定義文件中寫入。確保已經為每個步驟定義了代碼/函數。這個函數可以是Java函數,該函數中使用Java和Selenium命令來自動化的測試步驟。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM