ContributionsMost RecentMost LikesSolutionsHow to capture teststep status as pass/fail using groovy to write it in txt file Question How to capture teststep status as pass/fail using groovy to write it in txt file Answer This can be done very simply by using the following TearDown Script: // Define variables for holding test suites, test cases and test steps def testSuites def testCases def testSteps // Get all the test suites from the project testSuites = project.testSuiteList File file = new File("C:\\Users\\luciana\\Desktop\\test.txt") /** * Iterate through each test suite, test case and test step */ testSuites.each() { // Log test suite name file << "-----------------------------------\n" file << "Running test suite: " + it.getName() + "\n" file << "-----------------------------------\n" // Get a list with the contained test cases testCases = it.getTestCaseList() testCases.each() { // Log test case name file << "-----------------------------------\n" file << "Running test case: " + it.getName() + "\n" file << "-----------------------------------\n" // Get a list with the contained test steps testSteps = it.getTestStepList() testSteps.each() { file << it.getName() + " - " + it.getAssertionStatus() + "\n" } } } Sharing is caring - how to modify xml files using ReadyApi Modifying xml filesor simply working with xml strings might look difficult for a begginer so for those who need itI will try to break the whole thing into pieces so that it is easier to understand. Refering to nodes and attributes Let's say this piece of xml is stored in the file 'mijnFruits.xml' somewhere on your computer: <fruits> <apples> <apple color="red"/> <apple color="green"/> </apples> <oranges> <clementine> <mandarine> </oranges> <bananas/> <jabu-ticaba/> </fruits> If you would want to use a groovy script to identify a certain element, you'll first have to define a variable in which to parse the content: def xml = new XmlSlurper().parse("D:\\someFolder\\mijnFruits.xml") In order to refer to the 'clementine' node for instance, you would use: xml.oranges.clementine // For instance, the following will replace the node 'clementine' with 'tangerine' // xml.oranges.clementine.replaceNode { // tangerine '' // } Please note that while refering to any node, the root node (i.e. "fruits") is replaced by the variable in which the file is parsed (in our case 'xml'). That is why in our example we used 'xml.oranges.clementine' and not 'fruits.oranges.clementine' In order to refer to a node that has a certain attribute ( for instance <apple color="red"/> ), the syntax is as follows: // This will return a list will all the nodes 'xml.apples.apple' that have an attribute 'color' and the value 'red' xml.apples.apple.findAll{it.@color == "red"} // Hence, it is possible to check if such a node exists. For instance: //if (xml.apples.apple.findAll{it.@color == "red"}.size() == 0) // log.info("Such a node doesn't exist.") //else // log.info("Such a node exists.") If you want to refer to a node whose name contains dashes (example: 'jabu-ticaba') then the name must be placed inside single quotes like xml.'jabu-ticaba' // Example of node removing // xml.'jabu-ticaba'.replaceNode { } Practical examples Change an attribute value of a property import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\someFolder\\web.config") // Change the attribute value for 'xxs-protection' xml.webApplication.'xss-protection'.@enabled = "false" // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\someFolder\\web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Add a new node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\someFolder\\web.config") // Add a node called 'conf' inside the <confs></confs> tags. The node will have 3 attributes (name, project and version) along with their corresponding values ('myName', 'myProj' and 'v0.0') xml.shortcutSection.shortcuts.appendNode { shortcut(name: "myName", project: "myProject", version: "v0.0") } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\someFolder\\web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() // The output will look like // <confs> // <conf name="myName" project="myProject" version="v0.0"/> // </confs> Add a more complex node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Web.config") // Add a more complex node (which contain more nodes or attributes) xml.'hibernate-configuration'.'session-factories'.appendNode { 'session-factory'(name: "test") { properties { property(name: "one hell of a property", value: "great of course") property(name: "another hell of a property", value: "great again of course") property(name: "avcsacsadsadsa", value: "asdsadsadasda") property(name: "asdsadsadas", value: "asdsadsadsa") property(name: "another hell of a property", value: "great again of course") } } } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Remove a node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Web.config") // Remove a node xml.shortcutSection.replaceNode { } // this removes the shortcutSection node along with its containments // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Remove a node that contains a certain attribute import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Groovy projects\\Web.config") // Delete a node that has a certain given attribute xml.'hibernate-configuration'.'session-factories'.'session-factory'.findAll{it.@name == "myName"}.replaceNode { } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Groovy projects\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Replace a node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Groovy projects\\Web.config") xml.configSections.replaceNode { noMoreConfigSection '' } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Groovy projects\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() // The result would be that the configSections node i.e. // <configSections> // <section ...> // <section ...> // </configSections> // would be replaced by <noMoreConfigSection/> How to modify xml files using ReadyAPI Modifying xml filesor simply working with xml strings might look difficult for a beginner so for those who need itI will try to break the whole thing into pieces so that it is easier to understand. Referring to nodes and attributes Let's say this piece of xml is stored in the file 'mijnFruits.xml' somewhere on your computer: <fruits> <apples> <apple color="red"/> <apple color="green"/> </apples> <oranges> <clementine> <mandarine> </oranges> <bananas/> <jabu-ticaba/> </fruits> If you would want to use a groovy script to identify a certain element, you'll first have to define a variable in which to parse the content: def xml = new XmlSlurper().parse("D:\\someFolder\\mijnFruits.xml") In order to refer to the 'clementine' node for instance, you would use: xml.oranges.clementine // For instance, the following will replace the node 'clementine' with 'tangerine' // xml.oranges.clementine.replaceNode { // tangerine '' // } Please note that while refering to any node, the root node (i.e. "fruits") is replaced by the variable in which the file is parsed (in our case 'xml'). That is why in our example we used 'xml.oranges.clementine' and not 'fruits.oranges.clementine' In order to refer to a node that has a certain attribute ( for instance <apple color="red"/> ), the syntax is as follows: // This will return a list will all the nodes 'xml.apples.apple' that have an attribute 'color' and the value 'red' xml.apples.apple.findAll{it.@color == "red"} // Hence, it is possible to check if such a node exists. For instance: //if (xml.apples.apple.findAll{it.@color == "red"}.size() == 0) // log.info("Such a node doesn't exist.") //else // log.info("Such a node exists.") If you want to refer to a node whose name contains dashes (example: 'jabu-ticaba') then the name must be placed inside single quotes like xml.'jabu-ticaba' // Example of node removing // xml.'jabu-ticaba'.replaceNode { } Practical examples Change an attribute value of a property import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\someFolder\\web.config") // Change the attribute value for 'xxs-protection' xml.webApplication.'xss-protection'.@enabled = "false" // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\someFolder\\web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Add a new node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\someFolder\\web.config") // Add a node called 'conf' inside the <confs></confs> tags. The node will have 3 attributes (name, project and version) along with their corresponding values ('myName', 'myProj' and 'v0.0') xml.shortcutSection.shortcuts.appendNode { shortcut(name: "myName", project: "myProject", version: "v0.0") } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\someFolder\\web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() // The output will look like // <confs> // <conf name="myName" project="myProject" version="v0.0"/> // </confs> Add a more complex node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Web.config") // Add a more complex node (which contain more nodes or attributes) xml.'hibernate-configuration'.'session-factories'.appendNode { 'session-factory'(name: "test") { properties { property(name: "one hell of a property", value: "great of course") property(name: "another hell of a property", value: "great again of course") property(name: "avcsacsadsadsa", value: "asdsadsadasda") property(name: "asdsadsadas", value: "asdsadsadsa") property(name: "another hell of a property", value: "great again of course") } } } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Remove a node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Web.config") // Remove a node xml.shortcutSection.replaceNode { } // this removes the shortcutSection node along with its containments // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Remove a node that contains a certain attribute import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Groovy projects\\Web.config") // Delete a node that has a certain given attribute xml.'hibernate-configuration'.'session-factories'.'session-factory'.findAll{it.@name == "myName"}.replaceNode { } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Groovy projects\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() Replace a node import groovy.xml.XmlUtil import groovy.util.Node // Parse the content of the document and store it in the 'xml' variable def xml = new XmlSlurper().parse("D:\\mijn\\Groovy projects\\Web.config") xml.configSections.replaceNode { noMoreConfigSection '' } // Construct a FileWriter object and assigns it to the variable 'writer' def writer = new FileWriter("D:\\mijn\\Groovy projects\\Web.config") // Write the changes to the file XmlUtil.serialize(xml, writer) // Close file writer.close() // The result would be that the configSections node i.e. // <configSections> // <section ...> // <section ...> // </configSections> // would be replaced by <noMoreConfigSection/> Dispatching responses from a Virtual Service As suggested byOlga_TI created a scriptthat dispatches responses as presented in this topic. The idea behind it is pretty simple namely creating one operation, 4 responses and setting the dispatch style to script.The code I used is attached below: // Get the full query string def queryString = mockRequest.getRequest().getQueryString() // Get the password index in the query string def passwordIndex = queryString.indexOf("password=") + 9 // Cut out the password starting with the previously obtained index def pass = queryString.substring(passwordIndex) // If there are more parameters after the password rule them out if (pass.contains("&")) pass = pass.substring(0, pass.indexOf("&")) /** * Start dispatching responses */ if( !queryString.contains("grant_type=") || !queryString.contains("username=") || !queryString.contains("password=") ) { // Return a bad request return "BadRequest" } else if ( !queryString.contains("grant_type=password")) { // Return an unsupported grant type response return "UnsupportedGrantType" } else if ( !queryString.contains("username=test") || !checkPasswordComplexity(pass)) { // Return invalid credentials return "InvalidCredentials" } else { return "Success" } // Methods definition def checkPasswordComplexity(String password) { if (password ==~ /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}/) return true else return false } Dispatching responses from a Virtual Service Question Create a REST virtual service with the "GET /login"resource and add adispatching script, which returns different responses based on the values of query parameters (grant_type, username and password). A sample request: http://localhost:<VIRT_PORT>/login?grant_type=password&username=test&password=Test123! The dispatching condition: Ifgrant_typeis not equal to "password", the virt returns the "Unsupported Grant Type" response (HTTP status code is 401, body is {"error": "Unsupported Grant Type"}). Ifgrant_typeis equal to "password", but the username is not equal to "test" OR the password doesn't comply with the strength requirements (the minimal length is 8 characters, the password contains at least one uppercase letter, one lowercase letter, one number and one symbol) the virt returns the "Invalid Login Info" response (HTTP status code is 401, body is {"error": "Invalid Username or Password"}). If some parameters are missing, the virt returns the "Bad Request" response (HTTP state code is 400, body is {"error":"required parameters are missed"}) If all the parameters have valid values, the virt returns the "OK" response (HTTP status code is 200, body is {"result":"ok"}). Answer Here is a scriptthat dispatches responses The idea behind it is pretty simple namely creating one operation, 4 responses and setting the dispatch style to script.The code I used is attached below: // Get the full query string def queryString = mockRequest.getRequest().getQueryString() // Get the password index in the query string def passwordIndex = queryString.indexOf("password=") + 9 // Cut out the password starting with the previously obtained index def pass = queryString.substring(passwordIndex) // If there are more parameters after the password rule them out if (pass.contains("&")) pass = pass.substring(0, pass.indexOf("&")) /** * Start dispatching responses */ if( !queryString.contains("grant_type=") || !queryString.contains("username=") || !queryString.contains("password=") ) { // Return a bad request return "BadRequest" } else if ( !queryString.contains("grant_type=password")) { // Return an unsupported grant type response return "UnsupportedGrantType" } else if ( !queryString.contains("username=test") || !checkPasswordComplexity(pass)) { // Return invalid credentials return "InvalidCredentials" } else { return "Success" } // Methods definition def checkPasswordComplexity(String password) { if (password ==~ /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}/) return true else return false } Code for groovy datasource Question generates some identification codes like X00001, X00002 and so on... Answer // Get current row def row = testRunner.testCase.testSteps["DataSource"].currentRow // Declare an array that will contain the id codes def codes = [] // Generate all id codes for (int i = 1; i < 100000; i++) { codes.add( String.format('X%05d', i) ) } // Make the values reachable from the datasource if (row < codes.size()) { // Return the name to data source's "File" property result["Code"] = codes[row] } Sharing is caring - code for groovy datasource As perOlga_T's suggestion here is a script that generates some identification codes like X00001, X00002 and so on... // Get current row def row = testRunner.testCase.testSteps["DataSource"].currentRow // Declare an array that will contain the id codes def codes = [] // Generate all id codes for (int i = 1; i < 100000; i++) { codes.add( String.format('X%05d', i) ) } // Make the values reachable from the datasource if (row < codes.size()) { // Return the name to data source's "File" property result["Code"] = codes[row] } Cheers! Sharing is caring - ReadyApi libraries 1.Reasoning Although not very well documented, ReadyApi gives users the possibility to use libraries (-ish). Those are very handy when you have similar steps that are repeating over multiple test cases and they serve at least 3 basic purposes: less duplicate steps in your project more clear project structure given by the reduction of test steps in a test case ease in updating the project if something is to change at some point (because only the libary has to be edited - and not every test steps in particular where they are used) 2.Libraries inReadyApi Libraries in ReadyApi are just test cases which are run from another test case or test suite. Take this example: In each step we want to first navigate to someDetails Sectionin a website and then check for name, ocupation etc. You can see in the picture above that the Steps in blue quadrants are repeating themselves over and over again which is something usually undesirable. A cleaner way to do it would be to create aLibrariestest suite to hold any steps that are repeating in multiple places and/or are subject to future changes. In our case we would have a OpenDetailsSection test case: Then we would simply make use of the 'Run TestCase'test step to run theOpenDetailsSectiontest case: ReadyAPI libraries - How to set them up 1.Reasoning Although not very well documented, ReadyAPI gives users the possibility to use libraries (-ish). Those are very handy when you have similar steps that are repeating over multiple test cases and they serve at least 3 basic purposes: less duplicate steps in your project more clear project structure given by the reduction of test steps in a test case ease in updating the project if something is to change at some point (because only the library has to be edited - and not every test steps in particular where they are used) 2.Libraries inReadyAPI Libraries in ReadyAPI are just test cases which are run from another test case or test suite. Take this example: In each step we want to first navigate to someDetails Sectionin a website and then check for name, occupation etc. You can see in the picture above that the Steps in blue quadrants are repeating themselves over and over again which is something usually undesirable. A cleaner way to do it would be to create aLibrariestest suite to hold any steps that are repeating in multiple places and/or are subject to future changes. In our case we would have a OpenDetailsSection test case: Then we would simply make use of the 'Run TestCase'test step to run theOpenDetailsSectiontest case: Properties in SoapUI - case study The possibility to use properties is a very important aspect in SoapUI automation. And although they are widely used they tend to be not very well understood. Indeed the name 'property' seems a little unfit for an element that (by all means) is presented by SmartBear as a variable to hold certain values for later use in the test execution cycle. For instance, one can have a 'sessionId' element to hold the current session string or one can have a 'cookie' element to hold some cookie value and so on. 1.Defining properties Properties can be defined at the project level, at the test suite level, at the test case level or in a special properties step. For instance, in order to define or edit a property at the test case level one must click on the desired test case in the left hand navigator and then just select the 'Custom Properties' tab. This is similar for any test suite or project. As said before there is also the possibility to use a special'properties step'. Thisstepmust be created inside a certain test case thus its properties will only be available inside that particular test case. 2. Reading properties 2.1 Referencing in SoapUI steps When wanting to reference a property, attention has to be paid to the level at which it was defined. There are different syntaxes that need to be written depending on it as presented in the table below: Level at which it is defined Syntax Example property name Example request Project level ${#Project#Property} Port http://myserver:${#Project#Port} Test suite level ${#TestSuite#Property} Port http://myserver:${#TestSuite#Port} Test case level ${#TestCase#Property} Port http://myserver:${#TestCase#Port} Special properties step ${#Property} Port http://myserver:${#Port} Following the above example, if one would have a project level property named Port with the value 8080, then that property could be used anywhere in the project by simply referencing it like${#Project#Port}. Example: When sending the request, the value of the ${#Project#Port} would be replaced by its value (in our example 8080). 2.2 Referencing in scripts In order to read a property in a script the following syntax is required: /** * Notes: * There are 4 properties defined at 4 levels: project, test suite, test case and special properties step level * The special properties step is simply called 'Properties' */ def stepProperty = testRunner.testCase.getTestStepByName( "Properties" ).getPropertyValue( "myCustomProperty" ) def testCaseProperty = testRunner.testCase.getPropertyValue( "myCustomProperty" ) def testSuiteProperty = testRunner.testCase.testSuite.getPropertyValue( "myCustomProperty" ) def projectProperty = testRunner.testCase.testSuite.project.getPropertyValue( "myCustomProperty" ) 3. Updating properties Updating tests can be done using a groovy script or a special transfer property step. Both of the methods are simple and straightforward but for the purpuse of this article only a groovy script example will be shown: /** * Notes: * This script changes 4 properties defined at 4 levels: project, test suite, test case and special properties step level * The special properties step is simply called 'Properties' */ testRunner.testCase.getTestStepByName( "Properties" ).setPropertyValue( "myCustomProperty", "newSpecialValue" ) testRunner.testCase.setPropertyValue( "myCustomProperty", "newTestCaseValue" ) testRunner.testCase.testSuite.setPropertyValue( "myCustomProperty", "newTestSuiteValue" ) testRunner.testCase.testSuite.project.setPropertyValue( "myCustomProperty", "newProjectValue" )