如何使用Groovy测试步骤在soapUI中设置Cookie?

时间:2014-07-02 07:42:58

标签: groovy soapui

我需要发送一个带有cookie标头集的http请求,该标头取自文件。我已经知道如何阅读文件并提取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 个答案:

答案 0 :(得分: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 :(得分:2)

我在soapui中导入BasicClientCookie类时遇到了同样的错误。导入区分大小写,并且' C'在Cookie中应该是小写的。

import org.apache.http.impl.cookie.BasicClientCookie