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

标签 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。下面是我的代码:

封装 cucumber ;

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”。

提前致谢!

最佳答案

您的代码:

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的信息.

关于java - 在 Java 中使用 Cucumber 在两个方法之间传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53623527/

相关文章:

java - cucumber Java : How to force JSON plugin generation

escaping - Cucumber 步骤定义中的转义字符

java - 为什么 Dagger 2 会强制我使用 @Provides 而不是 @Binds

javascript - 将 null 从 JavaScript 传递到 Java 时,JxBrowser 中出现 NullPointerException

java - Selenium 测试用例在每种情况下都通过,无论元素是否出现在页面上

ruby - 你如何使用页面对象自动确认 ruby​​ 中的模态?

java - 在 MacOS Mojave 上安装 pymssql 时出现问题

Java 使用 wamp 服务器在 java 中编辑数据库

java - 无法通过任何对象定位器单击按钮?

cucumber - 如何在不运行测试的情况下获取所有标签和 cucumber 场景