Adding HTTP-headers to ALL test-cases of a test-suite (or several test-suites)?

Originally posted by mmoser18 on May 19, 2016

We had to change the authentication mechanism for several web-applications that we developed. That means that for all our test-cases I now need to include a set of additional HTTP-headers to each and every call. Since our Test-suite covers a couple of hundreds of test cases adding these into each and every request seems like quite a monstrous task!

And there isn’t even some copy-paste mechanism, i.e. I would have to clicke-di-click myself through the UI for a thousand times? NO way!

Is there a way to add common HTTP-headers (e.g. something like:

 "Authorization: ${#Project#Access\_Token}"

to ALL requests (or “Steps”) of a test suite?

Ideally one wouldn’t even have to add that to each individual request but there would be a common place to define those in single place and these headers would then be injected into all requests (visible maybe only in the RAW view).

Second-best solution would be to at least script the addition (and update/deletion) of those headers so one doesn’t have to do that all in the GUI.

Are there any such helpers available?

Originally posted by nmrao on May 19, 2016

Access_token is something pre-defined fixed value or it should be fetched using a service call? Then add that to all the further service calls?

Originally posted by mmoser18 on May 21, 2016

Access_token is a project-level custom property. It is always fetched and updated in a test-case at the very begin of my test-suites and is then valid and used during the entire execution of that test suite.

Originally posted by nmrao on May 22, 2016

So, as you already mentioned, you need to get it and save it as Project level custom property say ACCESS_TOKEN.

Then, In your case there are two options.

Either you need to add the predefined Header property using property expansion to all the required steps say ${#Project#ACCESS_TOKEN}. So, yes, the header needs to be to added to all existing test requests and for the new test requests as well which you will be adding in the future.

Or add ACCESS_TOKEN as header for each out going request programatically. Not only that, but also requires a feature called Events (which is part of pro edition) or an extension for Open source edition mentioned here.

Originally posted by nmrao on May 22, 2016

Action you needed here is “Before Submitting Each Request”

Originally posted by mmoser18 on May 31, 2016

Hi Rao

thanks for pointing me to this solution! I am now trying to implement this.

What I am currently stuck with is: How do I set/add HTTP-headers to a request?

My RequestSteoBeforeSubmit.groovy file will need to reads something like this:

testStep.httpRequest.setHeader('Authorization','Bearer ${#Project#Mashery_Access_Token}')
testStep.httpRequest.setHeader('X-Mashery-Host','${#Project#target_address}')
testStep.httpRequest.setHeader('X-Mashery-Service-Key','foobar')

I have two questions:

  1. I wrote here "…httpRequest.setHeader(‘key’, ‘value’) but I don’t even know, whether httpHeader provides such a method.Is there some documentation on these “inner” types (like testSuite, testCase, testStep, httpRequest, etc.) to find out, what fields and methods they support?

  2. Some of the values would first have to be expanded (like in the above example the “${#Project#Mashery_Access_Token}” and “${#Project#target_address}”). I assume that this listener is called *after* properties-expansion has already taken place. If so: how would I expand such a property here in the listener before assigning it? Is that possible via the same

value = context.expand("${some-property-name}")

as one would do in a scripted-assertion?

Regards,

Michael

Originally posted by nmrao on May 31, 2016

#1:
header value is a list. You can find my example in the below link where there is a separate method is defined to set the header including its value. you just need to call it as you needed.
http://stackoverflow.com/questions/37412281/script-assertion-to-get-token-and-set-it-as-http-header-to-the-next-test-step?noredirect=1#comment62336002_37412281

#2:
Yes, use context.expand(‘..’) to have the actual value.

Good luck.

Originally posted by mmoser18 on May 31, 2016

Hi Rao

thanks for your hints and pointers so far! Sorry for continue to pester you, but I am afraid, I haven’t yet fully understood, how these listeners are supposed to work. What I did so far:

  1. I added SoapUIExtListeners.jar to the C:\Program Files\SmartBear\SoapUI-5.2.1\bin\ext directory.
  2. I created a directory C:\Program Files\SmartBear\SoapUI-5.2.1\bin\listeners into which I placed the file custom-listeners.xml.
  3. I added all the groovy scripts (FilterRequest.groovy, …, TestSuiteTestStepAdded.groovy) that I found into directory C:\Program Files\SmartBear\SoapUI-5.2.1\bin\scripts

I would have expected that when I now fire a request that I would now find log-entries from these miscellaneous .groovy files in the soapui.log (since most .groovy files contain a log-statement emitting their name), but not so.

What I see instead is:

…

2016-06-01 00:51:15,637 INFO [RequestStepSubmitListener] Dummy script being run, implement required script
2016-06-01 01:04:39,744 INFO [RequestStepSubmitListener] Dummy script being run, implement required script
2016-06-01 01:04:39,747 INFO [RequestFilter] Setting submitContext, request, and log variables
2016-06-01 01:04:39,759 INFO [RequestFilter] Dummy script being run, implement required script
2016-06-01 01:04:39,892 INFO [RequestFilter] Setting submitContext, request, and log variables
2016-06-01 01:04:39,906 INFO [RequestFilter] Dummy script being run, implement required script

…

So: how do I “activate” these listeners? I am obviously missing something here…

Specifically that listener for “Before Submitting Each Request” that you mention: I didn’t find any file with that name - I guess you mean “RequestStepBeforeSubmit.groovy”? That’s at least the closest match that I found. How do I get that triggered/called?

Regards,

Michael

Originally posted by nmrao on May 31, 2016

Right, the script content you wrote should goes into the file called ‘RequestStepBeforeSubmit.groovy’ only. And place it in the scripts directory. The behaviour you observing is expected because there may not have any statements to execute or there could not be any file with such name, that is why it running default script when it can’t find such file with that name.

Also you must have condition in your case at least, so that those headers are not added to the request step which fetches the token itself.

For eg: condition can be such as if test step name is not getAccessToken then only add headers.

Originally posted by mmoser18 on June 01, 2016

And place it in the scripts directory.

I did! I tried both places mentioned in the description of this extension feeature:

C:\Program Files\SmartBear\SoapUI-5.2.1\bin\scripts

as well as:

C:\Program Files\SmartBear\SoapUI-5.2.1\scripts

but I get no output in the log!

I would expect something because all those .groovy scripts contain at least a line like:

log.info ‘Implement Request Step Before Submit’

echoing essentially their name. But I don’t see *any* of those names, just these messages:

“Dummy script being run, implement required script”

Aaahhh! Now I think I found something relevant!

I first didn’t pay attention to these stacktraces (thinking that they were caused by something else), but it actually seems that soapUI (or probably the extension in SoapUIExtListeners.jar) seems to have an issue locating certain classes. Could that be the reason that these custom-listeners are not executed?

2016-06-01 17:41:11,247 INFO  [SoapUI] Adding [C:\Program Files\SmartBear\SoapUI-5.2.1\bin\ext\pattern_match.jar] to extensions classpath
2016-06-01 17:41:11,247 INFO  [SoapUI] Adding [C:\Program Files\SmartBear\SoapUI-5.2.1\bin\ext\SoapUIExtListeners.jar] to extensions classpath
2016-06-01 17:41:11,250 INFO  [DefaultSoapUICore] initialized soapui-settings from [C:\Users\moserm\soapui-settings.xml]
2016-06-01 17:41:11,253 INFO  [DefaultSoapUICore] Adding listeners from [C:\Program Files\SmartBear\SoapUI-5.2.1\bin\listeners\custom-listeners.xml]
2016-06-01 17:41:11,258 INFO  [DefaultSoapUICore] Adding listener [class org.nmrao.soapuiext.impl.CaseRunListener]
2016-06-01 17:41:11,262 INFO  [DefaultSoapUICore] Adding listener [class org.nmrao.soapuiext.impl.ProjectRunListener]
2016-06-01 17:41:11,265 INFO  [DefaultSoapUICore] Adding listener [class org.nmrao.soapuiext.impl.RequestStepSubmitListener]
2016-06-01 17:41:11,268 INFO  [DefaultSoapUICore] Adding listener [class org.nmrao.soapuiext.impl.SuiteListener]
2016-06-01 17:41:11,272 INFO  [DefaultSoapUICore] Adding listener [class org.nmrao.soapuiext.impl.SuiteRunListener]
2016-06-01 17:41:11,275 INFO  [DefaultSoapUICore] Adding listener [class org.nmrao.soapuiext.impl.InterfaceListener]
2016-06-01 17:41:11,278 INFO  [DefaultSoapUICore] Adding listener [class org.nmrao.soapuiext.impl.RequestFilter]
2016-06-01 17:41:11,854 INFO  [PluginManager] Adding plugin from [C:\Users\moserm\.soapuios\plugins\soapui-swagger-plugin-2.2-dist.jar]
2016-06-01 17:41:11,854 INFO  [PluginManager] Adding plugin from [C:\Users\moserm\.soapuios\plugins\ready-uxm-plugin-1.0.1-dist.jar]
2016-06-01 17:41:12,380 ERROR [SoapUI] An error occurred [com.eviware.soapui.plugins.auto.factories.AutoDiscoveryMethodFactory], see error log for details
2016-06-01 17:41:12,380 ERROR [errorlog] java.lang.ClassNotFoundException: com.eviware.soapui.plugins.auto.factories.AutoDiscoveryMethodFactory
java.lang.ClassNotFoundException: com.eviware.soapui.plugins.auto.factories.AutoDiscoveryMethodFactory
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at com.eviware.soapui.plugins.LoaderBase.loadAutoFactories(LoaderBase.java:96)
	at com.eviware.soapui.plugins.LoaderBase.loadFactories(LoaderBase.java:64)
	at com.eviware.soapui.plugins.PluginLoader.loadPluginFactories(PluginLoader.java:129)
	at com.eviware.soapui.plugins.PluginLoader.loadPlugin(PluginLoader.java:92)
	at com.eviware.soapui.plugins.PluginLoader.loadPlugin(PluginLoader.java:42)
	at com.eviware.soapui.plugins.PluginManager.doInstallPlugin(PluginManager.java:136)
	at com.eviware.soapui.plugins.PluginManager.access$600(PluginManager.java:38)
	at com.eviware.soapui.plugins.PluginManager$LoadPluginsTask.computeSequentially(PluginManager.java:415)
	at com.eviware.soapui.plugins.PluginManager$LoadPluginsTask.compute(PluginManager.java:372)
	at com.eviware.soapui.plugins.PluginManager$LoadPluginsTask.compute(PluginManager.java:360)
	at java.util.concurrent.RecursiveTask.exec(Unknown Source)
	at java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
	at java.util.concurrent.ForkJoinWorkerThread.execTask(Unknown Source)
	at java.util.concurrent.ForkJoinPool.scan(Unknown Source)
	at java.util.concurrent.ForkJoinPool.work(Unknown Source)
	at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
2016-06-01 17:41:12,381 ERROR [SoapUI] An error occurred [com.eviware.soapui.plugins.auto.factories.AutoImportMethodFactory], see error log for details
2016-06-01 17:41:12,381 ERROR [errorlog] java.lang.ClassNotFoundException: com.eviware.soapui.plugins.auto.factories.AutoImportMethodFactory
java.lang.ClassNotFoundException: com.eviware.soapui.plugins.auto.factories.AutoImportMethodFactory
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Unknown Source)
	at com.eviware.soapui.plugins.LoaderBase.loadAutoFactories(LoaderBase.java:96)
	at com.eviware.soapui.plugins.LoaderBase.loadFactories(LoaderBase.java:64)
	at com.eviware.soapui.plugins.PluginLoader.loadPluginFactories(PluginLoader.java:129)
	at com.eviware.soapui.plugins.PluginLoader.loadPlugin(PluginLoader.java:92)
	at com.eviware.soapui.plugins.PluginLoader.loadPlugin(PluginLoader.java:42)
	at com.eviware.soapui.plugins.PluginManager.doInstallPlugin(PluginManager.java:136)
	at com.eviware.soapui.plugins.PluginManager.access$600(PluginManager.java:38)
	at com.eviware.soapui.plugins.PluginManager$LoadPluginsTask.computeSequentially(PluginManager.java:415)
	at com.eviware.soapui.plugins.PluginManager$LoadPluginsTask.compute(PluginManager.java:372)
	at com.eviware.soapui.plugins.PluginManager$LoadPluginsTask.compute(PluginManager.java:360)
	at java.util.concurrent.RecursiveTask.exec(Unknown Source)
	at java.util.concurrent.ForkJoinTask.doExec(Unknown Source)
	at java.util.concurrent.ForkJoinWorkerThread.execTask(Unknown Source)
	at java.util.concurrent.ForkJoinPool.scan(Unknown Source)
	at java.util.concurrent.ForkJoinPool.work(Unknown Source)
	at java.util.concurrent.ForkJoinWorkerThread.run(Unknown Source)
...
these two exceptions repeat several times, but are too long to append in this forum.
...
2016-06-01 17:41:13,082 INFO  [PluginManager] 4 plugins loaded in 1228 ms
2016-06-01 17:41:13,082 INFO  [DefaultSoapUICore] All plugins loaded
2016-06-01 17:41:14,017 INFO  [WorkspaceImpl] Loading workspace from [C:\Users\moserm\default-soapui-workspace.xml]
2016-06-01 17:41:14,736 INFO  [WsdlProject] Loaded project from [file:/W:/LHAPI_Tests/LHAPI-Tests-v3-0-0-soapui-project.xml]
2016-06-01 17:41:14,900 INFO  [SoapUI] Used java version: 1.7.0_55
2016-06-01 17:42:26,145 INFO  [RequestStepSubmitListener] Dummy script being run, implement required script
2016-06-01 17:42:26,169 INFO  [RequestFilter] Setting submitContext, request, and log variables
2016-06-01 17:42:26,178 INFO  [RequestFilter] Dummy script being run, implement required script
2016-06-01 17:42:33,471 INFO  [RequestFilter] Setting submitContext, request, and log variables
2016-06-01 17:42:33,479 INFO  [RequestFilter] Dummy script being run, implement required script
2016-06-01 17:42:34,399 INFO  [AbstractHttpRequestDesktopPanel] Got response for [ReferenceData - Countries.countries?lang:countries (xml)] in 7285ms (182888 bytes)
2016-06-01 17:42:34,416 INFO  [RequestStepSubmitListener] Dummy script being run, implement required script

Originally posted by mmoser18 on June 01, 2016

Nope - I just googled for that exception and found:

http://stackoverflow.com/questions/31409653/classnotfoundexception-after-updating-to-soapui-5-2-0

As described there I moved the content of <user_home>/.soapuios to <soapui_root>/bin and those exceptions went away.

But I am still not getting any output from the listeners!

Regards,

Michael

BTW: I agree with the opinion voiced there that putting such extensions into <user_home>/.soapuios is not such a good idea…

Originally posted by mmoser18 on June 01, 2016

I am an idiot! I forgot to set the SOAPUI_HOME variable! Even though it says loud and clear in the first line of your description:

set SOAPUI_HOME environment variable...

I had falsely assumed that my link that I use to start up soapUI was calling the soapui.bat file (which internally sets that env-var, so I thought I can safely omit that step), but the link actually starts “C:\Program Files\SmartBear\SoapUI-5.2.1\bin\SoapUI-5.2.1.exe” and then this env-var is NOT being set!

I now get an exception from my own groovy script, but that’s a good sign! It means that it is being called now! :wink:

Regards and thanks for you patience and apologies for the bandwidth consumed!

Michael

Originally posted by mmoser18 on June 01, 2016

This works just wonderfully! I am delighted!

A short piece of code instead of having to manually add two additional headers to 300+ test steps via the GUI!

This is soooo groovy! :wink:

For the sake of other that may find this thread (and in case I have to ever search it again myself):

Obviously the hook is called BEFORE variable expansion! I.e. one doesn’t even have to fiddle with code like:

headers['header-name'] = [context.expand('${#Project#some_property}')]

but can directly just write:

headers['header-name'] = ['${#Project#some_property}']

The only thing I had to learn is to surround the value with ‘[’ and ‘]’.

Originally posted by nmrao on June 01, 2016

First of all I would like to appreciate all of your efforts to make it work and thank you for that.

Glad to know that it is useful for you and it will be helpful for many.

By the way, if you see the github, it clearly says that it is developed and tested on version 4.5.1.

By the way, I see you are using it for 5.2.1. And do you have to make any other changes than mentioned in the read me?If so, would you mind list them, so that some one can easily follow and it is done?

I wish if you can add the contents of your script for others for future references.

Appreciate once again your efforts and trying all sorts of stuff and finish in style.

Originally posted by nmrao on June 01, 2016

Mostly, i use soapui zip installer without java which will reduce the size of the file and do not have install the zip installer, instead just extract it. And use soapui.bat commandline (from any path as I put %SOAPUI_HOME%/bin in %PATH% env. variable)