Java中两个方法之间使用Cucumber传递参数

时间:2018-12-05 00:40:50

标签: java selenium-webdriver cucumber-java

我是Cucumber的新手,我很难理解如何在两种方法之间传递数据。我一直在阅读有关数据表的信息,但我仅看到有关如何使用功能表中已列出的数据的示例。当我运行代码时,出现错误:

Step [^Send Results$] is defined with 3 parameters at 'cucumebr.test.addResult(int,int,String,Integer>>) in file:/Users/lcren1026/eclipse-workspace/cucumebr/target/classes/'.However, the gherkin step has 0 arguments.

我想做的是使用Selenium中收集的数据在两个方法之间传递Arraylist中的Map。下面是我的代码:

package cucumebr;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import testrail.APIException;
import org.json.simple.parser.ParseException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver; 
import cucumber.api.java.en.Then;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.When;

public class test
{       
    public static WebDriver driver; 
    String FFDriverDirectory = "directory";
    String FFDriverEXE = "webdriver.gecko.driver";
    ArrayList Results = new ArrayList<>();  
    @Given("^Open the browser$")

public void openBrowser() throws IOException, APIException, InterruptedException
    {
        System.setProperty(FFDriverEXE, FFDriverDirectory);
        driver = new FirefoxDriver();

        driver.navigate().to("https://www.google.com/");
        Thread.sleep(5000);
    }

    @When("^verify logo$")
    public void verifyLogo() throws IOException, APIException, ParseException
    {
        if(driver.findElement(By.xpath("//*[@id=\"hplogo\"]")).isDisplayed()) {
            addResult(120254, 1, Results);

        } else
        {
            addResult(120254, 5, Results);
        }
    }

    @Then("^verify btn$")
        public void verifyBtn() throws IOException, APIException, ParseException
        {
            if(driver.findElement(By.xpath("//*[@id=\"tsf\"]/div[2]/div/div[3]/center/input[1]")).isDisplayed()) {
                addResult(120255, 1, Results);

            } else
            {
                addResult(120255, 5, Results);
            }   
    }

        @Then("^Send Results$")
        public  void addResult(int testCaseId, int status, ArrayList<Map<String, Integer>> newResults
                ) throws IOException,  ParseException { 

                int count = testCaseId;
                if(testCaseId == count) {
                    Map myTestResults= new HashMap<String, Integer>(){{put("suite", testCaseId); put("milestone", 179);  put("status", status);}};     
                    System.out.println(myTestResults);
                    newResults.add(myTestResults);   
                    count++ ; 
                }
                System.out.println( newResults);            
        }   


    @Then("^Print Results$")
      public static void PrintResultForTestCase( ArrayList<Map<String, Integer>> newResults ) throws IOException, APIException {

        System.out.println("This is the final result " + newResults);   
        }}

以下是功能:

功能:谷歌

Scenario: Driver works
Given Open the browser  
When verify logo
Then verify btn
Then Send Results
Then Print Results

数据是ArrayList> newResults,方法是“ addResult”和“ PrintResultForTestCase”。

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码:

public  void addResult(int testCaseId, int status, ArrayList<Map<String, Integer>> newResults))

需要三个参数,但是您的特征文件没有任何参数。

以下是示例如何使用参数(数字,文本或表格)

示例场景:

  Scenario: Test Scenario
    Given Number parameter $1
    And Text parameter "text"
    And Table example
      | Team name | Number of members |
      | team1     | 2                 |
      | team2     | 5                 |

步骤:

@Given("^Number parameter \\$(\\d+)$")
public void numberParameter(int number)
{
    System.out.println("Number from step is: " + number);
}

@And("^Text parameter \"([^\"]*)\"$")
public void textParameter(String text)
{
    System.out.println("Text from step is: " + text);
}

@And("^Table example$")
public void tableExample(DataTable table)
{
    List<List<String>> data = table.raw();
    // iterate through 'data' here to access rows in table
}

详细了解DataTable here