3

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!

Nix
  • 176
  • 1
  • 10
  • What code have you tried and what is the current actual output? – Bill Hileman Mar 28 '19 at 19:48
  • Actually, what I like display correctly is the data of the When step. The data has characters ( multiple consecutive spaces and newlines) that are not correctly rendered in HTML unless enclosed within a pre tag. – Nix Apr 07 '19 at 21:48

1 Answers1

0

As i could not find any direct pre tag in order to populate the response ,

Found out a workaround to populate the response in the respective story:

Here below is the snippet which would help you .

The response is retrieved and the data format is build dynamically and then written in the story file.

ExampleSteps.java - Steps file which has the test case and the hard coded response through which were going to build the table.

TextUtils.java file which will help to read and write the response at the specific position.

TablePrinter.java File which will create a table like structure with the help of response data

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;
  }

}

File with output : 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

Moreover : The response can be written in any position in the file.

redhatvicky
  • 1,912
  • 9
  • 8
  • Actually, what I like display correctly is the data of the When step. The data has characters ( multiple consecutive spaces and new lines) that are not correctly rendered in HTML unless enclosed within a pre tag. – Nix Apr 07 '19 at 21:46