Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Groovy in Soap UI for lazy QA

ymkocv
December 19, 2016

Groovy in Soap UI for lazy QA

Small tips how to simplify work in testing web-services

ymkocv

December 19, 2016
Tweet

More Decks by ymkocv

Other Decks in Programming

Transcript

  1. About me  ~ 7 years in QA  Leading

    teams from 2 to 8 people  Mobile, desktop, WEB, BI, WS testing  Automation back-end tests writing & maintaining
  2. Web service  In a web service, web technology such

    as the HTTP, originally designed for human-to-machine communication, is utilized for machine-to-machine communication
  3. REST  Representational State Transfer  HTTP protocol is used

     XML/JSON SOAP • Simple Object Access Protocol • WSDL (Web Service Definition Language) • Based on XML
  4. HTTP Codes  1xx: Informational (100 Continue)  2xx: Success

    (200 OK, 202 Accepted)  3xx: Redirection (300 Multiple Choices)  4xx: Client Error (400 Bad Request, 404 Not Found)  5xx: Server Error (500 Internal Server Error)
  5. Apache Groovy is a powerful, optionally typed and dynamic language,

    with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity thanks to a concise, familiar and easy to learn syntax. It integrates smoothly with any Java program, and immediately delivers to your application powerful features, including scripting capabilities.
  6. Context & test runner // get username property from TestSuite

    def username = testRunner.testCase.testSuite.getPropertyValue( "Username" ) // get username property inside TestCase def username = context.testCase.getPropertyValue( "Username" )
  7. Rename steps 1. import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep; 2. import com.eviware.soapui.model.testsuite.TestStep; 3. String

    wordNeedToFind = "Wrong"; 4. String wordNeedToReplace = "Long"; 5. for (TestStep step : context.testCase.getTestStepList()) { 6. if(step instanceof WsdlTestStep){ 7. if (step.getName().contains(wordNeedToFind)){ 8. step.setName(step.getName().replaceAll("Wrong","Long")); } } }
  8. Rename steps 1. import com.eviware.soapui.support.GroovyUtils; 2. context.testCase.getTestStepList().each { 3. if

    (it.getLabel().contains("Long")){ 4. def newName = it.getLabel().replaceAll("Long","Wrong"); 5. it.setName(newName) } }
  9. Remove properties 1. def suitList = context.testCase.testSuite.project.getTestSuiteList() 2. suitList.each{ 3.

    def caseList = it.getTestCaseList() 4. caseList.each{ 5. String[] propToRemove = it.getPropertyNames(); 6. for ( int i = 0 ; i < propToRemove.size(); i++ ){ 7. it.removeProperty(propToRemove[i]);} } }
  10. Changing requests 1. def testStepList = it.getTestStepList(); 2. testStepList.each {eachTestStep

    -> 3. if(eachTestStep instanceof 4. com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep){ 5. RestRequest request = eachTestStep.getHttpRequest(); 6. def result = request.getRequestContent().replaceAll(wordNeedToFind, 7. wordNeedToReplace); 8. request.setRequestContent(result)} }
  11. Examples  def result = request.getRequestContent().replaceAll("ProviderInfo", "PartnerInfo");  def result

    = request.getRequestContent().replaceAll('(\"TransactionId\":\")(.*)(\")', "\"TransactionId\":" + "\"$randomCode\"");  String wordNeedToFind = "Channel\":\"String content";  String wordNeedToReplace = "Channel\":\"App";
  12. Write to a file 1. File file = new File("C://text.txt")

    2. String info = context.testCase.getPropertyValue("info") 3. String text = context.testCase.getPropertyValue("text") 4. String details = "Some imp info " + info + " has details " + text 5. file << ("\r\n") 6. file << details
  13. Hash map Type Parameters: K - the type of keys

    maintained by this map V - the type of mapped values
  14. Hash map 1. ArrayList ParamsList = createListWithRequiredParams(responseData); 2. HashMap RequiredFieldsMap

    = createMapWithRequiredParams( requiredParamsList); 3. buildJsonFromMapSetToTestCaseProp(contactRequiredFieldsMap, "contactRequiredFieldsMap")
  15. Hash map 1. //Create a list of required parameters 2.

    public ArrayList createListWithRequiredParams(def responseData) { 3. List requiredParams = new ArrayList(); 4. responseData.DynamicFields.each { 5. if (it.IsRequired.equals(true)) { 6. def name = it.Name; 7. requiredParams.add(name);} } 8. return requiredParams;}
  16. Hash map 1. // create map with parameters and values

    2. public HashMap createMapWithRequiredParams(ArrayList list) { 3. Map<String, Object> RequiredFieldsMap = new HashMap(); 4. list.each{ 5. contactRequiredFieldsMap.put(it, 6. context.testCase.getPropertyValue(getPropertyIfContains(it).toString()));} 7. return RequiredFieldsMap;}
  17. Hash map 1. // get necessary parameter from test case

    2. public String getPropertyIfContains(String pram){ 3. def propertyNames = context.testCase.getProperties().keySet(); 4. String name = null; 5. propertyNames.each{ 6. if( it.toLowerCase().contains("contact") && 7. it.toLowerCase().contains(pram.toLowerCase()) ){ 8. name = it; } } 9. return name;}
  18. Hash map 1. // build json from map 2. public

    void buildJsonFromMapSetToTestCaseProp(HashMap map, String propertyName){ 3. JSONObject contactRequiredFields = new JSONObject(map); 4. int spacesToIndentEachLevel = 2; 5. context.testCase.setPropertyValue(propertyName, 6. contactRequiredFields.toString(spacesToIndentEachLevel)); }
  19. Conclusions Scripting  is saving time & effort  is

    interesting  is simplifying work