4 Persisting Dictionaries In Pages - essenius/FitNesseFitSharpSupportFunctions GitHub Wiki

Quite frequently there are use cases where it is needed to save data in a FitNesse session to be able to use it in a following session. For example, when testing a batch process that creates an account, you want to be able to run a test the next day to check whether that account exists. However, to be able to run that test the next day, you will need access to the account number that today's test generates.

Of course, this is somewhat of a slippery slope. The premise of automated testing is having an environment that is the same every time you run the tests, and with a data persistence function in your test cases, you introduce variation – with the possibility that things start failing because you use different data. On the other hand, in practice teams don't always have a test environment at their disposal that is fully under their control.

So, having the capability to load and save data can make sense, if applied carefully. It would be nice if you could create an initial data set on your FitNesse page. What seems to be a useful mechanism is loading and saving dictionaries, i.e. key-value pairs. We do that in such a way that FitNesse could also use it as a decision table. The Dictionary Library fixture does just that. Here is an example of a data table it can generate:

!|Dictionary|Having|Name|Main|
|Key|Value|
|name|bob|
|address|there|
|phone|12345|
|employed|true|
|date|2016-08-13 17:04:43|

We define a table here called Main. In that, we define 5 key-value pairs: name=bob, address=there, etc. Now let's show how we could consume such a table in a test case:

!|script   |dictionary library                            |
|page root |http://localhost:${FITNESSE_PORT}/${PAGE_PATH}|
|load table|main       |from page      |${PAGE_NAME}      |
|check     |count      |5                                 |
|check     |get        |name           |=~/b.b/           |
|check     |get        |address        |there             |
|$phone=   |get        |phone                             |
|check     |echo       |$phone         |12344<_<12346     |
|ensure    |get        |employed                          |

The fixture that provides the dictionary capability is called Dictionary Library. The Page Root command defines the root page for the tables. This implies that you can have multiple pages with data under the same root if you want more structure than a single page. Then, Load Table asks the fixture to load the table into memory so we can start querying it. The Get command retrieves a value corresponding to a given key.

Check out how the page root and load table command use predefined variables to identify the right port and page name.

Now we change the phone number and save that to the page:

!|script                              |
|set       |phone|to     |2345        |
|save table|Main |to page|${PAGE_NAME}|

After running the test and reloading the data page, we will see that the table has changed:

!|Dictionary|Having|Name|Main|
|Key|Value|
|name|bob|
|address|there|
|phone|2345|
|employed|true|
|date|2016-08-13 17:04:43|

So now we saw how to create a data table, how to consume its data, and how to alter the data table. It makes the most sense to put these data tables on separate pages so they do not interfere with the tests. If you put such a data table on a test page, FitNesse would try to execute the table:

Dictionary Execute Exception

It would throw exceptions because the Dictionary fixture does not exist. However, putting the table on your test page also enables you to execute scenarios for each key-value pair. Define the scenario header as follows:

|scenario|Dictionary _ _ _|Name, Key, Value|

This will match the table because it matches the name Dictionary and it has 3 parameters: one table parameter called Name and 2 column parameters called Key and Value. Just defining this one-line scenario will make FitNesse ignore the page. It will execute the scenario for each row, but the scenario doesn’t do anything:

Ignored Dictionary

Here is the full FitNesse source:

|Library     |
|Echo Support|

We use this page to grab a table from, just as demo. 

!|script   |dictionary library                            |
|page root |http://localhost:${FITNESSE_PORT}/${PAGE_PATH}|
|load table|main       |from page      |${PAGE_NAME}      |
|check     |count      |5                                 |
|check     |get        |name           |=~/b.b/           |
|check     |get        |address        |there             |
|$phone=   |get        |phone                             |
|check     |echo       |$phone         |12344<_<12346     |
|ensure    |get        |employed                          |

!|script                              |
|set       |phone|to     |2345        |
|save table|Main |to page|${PAGE_NAME}|

----
Here is where the test ends. Below this is only the test data table

Note that this test will fail the next time it is run, because the page has changed
This also shows why it is kind of tricky to use this functionality

Introduce an empty scenario so the tables don't break the test run

|scenario|Dictionary _ _ _|Name, Key, Value|

This is the table to be captured. 

!|Dictionary|Having|Name|Main|
|Key|Value|
|name|bob|
|address|there|
|phone|2345|
|employed|true|
|date|2016-08-13 17:04:43|