簡體   English   中英

在Java中將Cucumber-jvm步驟獲取為String

[英]Get Cucumber-jvm step as String in java

我用黃瓜寫了以下情況

  Scenario: Correct login should take the user to the next screen
  Given User is on the login screen
  When User enters username as "Donald"
  And User enters password as "Trump"
  And User clicks the login button
  Then User should be taken to the next screen

我在黃瓜jvm中將以下步驟映射到這種情況

 ...
 @Given("^User is on the login screen$)
 public void goToLoginScreen(){
    //some logic
 }

如何在映射的步驟定義方法內的@Given批注之間獲取內容。 我有幾個用例,它們會有所幫助,但無法找出到目前為止的情況。

將整個步驟放在方括號中。 這將是第一個捕獲的組。

@Given("^(User is on the login screen)$)
 public void goToLoginScreen(String stepText){
    System.out.println(stepText);
    //some logic
 }

您可以使用捕獲組來捕獲步驟中的變量。 例如:

 @When("^User enters username as (.*)$")
 public void entersUsernameAs(String username){
    //some logic
 }

您還可以在一個步驟中使用多個變量。 例如:

 @Given("^User logs in as (.*) with password (.*)$")
 public void usersLogsInAs(String username, String password){
    //go to loginpage
    //enter username
    //enter password
    //click login button
 }

您可以使用不同的捕獲組。 我通常將(.*)用作可能為任意值的字符串,並將(\\\\d+)用作整數。 您還可以將(option|other|else)用於有限的一組字符串(在此示例中為"option""other""else" ),這些字符串將僅匹配使用這些字符串的步驟,而不是諸如(.*)類的任何字符串(.*)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM