2014-08-28 100 views
1

我是SoapUI和Groovy的新手,所以請原諒這篇文章,因爲它已經在Stackoverflow中發佈了很多次,但是我找不到修復程序。在SoapUI中執行Java jar文件groovy腳本不能正常工作

了SoapUI版本:4.5.2

我有2個問題,如果你們不介意的話:

  1. 我有我已經把在的\ bin \分機可執行的JAR文件目錄以及另一個被認爲是jar中的代碼中的依賴jar的jar,所以我希望它會在那裏引用。我在Stackoverflow中發現的應該執行這個jar的Groovy代碼如下,但不起作用,因爲我在SoapUI目錄中的任何地方都沒有看到任何輸出。

下面是代碼:

def command = "java -jar UpdateAppIdXMLRequest.jar file.xml" 
def process = command.execute() 
process.waitFor() 

def output = process.in.text 
log.info output 
  1. 這個jar創建25個xml文件,應該可以通過了SoapUI被拾起,並把它們作爲在同一個項目TestSteps。在我的java代碼中,我將這些文件放在什麼路徑中?

這裏是我的罐子代碼:

import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 

import org.jdom2.Document; 
import org.jdom2.Element; 
import org.jdom2.JDOMException; 
import org.jdom2.input.SAXBuilder; 
import org.jdom2.output.Format; 
import org.jdom2.output.XMLOutputter; 

class UpdateAppIdXMLRequest { 

    static main(args) { 

     try { 

      SAXBuilder builder = new SAXBuilder(); 
      File xmlFile = new File("c:\\file.xml"); 

      Document doc = (Document) builder.build(xmlFile); 
      Element rootNode = doc.getRootElement(); 

      // Create loop to create 25 testStepApps 
      for (int i = 1; i < 26; i++) { 

       // Get current AppID, incrementAppID and update the ApplicationNumber attribute value for next test script. 
       int appID = Integer.parseInt(rootNode.getAttributeValue("ApplicationNumber")); 
       appID++; 
       String appIDValue = Integer.toString(appID); 
       rootNode.getAttribute("ApplicationNumber").setValue(appIDValue); 

       XMLOutputter xmlOutput = new XMLOutputter(); 

       // Create new XML file with next AppID 
       xmlOutput.setFormat(Format.getPrettyFormat()); 
       xmlOutput.output(doc, new FileWriter("c:\\testStepApp" + i + ".xml")); 

       // xmlOutput.output(doc, System.out); 

       // System.out.println("File updated!"); 
      } 
     } catch (IOException io) { 
      io.printStackTrace(); 
     } catch (JDOMException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

任何幫助/方向,將不勝感激。

謝謝。

+0

我不知道你的jar在做什麼,所以當你執行groovy測試步驟時,當你打印'log.info output'時,你在這個測試步驟的'Log Output'選項卡上看到了什麼? – albciff 2014-08-29 08:09:58

+0

感謝您的回覆。正如我所說的,我對groovy瞭解不多,但是我正在閱讀代碼,log.info命令將採用參數「output」,該參數定義在這行代碼的上方。這是不正確的?再次感謝。 – Melinda 2014-08-29 18:35:41

+0

我沒有說代碼是錯的。爲了幫助你,我想知道groovy腳本執行時打印的日誌。 'log.info'將信息打印在'Log Output'選項卡中,該選項卡位於SOAPUI中的groovy測試步驟窗口的後面。有人可以幫助你的其他可能性是你分享'.jar'代碼。 – albciff 2014-08-29 22:15:37

回答

2

爲了做到這一點,我建議您直接使用groovy測試步驟而不是jar,這樣您可以擁有更大的靈活性,每次您必須重新編譯jar時,您都必須更改某些內容。

因此,爲了實現自己的目標,首先需要創建一個TestCase你的項目中,創建一個SOAP Test StepGroovy Test Step裏面是這樣的:

enter image description here

我會用SOAP Test Step創建其他測試步驟(創建測試步驟需要wsdl:operation等等,並且更容易複製直接創建的測試步驟)。

Groovy Test Step我會把必要的代碼做這表明以下工作:

import com.eviware.soapui.support.XmlHolder 
import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory 

// read your request template 
def requestFile = new File("C:/file.xml"); 
// parse it as xml 
def requestXml = new XmlHolder(requestFile.text) 

// get the current testCase to add testSteps later 
def tc = testRunner.testCase; 
// get the testStep as template to create the other requests 
def tsTemplate = tc.getTestStepByName("Template"); 

// loop to create 25 testStep 
for(int i = 1; i < 26; i++){ 
    // xpath expression to get applicationNumber attribute in root node 
    def xpathNodeAttr = "/*/@ApplicationNumber"; 

    // get the root node attribute applicationNumber throught an XPATH 
    int appId = Integer.parseInt(requestXml.getNodeValue(xpathNodeAttr)); 
    // add 1 to appId 
    appId++; 
    // set the value again in the attribute 
    requestXml.setNodeValue(xpathNodeAttr,appId); 


    def testStepName = "TestStep_ApplicationNumber_"+ String.valueOf(appId) 
    log.info testStepName; 
    log.info testStepName.getClass().getName() 
    log.info tc.getClass().getName() 
    // create a new testStepConfig 
    def testStepFactory = new WsdlTestRequestStepFactory(); 
    def testStepConfig = testStepFactory.createConfig(tsTemplate.getOperation(), testStepName) 
    // add the new testStep to TestCase 
    def newTestStep = tc.insertTestStep(testStepConfig, -1) 
    // set the request which just create 
    newTestStep.getTestRequest().setRequestContent(requestXml.getXml()) 
} 

此代碼它基本上你java代碼「翻譯」到groovy並添加必要的代碼來創建測試腳步。簡而言之,這段代碼從文件中讀取一個請求,並在當前測試用例中使用請求創建25個測試步驟,在每個請求中它只改變根節點的ApplicationNumber屬性,並將其添加+1。

基於編輯簡評:

如果您使用的REST Request step代替SOAP Request Step你必須改變你的一點Groovy代碼使用com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactorygetTestRequest()方法就可以了。所以,如果你有一個REST服務與POST方法創建一個REST Request test stepGroovy Test Step這樣一個Test Case

enter image description here

,並使用此Groovy代碼代替,基本上是這樣的代碼是相同的,就像上面的代碼並使得同樣的事情,而不是REST RequestSOAP Request

import com.eviware.soapui.support.XmlHolder 
import com.eviware.soapui.impl.wsdl.teststeps.registry.RestRequestStepFactory 

// read your request template 
def requestFile = new File("C:/file.xml"); 
// parse it as xml 
def requestXml = new XmlHolder(requestFile.text) 

// get the current testCase to add testSteps later 
def tc = testRunner.testCase; 
// get the testStep as template to create the other requests 
def tsTemplate = tc.getTestStepByName("Template"); 

// loop to create 25 testStep 
for(int i = 1; i < 26; i++){ 
    // xpath expression to get applicationNumber attribute in root node 
    def xpathNodeAttr = "/*/@ApplicationNumber"; 

    // get the root node attribute applicationNumber throught an XPATH 
    int appId = Integer.parseInt(requestXml.getNodeValue(xpathNodeAttr)); 
    // add 1 to appId 
    appId++; 
    // set the value again in the attribute 
    requestXml.setNodeValue(xpathNodeAttr,appId); 


    def testStepName = "TestStep_ApplicationNumber_"+ String.valueOf(appId) 
    log.info testStepName; 
    log.info testStepName.getClass().getName() 
    log.info tc.getClass().getName() 
    // create a new testStepConfig 
    def testStepFactory = new RestRequestStepFactory(); 
    def testStepConfig = testStepFactory.createConfig(tsTemplate.getTestRequest(), testStepName) 
    // add the new testStep to TestCase 
    def newTestStep = tc.insertTestStep(testStepConfig, -1) 
    // set the request which just create 
    newTestStep.getTestRequest().setRequestContent(requestXml.getXml()) 
} 

希望這有助於。

+0

OMG!非常感謝。我感謝你的幫助,更重要的是你的時間。我會試一試,希望這能幫助我們在SoapUI工作的QA人員。我試圖寫出一些Java代碼來幫助他,因爲他不是開發人員。再次感謝。 – Melinda 2014-09-03 14:35:32

+0

運行REST應用程序時出現問題。這是錯誤:發生錯誤[方法的簽名:com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory.createConfig()適用於參數類型:(com.eviware.soapui.impl.rest.RestResource ,java.lang.String)values:[[email protected],TestStep_ApplicationNumber_65244040] 可能的解決方案:createConfig(com.eviware.soapui.impl.wsdl.WsdlOperation,java.lang.String) ,createConfig(com.eviware.soapui.impl.wsdl.WsdlRequest,java.lang.String)],請參閱錯誤日誌以獲取詳細信息 – Melinda 2014-09-04 15:23:22

+0

我認爲您使用SOAP測試請求是因爲您處理了xml請求,但是您正在使用一個REST資源,所以你需要一種不同的方式來創建測試步驟,因爲我的代碼是創建SOAP測試請求。我更新答案以添加適用於REST請求而不是SOAP請求的groovy代碼,請看一下。我希望這次能實現你的目標':)' – albciff 2014-09-05 08:45:58