[Webtest] javascript and webtest

Marc Guillemot webtest@lists.canoo.com
Thu, 29 Jun 2006 04:52:13 -0700 (PDT)


Hi Stefanie,

here is an example changing the string "User ID" to "toto User toto Id" in
received responses before the content gets parsed (works with latest build
using htmlunit 1.9, if you want to use an older version you need to look at
the API as a bit more glue code is needed):

<groovy>
import com.gargoylesoftware.htmlunit.*

def webClient = (WebClient) step.context.webClient

class FixerWebConnection extends WebConnectionWrapper
{
	FixerWebConnection(WebConnection _wrappedConnection)
	{
		super(_wrappedConnection)
	}

	WebResponse getResponse(WebRequestSettings settings)
	{
		WebResponse response = super.getResponse(settings)
		
		def text = response.getContentAsString()
		if (text.contains('User ID'))
		{
			text = text.replaceAll('User ID', 'toto User Toto Id')
			def newResponseData = new
WebResponseData(TextUtil.stringToByteArray(text), response.statusCode,
response.statusMessage, response.responseHeaders)
			response = new WebResponseImpl(newResponseData, response.url,
response.requestMethod, response.loadTimeInMilliSeconds)
		}
		return response
	}
}

webClient.webConnection = new FixerWebConnection((WebConnection)
webClient.webConnection)
</groovy>

Marc.
-- 
View this message in context: http://www.nabble.com/Re%3A-javascript-and-webtest-tf1835303.html#a5101348
Sent from the WebTest forum at Nabble.com.