Как заставить Serenity заключить значение параметра в тег <pre>? - PullRequest
3 голосов
/ 19 марта 2019

Есть ли способ заставить Serenity заключить значение параметра в

tag?</p>

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

<pre><code>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      |
</code>

Спасибо!

1 Ответ

0 голосов
/ 02 апреля 2019

Поскольку я не смог найти какой-либо прямой предварительный тег для заполнения ответа,

Обнаружил обходной путь для заполнения ответа в соответствующей истории:

Ниже приведен фрагмент, которыйможет помочь вам.

Ответ получен, а формат данных построен динамически, а затем записан в файле истории.

ExampleSteps.java - Файл шагов, в котором есть тестовый пример и жестко закодированный ответ, из которого собирались построить таблицу.

Файл 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

Более того: Ответ может быть записан в любом месте файла.

...