gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:00:07 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com