在 Java 中的两个方法之间使用 Cucumber 传递参数
Argument passing using Cucumber between two methods in Java
我是 Cucumber 的新手,我很难理解如何在两种方法之间传递数据。我一直在阅读有关数据 table 的信息,但我只看到有关如何使用已在 table 功能中列出的数据的示例。当我 运行 我的代码出现错误时:
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。下面是我的代码:
包 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);
}}
这是功能:
特征:google
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))
需要三个参数,但是你的特征文件没有。
这是一个如何使用参数(数字、文本或 table)的示例
示例场景:
Scenario: Test Scenario
Given Number parameter
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。
我是 Cucumber 的新手,我很难理解如何在两种方法之间传递数据。我一直在阅读有关数据 table 的信息,但我只看到有关如何使用已在 table 功能中列出的数据的示例。当我 运行 我的代码出现错误时:
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。下面是我的代码:
包 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);
}}
这是功能:
特征:google
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))
需要三个参数,但是你的特征文件没有。
这是一个如何使用参数(数字、文本或 table)的示例
示例场景:
Scenario: Test Scenario
Given Number parameter
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。