如何使Serenity将参数值包含在<pre> tag?

时间:2019-03-19 14:37:38

标签: java jbehave serenity-bdd

Is there a way to make Serenity enclose a parameter value within a <pre> tag?

I need the posted data =(When step) to be correctly displayed in the report (spaces and new lines)

Given that API is up
When the following data is posted:
xxxx             xxxxx              xxxxx
xxxxxx           xxxxxxxxxx         

                 xxxxxxx
Then the entry is updated the following data:
|     status      |    entry_id    |
|   PROCESSED     |      xxxx      |

Thanks!

1 个答案:

答案 0 :(得分:0)

由于我无法找到任何直接的前置标签来填充响应,

找到了一种解决方法来填充相应故事中的响应:

下面是可以帮助您的代码段。

检索响应,并动态构建数据格式,然后将其写入故事文件。

ExampleSteps.java-具有测试用例和硬编码响应的Steps文件,通过它们将构建表。

TextUtils.java文件,它将有助于在特定位置读取和写入响应。

TablePrinter.java文件,它将在响应数据的帮助下创建类似于表格的表

import org.example.selenide.TablePrinter;
import org.example.selenide.TextUtils;
import org.jbehave.core.annotations.Alias;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Named;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.jbehave.core.steps.Steps;

    public class ExampleSteps extends Steps {
      private int x;

      @Given("a variable x with value $value")
      @Alias("a parameter x with value $value")
      public void givenXValue(@Named("value") int value) {
        x = value;
      }

      @When("I multiply x by $value")
      public void whenImultiplyXBy(@Named("value") int value) {
        x = x * value;
      }

      @Then("x should equal $value")
      public void thenXshouldBe(@Named("value") int value) {
        if (value != x) {
          throw new RuntimeException("x is " + x + ", but should be " + value);
        }
        String replaceText = "When I multiply x by " + 2;
        // Assume that this is the response and we are going to build a table format data using the response
        Object[][] data = { { "angles", 75, 5, 10 }, { "meters", 30, 3, 12 } };
        String replaceContends = TablePrinter.getContends(data);
        TextUtils.replaceText(replaceText, replaceText + "\n" + replaceContends);
      }
    }

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

public class TextUtils {

  public static String replaceText(String findText, String replaceText) {
    String filePath = "FILE PATH OF STORY";
    try {
      ArrayList<String> outputcontendsList = new ArrayList<String>();
      try (BufferedReader bufferInstance = new BufferedReader(new FileReader(filePath))) {
        String currentLine = null;
        while ((currentLine = bufferInstance.readLine()) != null) {
          if (currentLine.equalsIgnoreCase(findText)) {
            outputcontendsList.add(replaceText);
          } else {
            outputcontendsList.add(currentLine);
          }
        }
      }
      FileWriter fileWriterInstance = new FileWriter(filePath);
      for (String str : outputcontendsList) {
        fileWriterInstance.write(str);
        fileWriterInstance.write("\n");
      }
      fileWriterInstance.close();
    } catch (Exception exceptionInstance) {
      exceptionInstance.printStackTrace();
    }
    return null;
  }

}

public class TablePrinter {

  public static interface RowPrinter {
    String print(int rowIndex, int dataIndex, Object[][] data);
  }

  public static String getContends(Object[][] data) {
    //Object[][] data = { { "angles", 75, 5, 10 }, { "meters", 30, 3, 12 } };
    String outputContends = printTable(data, new RowPrinter() {
      @Override
      public String print(int rowIndex, int dataIndex, Object[][] data) {
        int max = (int) data[dataIndex][1];
        int factor = (int) data[dataIndex][2];
        int offset = (int) data[dataIndex][3];
        return String.format(" | %8d", calculate(rowIndex, max, factor, offset));
      }
    });
    return outputContends;
  }

  public static String printTable(Object[][] data, RowPrinter rowPrinter) {
    StringBuilder outputString = new StringBuilder();
    outputString.append(String.format("%8s", "index"));
    for (int i = 0; i < data.length; i++) {
      outputString.append(String.format(" | %8s", data[i][0]));
    }
    outputString.append("\n");
    for (int i = 0; i < 75; i++) {
      outputString.append(String.format("%8d", i + 1));
      for (int j = 0; j < data.length; j++) {
        outputString.append(rowPrinter.print(i, j, data));
      }
      outputString.append("\n");
    }
    return outputString.toString();
  }

  public static int calculate(int index, int max, int factor, int offset) {
    return ((index * factor) % (max - offset + factor)) + offset;
  }

}

带有输出的文件:example_story.story

Narrative:
In order to develop an application that requires a stack efficiently
As a development team
I would like to use an interface and implementation in Java directly

Scenario: 2 squared

Given a variable x with value 2
When I multiply x by 2
   index |   angles |   meters
       1 |       10 |       12
       2 |       15 |       15
       3 |       20 |       18
       4 |       25 |       21
       5 |       30 |       24
       6 |       35 |       27
       7 |       40 |       30
       8 |       45 |       12
       9 |       50 |       15
      10 |       55 |       18
      11 |       60 |       21
      12 |       65 |       24
      13 |       70 |       27
      14 |       75 |       30
      15 |       10 |       12
      16 |       15 |       15
      17 |       20 |       18
      18 |       25 |       21
      19 |       30 |       24
      20 |       35 |       27
      21 |       40 |       30
      22 |       45 |       12
      23 |       50 |       15
      24 |       55 |       18
      25 |       60 |       21
      26 |       65 |       24
      27 |       70 |       27
      28 |       75 |       30
      29 |       10 |       12
      30 |       15 |       15
      31 |       20 |       18
      32 |       25 |       21
      33 |       30 |       24
      34 |       35 |       27
      35 |       40 |       30
      36 |       45 |       12
      37 |       50 |       15
      38 |       55 |       18
      39 |       60 |       21
      40 |       65 |       24
      41 |       70 |       27
      42 |       75 |       30
      43 |       10 |       12
      44 |       15 |       15
      45 |       20 |       18
      46 |       25 |       21
      47 |       30 |       24
      48 |       35 |       27
      49 |       40 |       30
      50 |       45 |       12
      51 |       50 |       15
      52 |       55 |       18
      53 |       60 |       21
      54 |       65 |       24
      55 |       70 |       27
      56 |       75 |       30
      57 |       10 |       12
      58 |       15 |       15
      59 |       20 |       18
      60 |       25 |       21
      61 |       30 |       24
      62 |       35 |       27
      63 |       40 |       30
      64 |       45 |       12
      65 |       50 |       15
      66 |       55 |       18
      67 |       60 |       21
      68 |       65 |       24
      69 |       70 |       27
      70 |       75 |       30
      71 |       10 |       12
      72 |       15 |       15
      73 |       20 |       18
      74 |       25 |       21
      75 |       30 |       24

Then x should equal 4

Scenario: 3 squared

Given a variable x with value 3
When I multiply x by 3
Then x should equal 9

此外:响应可以写在文件中的任何位置。