Forum Discussion

aaronpliu's avatar
aaronpliu
Frequent Contributor
7 years ago

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

  • Olga_T's avatar
    Olga_T
    SmartBear Alumni (Retired)

    Hi aaronpliu,

     

    Well done! Thanks for sharing the script with the Community :smileyhappy:

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

     

    • Olga_T's avatar
      Olga_T
      SmartBear Alumni (Retired)

      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: 

      • aaronpliu's avatar
        aaronpliu
        Frequent Contributor

        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())