Cucumber 命令列選項



Cucumber 可用於測試幾乎任何計算機系統。到目前為止,我們已經瞭解瞭如何使用 Eclipse IDE 執行測試。還有一種方法可以透過命令列介面執行 Cucumber 測試。那麼這樣做有什麼好處呢?

從終端執行任何測試框架都有其自身的優勢,例如覆蓋程式碼中提到的執行配置。

為了使用命令提示符執行 Cucumber 測試,請在系統配置後執行以下步驟。

步驟 1 - 建立一個名為 commandLine 的 Maven 測試專案。

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

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

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

  • 單擊完成。

  • 開啟 pom.xml -

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

    • 展開專案 CucumberTest。

    • 找到 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 下建立一個名為“outline”的包

步驟 3 - 建立一個名為“commandLine.feature”的功能檔案。

  • 選擇並右鍵單擊 outline 包。

  • 單擊“新建”檔案。

  • 給檔案命名,例如“commandLine.feature”

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

    功能 - 場景輪廓

    場景輪廓 - 社交網站登入功能。

    給定使用者導航到 Facebook

    當我輸入使用者名稱為“<username>”和密碼為“<password>”時

    然後登入應該失敗

示例

| username  | password  | 
| username1 | password1 | 
| username2 | password2 |

注意 - 在這裡,example 註解描述了在場景執行時要提供的輸入範圍。對於提供的每個輸入,測試場景都將執行。因此,在給定的示例中,測試場景將執行三次。

步驟 4 - 建立一個步驟定義檔案。

  • 選擇並右鍵單擊 outline 包。

  • 單擊“新建”檔案。

  • 將檔案命名為 commandLine.java

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

package Outline;
 
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; i
import cucumber.annotation.en.When; 

public class stepdefinition { 
   WebDriver driver = null; 
	
   @Given("^user navigates to facebook$") 
   public void goToFacebook() { 
      driver = new FirefoxDriver(); 
      driver.navigate().to("https://#/"); 
   } 
	
   @When("^I enter Username as \"([^\"]*)\" and Password as \"([^\"]*)\"$") 
   public void I_enter_Username_as_and_Password_as(String arg1, String arg2) {
      driver.findElement(By.id("email")).sendKeys(arg1);
      driver.findElement(By.id("pass")).sendKeys(arg2);
      driver.findElement(By.id("u_0_v")).click(); 
   } 
	
   @Then("^login should be unsuccessful$") 
   public void validateRelogin() { 
      if(driver.getCurrentUrl().equalsIgnoreCase(
         "https://#/login.php?login_attempt=1&lwv=110")){ 
            System.out.println("Test Pass"); 
      } else { 
         System.out.println("Test Failed"); 
      } 
      driver.close(); 
   } 
}

注意 - 在程式碼中,我們必須定義一個具有兩個輸入引數的函式:一個使用者名稱,另一個用於密碼。因此,對於 example 標籤中提供的每一組輸入,將執行 GIVEN、WHEN 和 THEN 的集合。

步驟 5 - 建立一個執行器類檔案。

  • 選擇並右鍵單擊 outline 包。

  • 單擊“新建”檔案。

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

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

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

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

public class runTest { }
  • 開啟命令提示符。

  • 轉到此包“commandLine”所在的目錄。e:\Workspace\LoginTest\src>cd test\java

  • 執行命令 mvn test:您將看到功能檔案中描述的所有場景都已執行(如果沒有錯誤)。最後,在底部您將找到以下資訊。

結果

This describes the total test run, along with failure if any.

前面的命令執行 JUnit Runner 類中提到的所有內容。但是,如果我們想要覆蓋 Runner 中提到的配置,以下是一些示例。

  • 現在在命令提示符上執行命令 mvn test - Dcucumber.options="--help"。執行此命令將列印所有可用選項。

  • 要僅執行特定標籤,請在命令提示符上執行命令 mvn test -Dcucumber.options="--tags @SmokeTest"。它將只執行標有 @SmokeTest 的標籤。

  • 為了更改結果的格式,請在命令提示符上執行命令 E:\Workspace\LoginTest>mvn test -Dcucumber.options="--plugin junit:target/cucumber-junit-report.xml"。它將報告格式更改為 JUnit 報告生成器。

廣告
© . All rights reserved.