Cucumber Java:如何动态添加(或减去,相乘)先前存储的变量?

时间:2018-04-04 19:59:36

标签: java cucumber cucumber-jvm cucumber-java

下面,我将四个数字存储到变量中,在最后一步中,我想确保总数等于count1到count3的总和。如何编写它以便无论" count"我输入的变量,我可以使用一个单步定义?

Given User logs into "AppA" application of Application
When user gets count from "TotalLines" and stores into "$TotalRecords"
And user gets count from "Car1" and stores into "$Count1"
And user gets count from "Car2" and stores into "$Count2"
And user gets count from "Car3" and stores into "$Count3"
Then user validates that "$TotalRecords" is equal to the sum of "$Count1", "$Count2", and "$Count3"

3 个答案:

答案 0 :(得分:0)

制作花车的arraylist并将其命名为TotalRecords。

现在每当你在count变量中添加car的值时;而不是将其添加到计数变量;您只需将其直接存储在ArrayList中即可。

现在终于可以通过使用以下函数找到存储在TotalRecords arraylist中的所有值的总和:

ArrayList<float> TotalRecords = new ArrayList<float>();
TotalRecords.add(carValue1);
TotalRecords.add(carValue2);
TotalRecords.add(carValue3);
TotalRecords.add(carValue4);
.
.
.
//now to add all the elements ; do this
public float Total(){
float sum = 0;
for(int i = 0; i < TotalRecords.size(); i++){
   sum+= TotalRecords.get(i);

 }
return sum;}

答案 1 :(得分:0)

将计数值存储在List中,或者如果需要,还可以保留&#39; Car&#39;零件存储在Map

将计数行的方案修改为

And user gets count from "Car1"
And user gets count from "Car2"
And user gets count from "Car3"

在步骤定义类

中添加以下内容
   List<Integer> counts = new ArrayList<>();

    @When("^user gets count from \"([^\"]*)\"$")
    public void userGetsCountFromAndStores(String arg) {
        //Get count from the argument passed
        counts.add(countvalue);
    }

在最后一步中,您可以添加计数并进行比较。

答案 2 :(得分:0)

您可以使用DataTable:

When user gets the following variables:
  | count  | variable  |
  | "Car1" | "$Count1" |
  | "Car2" | "$Count2" |
  | "Car3" | "$Count3" |

在步骤定义中,您可以使用DataTable作为参数,也可以使用类型的列表或地图。