FitNesse: Is it possible to extract information from the response of a test step, and use this value in the next test step - fitnesse

I am wondering that is there any possibility in FitNesse, while executing the test stages, to get a value from the response of a test stage and use this value in the next test stage.
I am using hsac-fitnesse-fixtures and SOAP web services.
For example, we have 3 test stages, and value from the response of the first stage can automatically transfer to the second stage to get the response of the second stage.
When comparing with SOAP UI we have property transfer.
Example below:
We have request XML:
!define POST_BODY_2 { {{{
<ns1:ZIP>#{zip}</ns1:ZIP>
</s11:Envelope>
}}} }
Stage 1:
|check|xPath|//weather:City/text()|#{City}|
And we get a response XML that contains the city name, as here.
Is it possible to pass this city name as a value to the second test stage?
I.e we have another post XML request !define POST_BODY_3 and to this request can we pass the value (city value) and have the next response XML.
Stage 2 test:
|check |response status|200|

If you are using SLiM as test system, you can use slim symbol
$slimSymbol is a "runtime variable" used in SLiM test system. They are defined by using $slimSymbol= symtax in the test case, and the value will only be available in the runtime. Documentation here
In your case, you are using a decision table in the first test case. So instead of having only one output column, I guess you can do
#some setup here
| send request |
| zip | City? | City? |
| 10007 | New York | $response1= |
| 94102 | San Francisco | $response2= |
And in the later test case, you can refer to the city names using $response1 and $response2. Note that there isn't {} around the variables.

Related

Creating copy of Robot Framework output.xml in Jenkins pipeline

I am in the process of creating a Groovy email template for a Jenkins pipeline running Robot Framework tests. I intend to use Groovy's XMLSlurper to parse the output.xml created by Jenkins to extract the information I need. However, the template also relies on using Robot Publisher which I've now realized automatically deletes the output.xml. I would rather not have to archive the artifacts and access them that way, so is there a way to create a copy of the output.xml in the Jenkins pipeline before the Robot Publisher stage, that will not be deleted by Robot Publisher, that I can parse in my email stage?
Please bear with me as I'm relatively new to Jenkins (and stackoverflow for that matter), so apologies if I've excluded vital information, but any ideas would be much appreciated! Thanks
I would approach your problem from a different angle. First of all I do not suggest using Groovy's XMLSlurper or any other XML parser to extract the information you need from Robot Framework's output.xml.
What you should use is Robot Framework's own API that already implements the parsers you need. You could easily access any information described in the robot.result.model module. You can find everything here, suites, tests and keywords with all thier attributes like, test messages, failure messages, execution times, test results, etc.
All in all this would be the most future proof parsing solution as this parser will always match the version of the framework. Make sure to use the API documentation that matches your current framework version.
Now back to your task, you should utilize the above mentioned API via Robot Framework's listener interface. Implementing the output_file listener method you can access the output.xml (you can even make a copy of it here) file before the Robot Publisher plugin moves the file. The output_file will be automatically called once the output.xml is ready. The method will get the path to the xml file as an input. You can pass this path straight to the ExecutionResult class from the API, then you could "visit" the results by your ResultVisitor and acquire the information needed.
Last step would be to write the data into a file that would serve as an input to your e-mail stage. Note that this file won't be touched by the Robot Publisher by default as it is not a standard output, but a custom you just made using Robot Framework's API.
As it could sound a lot, here is an example to demonstrate the idea. The listener and the result visitor in EmailInputProvider.py:
from robot.api import ExecutionResult, ResultVisitor
class MyTestResultVisitor(ResultVisitor):
def __init__(self):
self.test_results = dict()
def visit_test(self, test):
self.test_results[test.longname] = test.status
class EmailInputProvider:
ROBOT_LISTENER_API_VERSION = 3
def output_file(self, path):
output = 'EmailInput.txt'
visitor = MyTestResultVisitor() # Instantiate result visitor
result = ExecutionResult(path) # Parse up execution result using robot API
result.visit(visitor) # Visit and top level suite to retrive needed metadata
with open(output, 'w') as f: # Write retrived data into a file
for testname, result in visitor.test_results.items():
print(f'{testname} - {result}', file=f)
# You can make a copy of the output.xml here as well
print(f'Email: Input saved into {output}') # Log about custom output to console
globals()[__name__] = EmailInputProvider
This would give the following results for this dummy suite (SO2.robot):
*** Test Cases ***
Test A
No Operation
Test B
No Operation
Test C
No Operation
Test D
No Operation
Test E
No Operation
Test F
Fail
Console output:
$ robot --listener EmailInputProvider SO2.robot
==============================================================================
SO2
==============================================================================
Test A | PASS |
------------------------------------------------------------------------------
Test B | PASS |
------------------------------------------------------------------------------
Test C | PASS |
------------------------------------------------------------------------------
Test D | PASS |
------------------------------------------------------------------------------
Test E | PASS |
------------------------------------------------------------------------------
Test F | FAIL |
AssertionError
------------------------------------------------------------------------------
SO2 | FAIL |
6 critical tests, 5 passed, 1 failed
6 tests total, 5 passed, 1 failed
==============================================================================
Email: Input saved into EmailInput.txt
Output: ..\output.xml
Log: ..\log.html
Report: ..\report.html
Custom output file:
SO2.Test A - PASS
SO2.Test B - PASS
SO2.Test C - PASS
SO2.Test D - PASS
SO2.Test E - PASS
SO2.Test F - FAIL

FitNesse: Is it required to write fixtures to add and use variables on test page

I am trying to add a table of variables and its values in FitNesse suite page, so that it can be used for all my tests.
I am using xmlHtttp tests for SOAP web services and fhoeben/hsac-fitnesse-fixtures (slim) for this.
Is it required to write separate fixtures to add a table?
Yes you can.
Using a scenario allows us to generate multiple request, only changing certain values.
!*> Scenario definition
!define POST_BODY_2 { {{{
<s11:Envelope xmlns:s11="http://schemas.xmlsoap.org/soap/envelope/">
<s11:Body>
<ns1:GetCityWeatherByZIP xmlns:ns1="http://ws.cdyne.com/WeatherWS/">
<ns1:ZIP>#{zip}</ns1:ZIP>
</ns1:GetCityWeatherByZIP>
</s11:Body>
</s11:Envelope>
}}} }
|script|xml http test|
|table template |send request |
|post |${POST_BODY_2} |to |${URL} |
|check |response status|200 |
|show |response |
|register prefix|weather |for namespace|http://ws.cdyne.com/WeatherWS/|
|$City= |xPath |//weather:City/text() |
*!
|send request |
|zip |City? |
|10007|New York |
|94102|San Francisco|
In this example the variable zip is used so the request is sent with either 10007 or 94102.

How to change Get-Mailbox output

I apologize in advance as I am still fairly new to powershell. I'm figuring things out as I go, but this specific issue is stumping me. Currently this is with powershell 2.0 on exchange 2007.
I am trying to add to a script that I have been writing up that shows the basic information for our exchange accounts. This is just a small tool to be introduced to our help desk to assist in a quick overview of what is going on with a user's account. I have everything working, however, I want to change what is displayed. For example, I have:
Get-Mailbox $username | ft #{label="Hidden from GAL"; expression= {$_.HiddenFromAddressListsEnabled}}, #{label="Restricted Delivery"; expression={$_.AcceptMessagesOnlyFrom}} -auto | out-string;
Which ends up returning true/false for hidden from address list, but for Accept Messages, if it is disabled, it returns "{}" (without quotes). If it is enabled, it displays the full group name (along the lines of admin.local/groupname). I only want to change {} to disabled, and instead of showing the group name, just show "enabled"
I tried an if/then statement, and then tried putting the variable "messRestrict" in the expression for accept messages above, and then the function name, but neither worked. They just returned blank values or always said "true." Here is the function:
function restricted {
$accept = Get-Mailbox $username | AcceptMessagesOnlyFrom | select -expand Priority
#if ($accept -match "\s")
#{$messRestrict="False"};
#else
#{$messRestrict="True"};
}
The output is the standard Get-Mailbox output, I just want to replace what it says under the header.
Thanks!
You can try this :
#{label="Restricted Delivery"; expression={if($_.AcceptMessagesOnlyFrom){"Enabled"}else{"Disabled"}}}
It gives :
Get-Mailbox $username | ft #{label="Hidden from GAL"; expression= {$_.HiddenFromAddressListsEnabled}}, #{label="Restricted Delivery"; expression={if($_.AcceptMessagesOnlyFrom){"Enabled"}else{"Disabled"}}} -auto | out-string;

Re-using Python's behave feature file for both Unit and Functional tests

The following Gherking test defines the desired behaviour for one of my servers:
Scenario Outline: Calling the server with a valid JSON
Given A GIS update server
When I call /update with the json in the file <filename>
Then the response status code is <status_code>
And the response is a valid JSON
And the response JSON contains the key status
And the response JSON contains the key timestamp
And the response JSON contains the key validity
Examples:
| filename | status_code |
| valid_minimal.json | 200 |
| valid_minimal_minified.json | 200 |
| valid_full.json | 200 |
| valid_full_minified.json | 200 |
| valid_full_some_nulls.json | 200 |
| valid_full_all_nulls.json | 200 |
I wrote this code for unit testing a Flask server. The steps file, which interpret the Gherkin directives, open a test client and make the necessary calls and assertions:
#given(u'A GIS update server')
def step_impl(context):
context.app = application.test_client()
The feature file is similar for unit and functional tests. The only difference is in a few steps file which would have to make HTTP calls rather than calling the test client's methods.
What's the right way to re-use this behave feature file by passing parameters to the steps file?
Expanding on Parva's comments, I suggest sending parameters via the command line which will be detected in your step definitions and adjust behaviour for unit vs functional testing (the decision of whether you separate your unit and functional tests for clarity is up to you ;).
There is an example given in the Behave documentation around Debug on error, which gives a good example of using environmental attributes in modifying the execution of your steps method:
# -- FILE: features/environment.py
# USE: BEHAVE_DEBUG_ON_ERROR=yes (to enable debug-on-error)
from distutils.util import strtobool as _bool
import os
BEHAVE_DEBUG_ON_ERROR = _bool(os.environ.get("BEHAVE_DEBUG_ON_ERROR", "no"))
def after_step(context, step):
if BEHAVE_DEBUG_ON_ERROR and step.status == "failed":
# -- ENTER DEBUGGER: Zoom in on failure location.
# NOTE: Use IPython debugger, same for pdb (basic python debugger).
import ipdb
ipdb.post_mortem(step.exc_traceback)
You may change that to detect a command line passed variable such as UNIT_TESTING and have it hit a different endpoint or perform alternate functionality for your tests.
REQUIRES: behave >= 1.2.5
I think, the test stage concept should help you with your needs. It allows you to use different step implementations for different test stages.
behave --stage=functional
If your changes are minor, use the userdata concept to pass a flag to your step implementation, like:
behave -D test_stage=unit …
behave -D test_stage=functional …

How do I compare xml output in a Cucumber step using a multiline string example?

Chargify has this Cucumber scenario in their docs.
Scenario: Retrieve a customer via my reference id (as an integer or simple string)
Given I have a customer with these attributes
| reference | first_name | last_name | email |
| 7890 | Joe | Blow | joe#example.com |
When I send a GET request to https://[#subdomain].chargify.com/customers/lookup.xml?reference=7890
Then the response status should be "200 OK"
And the response should be the xml:
"""
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<id type="integer">`auto generated`</id>
<first_name>Joe</first_name>
<last_name>Blow</last_name>
<email>joe#example.com</email>
<organization>`your value`</organization>
<reference>7890</reference>
<created_at type="datetime">`auto generated`</created_at>
<updated_at type="datetime">`auto generated`</updated_at>
</customer>
"""
I'm trying to follow their approach in testing an API we're developing here, instead of checking for the tags one by one, like I we were doing before coming across this example.
So far no luck matching the response's output with the step's multiline string, due to formatting issues. Haven't found a way with Nokogiri nor with simple stripped string comparison.
Is there an elegant (and efficient) way to do this?
Update
Here's the cucumber step using Mark's solution:
Then /^I should see the following output$/ do |xml_output|
response = Hash.from_xml(page.body)
expected = Hash.from_xml(xml_output)
expected.diff(response).should == {}
end
You can use Hash.from_xml and then compare with Hash.diff. Comparing as hashes eliminates insignificant whitespace from messing up comparisons.
In case you don't want the dependency on ActiveSuport (for instance, if you are outside Rails), you might try the equivalent-xml gem. It allows you to write the above expectation as follows:
response = page.body
response.should be_equivalent_to(xml_output)

Resources