2014-07-02 137 views
2

我需要發送一個http請求,其中包含從文件中提取的cookie頭集。我已經知道如何讀取文件並提取cookie,但不知道如何將它傳遞給請求。如何使用Groovy測試步驟在soapUI中設置Cookie?

我已經寫了下面的一段代碼:

Random random = new Random(); 
String line = random.nextInt(3)+1; 
def testCase = testRunner.testCase; 
File f = new File("F:/data.txt"); 
cookies = f.readLines(); 
String cookie = cookies.getAt(line); 
// I can read the headers 
def headerWatch = testRunner.testCase.testSteps['HTTP Test Request'].testRequest.response.getRequestHeaders() 
log.info(headerWatch) 
// TODO : set cookie to request 

回答

2

要創建一個新的cookie,你可以使用以下命令:

import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport 
def myCookieStore = HttpClientSupport.getHttpClient().getCookieStore() 

import org.apache.http.impl.cookie.BasicClientCookie 
def myNewCookie = new BasicClientCookie("cookie_name", "cookie_value") 
myNewCookie.version = 1 
myNewCookie.domain = "qa.test" 
myCookieStore.addCookie(myNewCookie) 

上的cookie的更多信息,請訪問:http://siking.wordpress.com/2013/07/25/soapui-cookie-management/

+1

後,我添加這些代碼,它拋出一個錯誤:無法解析類org.apache.http.impl.Cookie.BasicClientCookie – wormchaos

+0

什麼了SoapUI的版本? – SiKing

+0

我使用soapUI 5.0.0,最新版本 – wormchaos

2

我在使用soapui導入BasicClientCookie類時遇到了同樣的錯誤。導入區分大小寫,Cookie中的'C'應該小寫。

import org.apache.http.impl.cookie.BasicClientCookie 
相關問題