SoapUI enable list of test steps (Groovy) - groovy

I have some SoapUI test cases, where I need to enable specific test steps.
I decided to wrote a simple Groovy script which enables required test steps.
At first I disable all test steps in the test case:
//Get the names of all test steps
def oNameList = testRunner.testCase.getTestStepList().name
for(iNameCounter in (0..oNameList.size-1))
{
testRunner.testCase.getTestStepByName(oNameList[iNameCounter]).setDisabled(true)
}
Then I have list with test steps to enable:
def list = ['Login', 'Get Messages', 'Logout']
for (i = 0; i <list.size; i++) {
testRunner.testCase.getTestStepByName(list[i]).setDisabled(false)
}
It works If 'list' elements exist as test steps in that test case. But not If one of them is missing. Is it possible to make it skip missing test steps? I need to make this groovy work on every test case (in 'list' there will be all preferred test steps from every test case in test suite).
For example:
I have Test case with these test steps: 'Login', 'See bill history', 'Logout'.
I will run this groovy script which disable all test steps.
Then it starts to enable test steps specified in the 'list'.
But it fails because in that test case there doesn't exist test step 'Get messages'.
I want to make it to skip enabling test steps from 'list' which doesn't exist in the actual test case.
Output on this test case should be - enabled: 'Login', 'Logout'; disabled: 'See bill history'

Related

Associate existing shared steps with a test case programmatically

I am trying to add a shared step to a test case that is being created using
CreateWorkItemAsync()
It is no problem to create test steps and add them to the test case
ITestStep testStep1 = testBase.CreateTestStep();
but I am trying to add an existing shared step to the test case. I cannot find a way in the Azure Devops SDK to do so.
There is a ISharedStepReference Interface which used to call a shared step set from a test case.
You should be able to add a shared step into a specific test case. A code snippet for your reference:
ITestManagementService testService = tfsCollection.GetService<ITestManagementService>();
ITestManagementTeamProject teamProject = testService.GetTeamProject(teamProjectName);
//find test case by testcase id
ITestCase testcase1 = teamProject.TestCases.Find(192); //192 is the testcase id
//find share steps by sharedstep id
ISharedStep sharedStep1 = teamProject.SharedSteps.Find(140); //140 is the shared step id
//Add shareSteps to the specific test case
ISharedStepReference sharedStepReference = testcase1.CreateSharedStepReference();
sharedStepReference.SharedStepId = sharedStep1.Id;
testcase1.Actions.Add(sharedStepReference);
testcase1.Save();
What I ended up doing is creating the test case using
CreateWorkItemAsync()
and then getting that work item using
GetWorkItemAsync()
and then editing the field
Microsoft.VSTS.TCM.Steps
as xml and inserting my own nodes into the proper places to add shared steps to the test case. You can see the format of the shared steps by getting a test case with an API GET request and looking at the format of Microsoft.VSTS.TCM.Steps.

Global Variables - Katalon Studio

I am working with Cucumber & Groovy in Katalon Studio.
I have ten feature file lines in Cucumber and corresponding step definitions.
In my cucumber feature file first step has the indicator where if the first line is passed with the parameter with "NO RUN", the test case should not run and it should be moved to the next test case.
So, I thought, I will use the Global variable indicator where I can handle in the test and assign the values. I see that and could create the Global Variable (RUN INDICATOR) under the Execution profile. But, not sure how I need to use that variable in the test script or refer.
Can someone please provide the inputs on this to proceed further ?
Step Definition
#Given("running indicator flag (.*)")
def run_indicator_flag(String ind1) {
println "Passing Indicator " + ind1
assert ((ind1!='') || (ind1!='N'))
WebUI.openBrowser('', FailureHandling.STOP_ON_FAILURE)
}
You can use test listeners to do that.
Create a Global Variable with the empty string value (You need to do this before actually running the test case/suite):
GlobalVariable.RUN_INDICATOR = ''
You will update its value either manually or preceding test will update it to whatever value you wish.
Create a test listener with the following code
#BeforeTestCase
def sampleBeforeTestCase(TestCaseContext testCaseContext) {
if(GlobalVariable.RUN_INDICATOR=='NO RUN'){
testCaseContext.skipThisTestCase()
println "Test Case skipped"
}
}
If the GlobalVariable.RUN_INDICATOR is set to 'NO RUN', this test case will be skipped and the test suite will continue with the next one.

How to use nested paramter in SoapUI context.expand expression?

My use case is that I want to do a bulk update of request bodies in multiple SoapUI projects.
Example of request body.
{
"name": "${#TestSuite#NameProperty}"
"id": "${#TestSuite#IdProperty}"
}
I want to expand the property ${#TestSuite#NameProperty} through Groovy, and get the value stored at TestSuite level then modify it as necessary.
Suppose I have 50 test steps in my test case and I want to expand the request for each one from Groovy script. To expand a specific test steps, I would pass the test Steps's name. For example:
expandedProperty = context.expand('${testStep1#Request}')
But, how can I achieve the same, if I wanted to iterate over all 50 test steps? I tried to used a nested parameter inside the context.expand expression, but it did not work. For Example:
currentTestStepName = "TestStep1"
expandedProperty = context.expand('${${currentTestStepName}#Request}')
This only returned me the expanded request from the test step right above it (where I am running the groovy script from) rather than the "TestStep1" step. ( Which is madness!)
Also, context.expand only seems to work while executing via Groovy script from the SoapUI workspace project. Is there and other way, or method similar to context.expand which can expand the properties like "${#TestSuite#NameProperty}" during headless execution? Eg: A groovy compiled jar file imported in SoapUI.
Thanks for any help in advance!
You can use context.expand('${${currentTestStepName}#Request}') way to get it.
There are other approaches as well, which does not use context.expand.
In order to get single test step request of any given test step:
Here user passes step name to the variable stepName.
log.info context.testCase.testSteps[stepName].getPropertyValue('Request')
If you want to get all the requests of the test case, here is the simple way using the below script.
/**
* This script loops thru the tests steps of SOAP Request steps,
* Adds the step name, and request to a map.
* So, that one can query the map to get the request using step name any time later.
*/
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
def requestsMap = [:]
context.testCase.testStepList.each { step ->
log.info "Looking into soap request step: ${step.name}"
if (step instanceof WsdlTestRequestStep) {
log.info "Found a request step of required type "
requestsMap[step.name] = context.expand(step.getPropertyValue('Request'))
}
}
log.info requestsMap['TestStep1']
Update :
If the step that you are interested is REST step, use below condition instead of WsdlTestRequestStep in the above.
if (step instanceof com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep) { //do the stuff }

Transfer Groovy script response to properties in SOAP UI 5.21

Can any one know how to transfer the groovyscript response into the properties step of SOAP UI. I am trying to generate the random numbers using the groovy script, and when i am gettign the random generated numbers how do i transfer that value to properties in soap ui which can be used for the TCs as a parametered value.
TIA
To make it simple,
Use below code to store any value on,
test case level custom properties:
testRunner.testCase.setPropertyValue("propertyName","value");
test suite level custom properties:
testRunner.testCase.testSuite.setPropertyValue("propertyName","value");
project level custom properties:
testRunner.testCase.testSuite.project.setPropertyValue("propertyName","value");
Use below code to check whether value stored successfully on runtime:
test case level:
log.info testRunner.testCase.getPropertyValue("propertyName");
test suite level:
log.info testRunner.testCase.testSuite.getPropertyValue("propertyName");
project level:
log.info testRunner.testCase.testSuite.project.getPropertyValue("propertyName");
Use below code to use the property value inside anywhere on,
test case level:
${#TestCase#propertyName}
test suite level:
${#TestSuite#propertyName}
project level:
${#Project#propertyName}
global level:
${#Global#propertyName}
Here you go:
The below groovy script code snippet will generate a random number and set the value into to a test case level custom property, say PROPERTY_NAME.
Groovy Script
context.testCase.setPropertyValue('PROPERTY_NAME', (Math.abs(new Random().nextInt()) + 1).toString())
In the same test case, it can be accessed in any test requests as ${#TestCase#PROPERTY_NAME}
EDIT: Based on the change you wanted while above original code works though
def a = 9
def AccountName = ''
(0..a).each { AccountName = AccountName + new Random().nextInt(a) }
context.testCase.setPropertyValue('Property_Name', AccountName.toString())
Even you achieve the same thing using below (just updated value in nextInt() to the first answer)
context.testCase.setPropertyValue('PROPERTY_NAME', (Math.abs(new Random().nextInt(999999998)) + 1).toString())

Skip Test Step in SOAPUI through groovy

I have Test case starts with Groovy Test step and followed with Property and 4 SOAP request test steps. In groovy Test step I performing execution of those SOAP requests, accessing data from property Test step.
Here I just want to execute those SOAP request from groovy test step alone. When I ran it as Test Case in SOAPUI after Executing groovy Test step, those 4 SOAP requests also get executed and my Test case got failed.
I use testRunner.cancel("Skip the Teststep") it could skip those test step, But it results as failure in Execution report and I cant find any method to skip test step using groovy.
Please help me somebody on this.
Regards,
Madhan
Try this in the Groovy Script step.
testRunner.testCase.testSteps.each{k, v ->
if(k in ['step1', 'step2'])
v.cancel()
}
where step1 and step2 are the steps you want to skip.
If you want to cancel all the test steps then use
testRunner.testCase.testSteps.each{k, v ->
testRunner.cancel(k)
if want to disable test steps
def testSuite = context.testCase.testSuite;
def totalTestCases = testSuite.getTestCases().size();
for(n in (0..totalTestCases-1))
{
testSuite.getTestCaseAt(n).setDisabled(true)
}

Resources