[英]Argument passing using Cucumber between two methods in 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。 下面是我的代碼:
包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”。
提前致謝!
您的代碼:
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的更多信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.