SOAPUI + Groovy Interface Automation test eighth chapter

Source: Internet
Author: User
Tags soapui smartbear groovy script

A wave of small group battles!

1. Manipulating Excel's Groovy script

Package Pubimport Jxl.*import Jxl.write.Labelimport jxl.write.WritableWorkbookclass exceloperation {def xlsfiledef Workbookdef writableworkbookdef exceloperation () {}//set Xlsfile file path def exceloperation (xlsfile) {this.xlsFile = XlsFile  }//Initialize WORKBOOKDEF init () {workbook = Workbook.getworkbook (new File (xlsfile)) Writableworkbook = Workbook.createworkbook (New File (Xlsfile), Workbook)}//close Workbook stream def close () {Writableworkbook.write () Writableworkbook.close () workbook.close ()}//get list Environment def envlistget (SheetName, projectName) {def list = []init () List = Envlist (Workbook, SheetName, ProjectName) Close () return list}//get the map Environment def Envmapget (SheetName, projectName) {def map = [ :]init () map = envmap (Workbook, SheetName, ProjectName) Close () return map}//write file def cellwriteset (SheetName, RowName, ColumnName, content) {init () cellwrite (Writableworkbook, SheetName, RowName, columnName, content) Close ()}//Get test environment *def Envlist (Workbook Workbook, SheetName, projectName) {def list = []sheet Sheet = Workbook.getsheet (Sheetname) cell[] C1 = sheet.getcolumn (0) cell[] C2 = Sheet.getcolumn (1) def Endc1.each {It->if (it.getcontents (). Contains ( ProjectName) {end = It.row}}for (i in 0..5) {List[i] = c2[end].getcontents () end--}return list}//get test environment def ENVMAP ( Workbook Workbook, SheetName, projectName) {def map = [:]sheet Sheet = Workbook.getsheet (sheetname) cell[] C1 = Sheet.getco Lumn (0) cell[] C2 = Sheet.getcolumn (1) def Endc1.each {It->if (it.getcontents (). Contains (ProjectName)) {end = It.row}} For (I-in 0..5) {Map.put (C1[end].getcontents (), c2[end].getcontents ()) End--}return map}//write test result def cellwrite ( Writableworkbook Writableworkbook, SheetName, RowName, columnName, content) {Sheet Sheet = Writableworkbook.getsheet ( SheetName) if (sheet = = null) {sheet = Writableworkbook.createsheet (sheetname,2)}cell[] C1 = sheet.getcolumn (0) cell[] C2 = Sheet.getrow (0) def Row, Colc1.each {it->if (it.getcontents (). Equals (RowName)) {row = It.row}}c2.each {It->if ( It.getcontents (). Equals (ColumnName)) {col = It.column}}sheet.addcell (new LaBel (Col, Row, content))}} 

This is a well-packaged call to Excel class, mainly to see how to read and write Excel, other aspects of the individual needs to modify

2. Generate MD5 's groovy script

Package Pubimport java.security.MessageDigestclass MD5 {def md5//null construction method def MD5 () {}//constructor method implementation construct return MD5 value def MD5 (map, pwd) { string s = getmapstring (map, pwd) String B = getMD5 (s) MD5 = B.tolowercase ()}//sorts the map by key and takes out the value composed of the string Def getmapstring ( Map map, String pwd) {def TM = new TreeMap (map)//Sort by Mapkey stringbuffer sb = new StringBuffer () Tm.each {key,value->SB . Append "$value"}sb.append (PWD) return sb.tostring ()  }//returns the MD5 value of the passed-in string def getMD5 (string source) {MessageDigest MD5 = Messagedigest.getinstance ("MD5") Md5.update (Source.getbytes ())  //Pass byte array to md5byte[] Digest = md5.digest ()   / /Generate MD5 sequence StringBuffer sb = new StringBuffer () digest.eachbyte {sb.append (String.Format ("%02x", it & 0xff))// Convert MD5 value to 16 binary}return sb.tostring ()  

The map and PWD are passed in, and the map is sorted by key, and the value of map is concatenated to generate a MD5 value return.

3. Working with TXT text

Package Pubimport java.io.Fileclass Text {def writefile (filename, context) {def file = new file (filename) if (!file.exists ( ) {File.createnewfile ()}file.append (New Date (). Format (' Yyyy-mm-dd hh-mm-ss '), ' UTF-8 ') file.append (' \ n ') File.append (context, ' UTF-8 ') file.append (' \ n ')}}

4. Invoking a third-party jar package

Soapui can call third party jar package/.class/.groovy file;

Open the Lib directory in SoapUI (my Soapui-pro-5.1.2.exe directory is C:\Program files (x86) \smartbear\soapui-pro-5.1.2\lib) and you can see that there are many jar files here. Put the third-party jar (Java.jar/groovy.jar) package in, and put the above three scripts into a Pub.jar, call the following method:

Create a new groovy Script in TestCase and enter the following code:

Import Pub. Text

def text = new text ()

Text.writefile ("D:\1.log", "test")

After execution, you can see that a 1.log file is created under the D disk and written to test;

5. Calling a third-party. class file

Extract the resulting Java.jar file to the. class file, put the extracted folder and the. class file into the ${soapui}/bin/scripts (my Soapui-pro-5.1.2.exe directory is C:\Program files ( x86) \smartbear\soapui-pro-5.1.2\bin\scripts) directory, the calling method is consistent with the. Groovy file

6. Calling a third-party. Groovy file

Place the third. Groovy script in the pub folder and copy it to ${soapui}/bin/scripts (my Soapui-pro-5.1.2.exe directory is C:\Program Files (x86) \smartbear\ soapui-pro-5.1.2\bin\scripts) directory, call the following method:

Import Pub. Text//If this error is changed to import Scripts.pub.Text

def text = new text ()

Text.writefile ("D:\1.log", "test")

After execution, you can see that a 1.log file is created under the D disk and written to test;

7. Talk about invoking third-party jar packages!

Open ${soapui}/bin/ext (My Soapui-pro-5.1.2.exe directory is C:\Program Files (x86) \smartbear\soapui-pro-5.1.2\bin\ EXT) Directory There is a Readme.txt writing a line of birds, roughly meaning that this folder is the location of the external jar file;

It is recommended that all external files be placed under this folder, like. Class/.groovy as much as possible into the jar package placed in the Ext folder; This recommendation is based on the following reasons:

1. Some installed Soapui Bin directory does not have scripts folder (Linux installation or free version soapui), of course, you have to manually create the words and installation automatically generated and no difference;

2. Scripts that run external files under Linux in the Scripts folder will error;

3. The official description of the Ext folder is the place where the external jar package is loaded first when the SOAPUI is started;

4.groovy script to be careful when packaging, Using Eclipse packaging will let us tick the build. class file, do not tick this option, use the original. Groovy file (otherwise the runtime will error); In short, this package is compressed with your compression tool and then renamed to. Jar effect is the same.

By the end of this chapter, the following chapter describes the problems encountered during the use of SOAPUI.

SOAPUI + Groovy Interface Automation test eighth chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.