Cucumber - 註解



註解 (Annotation) 是預定義的文字,具有特定含義。它讓編譯器/直譯器知道在執行時應該做什麼。Cucumber 有以下幾種註解:

  • Given -

    • 它描述了執行測試的先決條件。

    • 示例 - GIVEN 我是一個 Facebook 使用者

  • When -

    • 它定義了任何測試場景執行的觸發點。

    • 示例 - WHEN 我輸入 "<username>"

  • Then -

    • Then 包含要執行測試的預期結果。

    • 示例 - THEN 登入應該成功。

  • And -

    • 它提供任何兩個語句之間的邏輯 AND 條件。AND 可以與 GIVEN、WHEN 和 THEN 語句結合使用。

    • 示例 - WHEN 我輸入我的 "<username>" AND 我輸入我的 "<password>"

  • But -

    • 它表示任何兩個語句之間的邏輯 OR 條件。OR 可以與 GIVEN、WHEN 和 THEN 語句結合使用。

    • 示例 - THEN 登入應該成功。BUT 首頁不應該丟失。

  • Scenario -

    • 在關鍵字“Scenario:”之後需要捕獲被測場景的詳細資訊。

    • 示例 -

      Scenario

      GIVEN 我是一個 Facebook 使用者

      WHEN 我輸入我的

      AND 我輸入我的

      THEN 登入應該成功。

      BUT 首頁不應該丟失。

  • Scenario Outline - (稍後講解)

  • Examples - (稍後講解)

  • Background -

    • Background 通常包含在每個場景執行之前要設定的指令。但是,它在“Before” hook (稍後講解) 之後執行。因此,當我們想要設定 web 瀏覽器或建立資料庫連線時,這非常適合用於程式碼。

      • 示例 -

        Background

        轉到 Facebook 首頁。

示例場景

讓我們自動化一個場景以便更好地理解註解。

步驟 1

建立一個名為 AnnotationTest 的 Maven 測試專案。

  • 轉到 檔案 → 新建 → 其他 → Maven → Maven 專案 → 下一步。

  • 提供 group Id(group Id 將唯一標識您在所有專案中的專案)。

  • 提供 artifact Id(artifact Id 是 jar 的名稱,不包含版本號。您可以選擇任何小寫的名稱)。

  • 單擊完成。

  • 開啟 pom.xml -

    • 轉到 Eclipse 左側的包資源管理器。

    • 展開 AnnotationTest 專案。

    • 找到 pom.xml 檔案。

    • 右鍵單擊並選擇選項“使用文字編輯器開啟”。

  • 新增 Selenium 的依賴項 - 這將指示 Maven 從中央儲存庫下載哪些 Selenium jar 檔案到本地儲存庫。

    • 開啟 pom.xml 並處於編輯模式,在 project 標籤內建立 dependencies 標籤 (<dependencies></dependencies>)。

    • 在 dependencies 標籤內,建立 dependency 標籤 (<dependency></dependency>)。

    • 在 dependency 標籤內提供以下資訊。

<dependency> 
   <groupId>org.seleniumhq.selenium</groupId> 
   <artifactId>selenium-java</artifactId> 
   <version>2.47.1</version> 
</dependency>
  • 新增 Cucumber-Java 的依賴項 - 這將指示 Maven 從中央儲存庫下載哪些 Cucumber 檔案到本地儲存庫。

    • 建立另一個 dependency 標籤。

    • 在 dependency 標籤內提供以下資訊。

<dependency> 
   <groupId>info.cukes</groupId> 
   <artifactId>cucumber-java</artifactId> 
   <version>1.0.2</version> 
   <scope>test</scope> 
</dependency>
  • 新增 Cucumber-JUnit 的依賴項 - 這將指示 Maven 從中央儲存庫下載哪些 Cucumber JUnit 檔案到本地儲存庫。

    • 建立另一個 dependency 標籤。

    • 在 dependency 標籤內提供以下資訊。

<dependency> 
   <groupId>info.cukes</groupId> 
   <artifactId>cucumber-junit</artifactId> 
   <version>1.0.2</version> 
   <scope>test</scope> 
</dependency>
  • 新增 JUnit 的依賴項 - 這將指示 Maven 從中央儲存庫下載哪些 JUnit 檔案到本地儲存庫。

    • 建立另一個 dependency 標籤。

    • 在 dependency 標籤內提供以下資訊。

<dependency> 
   <groupId>junit</groupId> 
   <artifactId>junit</artifactId> 
   <version>4.10</version> 
   <scope>test</scope> 
</dependency>
  • 驗證二進位制檔案。

    • 成功編輯 pom.xml 後,儲存它。

    • 轉到 專案 → 清理 - 這需要幾分鐘。

步驟 2

src/test/java 下建立一個名為 Annotation 的包

  • 選擇新建立的專案。

  • 右鍵單擊並選擇“新建”。

  • 選擇“包”選項。

  • 將其命名為“Annotation”。

  • 儲存。

步驟 3

建立一個名為 annotation.feature 的特性檔案。

  • 選擇並右鍵單擊包大綱。

  • 單擊“新建檔案”。

  • 為檔案命名,例如 outline.feature

  • 在檔案中寫入以下文字並儲存。

Feature: annotation 
#This is how background can be used to eliminate duplicate steps 

Background: 
   User navigates to Facebook Given 
   I am on Facebook login page 

#Scenario with AND 
Scenario: 
   When I enter username as "TOM"
   And I enter password as "JERRY" 
   Then Login should fail 

#Scenario with BUT 
Scenario: 
   When I enter username as "TOM" 
   And I enter password as "JERRY" 
   Then Login should fail 
   But Relogin option should be available

步驟 4

建立一個步驟定義檔案。

  • 選擇並右鍵單擊包大綱。

  • 單擊“新建檔案”。

  • 將檔名命名為 annotation.java

  • 在檔案中寫入以下文字並儲存。

package Annotation; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 

import cucumber.annotation.en.Given; 
import cucumber.annotation.en.Then; 
import cucumber.annotation.en.When; 

public class annotation { 
   WebDriver driver = null; 
   @Given("^I am on Facebook login page$") 
	
   public void goToFacebook() { 
      driver = new FirefoxDriver(); 
      driver.navigate().to("https://#/"); 
   }
	
   @When("^I enter username as \"(.*)\"$") 
   public void enterUsername(String arg1) {   
      driver.findElement(By.id("email")).sendKeys(arg1); 
   }
	
   @When ("^I enter password as \"(.*)\"$") 
   public void enterPassword(String arg1) {
      driver.findElement(By.id("pass")).sendKeys(arg1);
      driver.findElement(By.id("u_0_v")).click(); 
   } 
	
   @Then("^Login should fail$") 
   public void checkFail() {  
      if(driver.getCurrentUrl().equalsIgnoreCase(
         "https://#/login.php?login_attempt=1&lwv=110")){ 
            System.out.println("Test1 Pass"); 
      } else { 
         System.out.println("Test1 Failed"); 
      } 
      driver.close(); 
   }
	
   @Then("^Relogin option should be available$") 
   public void checkRelogin() { 
      if(driver.getCurrentUrl().equalsIgnoreCase(
         "https://#/login.php?login_attempt=1&lwv=110")){ 
            System.out.println("Test2 Pass"); 
      } else { 
         System.out.println("Test2 Failed"); 
      } 
      driver.close(); 
   }
} 

步驟 5

建立一個執行器類檔案。

  • 選擇並右鍵單擊包大綱。

  • 單擊“新建檔案”。

  • 為檔案命名,例如 runTest.java

  • 在檔案中寫入以下文字並儲存。

package Annotation; 

import org.junit.runner.RunWith; 
import cucumber.junit.Cucumber; 

@RunWith(Cucumber.class) 
@Cucumber.Options(format = {"pretty", "html:target/cucumber"}) 

public class runTest { }

步驟 6

使用以下選項執行測試:

  • 從包資源管理器中選擇 runTest.java 檔案。

  • 右鍵單擊並選擇“執行方式”選項。

  • 選擇 JUnit 測試。

執行此類檔案時,您將觀察到以下情況:

  • Facebook 在新的 Firefox 瀏覽器例項中開啟。

  • TOM 將作為輸入傳遞給使用者名稱欄位。

  • JERRY 將作為輸入傳遞給密碼欄位。

  • 將點選登入。

  • 瀏覽器上將顯示有關登入失敗的訊息。

  • 在控制檯中,您將看到列印“測試透過”。

  • 步驟結果 1 到 5 將針對使用者名稱為 "" 和密碼為 "" 的情況重新執行。

廣告
© . All rights reserved.