How can I reference a TestCase property from an AMF request TestStep script in soapUI? - properties

I have a Test Case called "testCaseOne"
It contains three Test Steps:
"AMFrequestOne"
"propertyTransfer"
"AMFrequestTwo"
"AMFrequestOne" creates a database object.
"propertyTransfer" sends the ResponseAsXml to a temporary property in "testCaseOne" called "tempProp".
I need to reference "tempProp" in a script inside of "AMFrequestTwo"
I've tried the following
def temp = testRunner.testCase.getPropertyValue( "tempProp" )
but I get the error "No such property: testRunner for class: Script6" (number increments with tries)
Is this because in an AMF request "Script is invoked with log, context, parameters and amfHeaders variables" and testRunner is not recognized?
I know it seems odd, but is it possible to do this? I'm unable to use a specific xpath property transfer between the two AMF Requests as it's possible for the structure to change and I'm not always looking for the same node.

Used
def temp = context.testCase.getPropertyValue( "tempProp" )
instead of
def temp = testRunner.testCase.getPropertyValue( "tempProp" )
and this works fine.

Related

How to validate API response against multiple instance of data base from feature file in Karate API automation?

I have developed a script which executes against one DB instance e.g.: db1. The code to connect to DB is written in Background section. Now what i want to do is, i have to execute same test script against diffrent db instance e.g.:db2
Feature:Execution against multiple DB instance.
##############################################
Background:
* def db_properties = {db_username,db_password,db_connection_string,driver}
* def createConnection = path to read .java file
* def readFromDB = new createConnection(db_properties)
##############################################
In * def db_properties, i have hard coded the actual values of username, password, conenction string and driver.What exactly i want to do is, i have to validate my API response agains't another DB instance e.g. build is deployed in another environment, and db properties which i have mentioned is diffrent environment. How can i do it?
This has nothing to do with Karate. Maybe the solution is to have 2 sets of DB connection values in your karate-config.js. Please figure out a solution that is appropriate for your situation.

Jmeter Beanshell: Accessing global lists of data

I'm using Jmeter to design a test that requires data to be randomly read from text files. To save memory, I have set up a "setUp Thread Group" with a BeanShell PreProcessor with the following:
//Imports
import org.apache.commons.io.FileUtils;
//Read data files
List items = FileUtils.readLines(new File(vars.get("DataFolder") + "/items.txt"));
//Store for future use
props.put("items", items);
I then attempt to read this in my other thread groups and am trying to access a random line in my text files with something like this:
(props.get("items")).get(new Random().nextInt((props.get("items")).size()))
However, this throws a "Typed variable declaration" error and I think it's because the get() method returns an object and I'm trying to invoke size() on it, since it's really a List. I'm not sure what to do here. My ultimate goal is to define some lists of data once to be used globally in my test so my tests don't have to store this data themselves.
Does anyone have any thoughts as to what might be wrong?
EDIT
I've also tried defining the variables in the setUp thread group as follows:
bsh.shared.items = items;
And then using them as this:
(bsh.shared.items).get(new Random().nextInt((bsh.shared.items).size()))
But that fails with the error "Method size() not found in class'bsh.Primitive'".
You were very close, just add casting to List so the interpreter will know what's the expected object:
log.info(((List)props.get("items")).get(new Random().nextInt((props.get("items")).size())));
Be aware that since JMeter 3.1 it is recommended to use Groovy for any form of scripting as:
Groovy performance is much better
Groovy supports more modern Java features while with Beanshell you're stuck at Java 5 level
Groovy has a plenty of JDK enhancements, i.e. File.readLines() function
So kindly find Groovy solution below:
In the first Thread Group:
props.put('items', new File(vars.get('DataFolder') + '/items.txt').readLines()
In the second Thread Group:
def items = props.get('items')
def randomLine = items.get(new Random().nextInt(items.size))

Retrieve response from a "Run Test Step", using SoapUI/ Groovy?

In SoapUI, I have a host Test Case, which executes another external Test Case (with several test steps) using the "Run Test Case" test step. I need to access a response from the external TC from within my host TC, since I need to assert on some values.
I cannot transfer the properties since they are in XML. Could I get some pointers as to how I could leverage Groovy/SoapUI for this.
For Response you can use the below code.
testRunner.testCase.getTestStepByName("test step").testRequest.response.responseContent
In you external TC create another property and at the end of the TC use Transfer Property step to transfer your XML node to it. In your host TC, just read that property as you would any other.
I also had a look around to see if this can be done from Groovy. SoapUI documentation says that you need to refer to the external name of the testsuite / testcase:
def tc = testRunner.testCase.testSuite.project.testSuites["external TestSuite"].testCases["external TestCase"]
def ts = tc.testSteps["test step"]
But I could not find how to get at the Response after that.
In addition to Guest and SiKing answers, I share a solution to a problem that I've met:
If your step is not of type 'request' but 'calltestcase' you cannot use Guest answer.
I have a lot of requests contained each in a testCase and my other testCases call these testCases each time I need to launch a request.
I configured my request testCases in order to return the response as a custom property that I call "testResponse" so I can easily access it from my other testCases.
I met a problem in the following configuration :
I have a "calltestcase" step that gives me a request result.
Further in the test I have a groovy script that needs to call this step and get the response value
If I use this solution :
testRunner.runTestStepByName("test step")
followed by testRunner.testCase.getTestStepByName("test step").testRequest.response.responseContent
I'm stuck as there is no testRequest property for the class.
The solution that works is :
testRunner.runTestStepByName("test step")
def response_value = context.expand( '${test step#testResponse#$[\'value\']}' )
another solution is :
testRunner.runTestStepByName("test step")
tStep = testRunner.testCase.getTestStepByName("test step")
response = tStep.getPropertyValue("testResponse")
Then I extract the relevant value from 'response' (in my case, it is a json that I have to parse).
Of course it works only because I the request response as a custom property of my request test case.
I hope I was clear enough

BeanShell PreProcessor updates User define variables

I'm very new at JMeter issues.
In a test script i have a BeanShell PreProcessor element that updates some variables previously defined at a "User Defined Variables" element.
Latter those variables are used in "Http Requests". However, the value that is used in the http request is the default one.
The scripts seems to be working due to some debug print();
My question is if it's necessary to delay the script to be sure that the BeanShell finishes?
Thanks a lot for your attention
There is no need to put any delay to Beanshell Pre-Processor as it's being executed before request. I'd recommend to check your jmeter.log file to see if there are any scripting issues as Beanshell Pre-Processor does not report errors anywhere including View Results Tree listener.
There are at least 2 ways to assure that everything is fine with your Beanshell script:
Put your debug print code after variables replace logic to see if it fires
Use JMeter __Beahshell function right in your HTTP request. If it's ok - View Results Tree will demonstrate beanshell-generated value. If not - the field will be blank and relevant error will be displayed in the log.
Example test case:
Given following Test Plan structure:
Thread Group with 1 user and 1 loop
HTTP GET Request to google.com with path of / and parameter q
If you provide as parameter "q" following beanshell function:
${__BeanShell(System.currentTimeMillis())}
and look into View Results Tree "Request" tab you should see something like:
GET http://www.google.com/?q=1385206045832
and if you change function to something incorrect like:
${__BeanShell(Something.incorrect())}
you'll see a blank request.
The correct way of changing existing variable (or creating new if variable doesn't exist) looks like
vars.put("variablename", "variablevalue");
*Important: * JMeter Variables are Java Strings, if you're trying to set something else (date, integer, whatever) to JMeter Variable you need to cast it to String somehow.
Example:
int i = 5;
vars.put("int_i", String.valueOf(i));
Hope this helps.
You can update the vale of a "user defined variable".
You have to create a bean shell sampler
vars.put("user_defined_variable", "newvalue");
#theINtoy got it right.
http://www.blazemeter.com/blog/queen-jmeters-built-componentshow-use-beanshell
I'm new to jmeter too but as I know variables defined in "User defined variables" are constants, so you can't change them. I recommend to use "User Parameters" in preprocessors or CSV Data Set Config.

soapUI: property not used in property expansion in a test case

I'm using soapUI 3.6.1 and I want to test a SOAP-interface with a test structure like this:
MyWorkspace
MyProject
TestSuite
TestCase Single
TestStep 1
TestCase Loop
Properties #1
Groovy #1
Run TestCase Single
Properties #2
Groovy #2
Run TestCase Single
TestCase Single contains a bunch of requests that should be performed with the same account-id. TestCase Loop is supposed to call TestCase Single using different account-ids. The Properties * are supposed to change the account-id.
MyProject has a property accountId set up with value 1234. This should work as the default value for single testing of the test cases, i.e. TestCase Single.
Properties #1 and Properties #2 have accountId specified with different values.
The intention is to allow TestStep 1 to be executed alone or as part of TestCase Loop.
TestStep 1 has a common SOAP-body where the following is a parameter sent to the web-service:
<accId>${='${#accountId}' != '' ? '${#accountId}' : ${#Project#accountId}}</accId>
Groovy #1 and Groovy #2 look like this:
log.info('Using account ' + (context.expand('${#accountId}') != '' ? context.expand('${#accountId}') : context.expand('${#Project#accountId}')));
My problem now is that the Groovy-script writes the correct value of accountId (i.e., the one inside the loop), but the TestStep 1 always uses the value from the project. I want TestStep 1 to use the value of the loop.
For debugging purposes I also put the following into TestStep 1:
<!--
{#Project#accountId}: ${#Project#accountId}
{#TestSuite#accountId}: ${#TestSuite#accountId}
{#TestRunContext#accountId}: ${#TestRunContext#accountId}
{#TestRun#accountId}: ${#TestRun#accountId}
{#TestCase#accountId}: ${#TestCase#accountId}
{#TestStep#accountId}: ${#TestStep#accountId}
{#MockService#accountId}: ${#MockService#accountId}
{#Global#accountId}: ${#Global#accountId}
{#System#accountId}: ${#System#accountId}
{#Env#accountId}: ${#Env#accountId}
{#accountId}: ${#accountId}
context.accountId: ${=context.accountId}
modelItem.accountId: ${=modelItem.accountId}
request.accountId: ${=request.accountId}
context.expand(): ${=context.expand('${#accountId}')}
-->
I traced the network traffic with Wireshark and noticed that only ${#Project#accountId} returned a value.
What am I doing wrong here? How do I need to code the <accId>-element to send the correct value to the remote host?
Now I revisited this issue and I think I found the best possible solution, even though I don't understand why my initial attempt didn't work.
I changed the structure to this:
MyWorkspace
MyProject
TestSuite
TestCase Single
TestStep 1
TestCase Loop
Groovy #1
Run TestCase Single
Groovy #2
Run TestCase Single
In other words I got rid of the Properties and moved the logic into the Groovies:
context.getTestCase().getTestSuite().setPropertyValue("accountId_loop", "1720");
and the appropriate line in TestStep 1:
<accId>${='${#TestSuite#accountId_loop}' != '' ? '${#TestSuite#accountId_loop}' : ${#MyProject#accountId}}</accId>