cancel
Showing results for 
Search instead for 
Did you mean: 

Operating with REST Interfaces and Requests

Highlighted
Contributor

Operating with REST Interfaces and Requests

Operating with REST Interfaces and Requests

Create a script which posts REST Interfaces structures of a current project to a text file. Under each method, list names of functional REST Request Test Steps for this method. The format of the file is arbitrary (whatever looks more reasonable and readable for you). Example: https://www.screencast.com/t/WVmvALNW2Ds

 

def space = {num -> ' ' * num}
def stringBuilder = new StringBuilder()
project.getInterfaceList().each{p ->
	// interface name
	stringBuilder.append("${p.name}\n")
	p.getOperationList().each{m ->
		//operation name
		stringBuilder.append("${space(5)}${m.name}\n")
		m.getRequestList().each{r ->
			//http method name
			stringBuilder.append("${space(10)}${r.getMethod()}\n")
			//rest method name
			stringBuilder.append("${space(15)}${r.getRestMethod().name}\n")
			}
		}
	}

def writeToFile(def directory, def filename, def extension, def content){
		if(! content.trim().isEmpty()){
			def folder = new File(directory)
			if( ! folder.exists()) folder.mkdirs()
			new File("$directory/$filename$extension").withWriter{out ->
				out << content
			}
		}
	}
writeToFile("C:/", "project_structure", ".txt", stringBuilder.toString())

 

BR,

/Aaron

4 REPLIES 4
Moderator

Re: Operating with REST Interfaces and Requests

Hi @aaronpliu,

 

Well done! Thanks for sharing the script with the Community Smiley Happy

This TechCorner script gives you 2 points to your score in API Summer 2018!

 


Olga Terentieva
SmartBear Assistant Community Manager

Moderator

Re: Operating with REST Interfaces and Requests

Hi @aaronpliu,


We have asked our Customer Care team to review the script. And, @NBorovykh has noted that it outputs names assigned to HTTP methods on the Projects tab. In the meantime, the original task was to log the names of functional REST Request Test Steps associated with those methods.

 

So, do you mind improving your script so that it could do the following?

 

  1. The script should iterate through all the TestSuites, TestCases, and TestSteps in the project, find the associations between functional steps (on the SoapUI tab) and the REST API methods (on the Projects tab), and output this info to the file.

  2. The script should produce a better output when names of Resources and Methods in ReadyAPI UI are empty. Currently, it produces this: 

    2018-08-13_1420

Olga Terentieva
SmartBear Assistant Community Manager

Contributor

Re: Operating with REST Interfaces and Requests

Hi ,

 

I enhanced scripts and attached below.

the topic is to print project structure from test steps?

 

// print project strucutre based on endpoints
def space = {num -> ' ' * num}
def stringBuilder = new StringBuilder()
testRunner.testCase.testSuite.project.getInterfaceList().each{p ->
	// interface name
	stringBuilder.append("${p.name}\n")
	p.getOperationList().each{m ->
		//operation name
		if(m.name.size() < 1)
			stringBuilder.append("${space(5)} ==>Resource name is empty\n")
		else 
			stringBuilder.append("${space(5)}${m.name}\n")
		m.getRequestList().each{r ->
			//http method name
			stringBuilder.append("${space(10)}${r.getMethod()}\n")
			//rest method name
			if(r.getRestMethod().name.size() < 1)
				stringBuilder.append("${space(15)} ==>Rest method name is empty\n")
			else
				stringBuilder.append("${space(15)}${r.getRestMethod().name}\n")
			}
		}
	}

def writeToFile(def directory, def filename, def extension, def content){
		if(! content.trim().isEmpty()){
			def folder = new File(directory)
			if( ! folder.exists()) folder.mkdirs()
			new File("$directory/$filename$extension").withWriter{out ->
				out << content
			}
		}
	}
writeToFile("C:/temp", "project_structure", ".txt", stringBuilder.toString())


//----------------------------------------------------------------------
// print project strucutre from test steps
def str2 = new StringBuilder()
def interfaceNameList = [] 
testRunner.testCase.testSuite.project.getTestSuiteList().each{testsuite ->
	testsuite.getTestCaseList().each{testcase ->
		testcase.getTestStepsOfType(com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep.class).each{teststep ->
			// interface name
			if(! (teststep.testRequest.getInterface().name in interfaceNameList)){
				interfaceNameList << teststep.testRequest.getInterface().name
				str2.append("${teststep.testRequest.getInterface().name}\n")
				teststep.testRequest.getInterface().getOperationList().each{m ->
					//operation name
					if(m.name.size() < 1)
						str2.append("${space(5)} ==>Resource name is empty\n")
					else 
						str2.append("${space(5)}${m.name}\n")
					m.getRequestList().each{r ->
						//request method name
						str2.append("${space(10)}${r.getMethod()}\n")
						//rest method name
						if(r.getRestMethod().name.size() < 1)
							str2.append("${space(15)} ==>Rest method name is empty\n")
						else
							str2.append("${space(15)}${r.getRestMethod().name}\n")
					}
				}
			}
		}
	}
}

writeToFile("C:/temp", "project_structure2", ".txt", str2.toString())
Moderator

Re: Operating with REST Interfaces and Requests

Hi @aaronpliu,

 

You have significantly improved the original script, and you are pretty close to the final version, good job! Cat Happy


There is some additional work that should be done though. Please take a look at the screencasts. We've cited the desired and the actual results of the script execution.

 

Current results:  https://www.screencast.com/t/VqGaDbDAF

 

Desired results:  https://www.screencast.com/t/sddpc6QLIHo -the script needs to output a single file

 

I'm attaching a sample project our engineers worked with. You can use it to play with your script.

 

Have a great day!


Olga Terentieva
SmartBear Assistant Community Manager

New Here?
Join us and watch the welcome video:
Watch the New Interview
7Mistakes
Join the exciting event
SeptemberHubBub
Top Kudoed Authors
Join the September Hub-bub to show off, learn and win