[Webtest] javascript and webtest
Stefanie Wilms
webtest@lists.canoo.com
Tue, 11 Jul 2006 23:12:20 +0200
Hi Marc,
I tried your suggestion and it seems to work in general. My problem is
that the resulting page after a 'setSelectField'-step creates still the
same error message about the unterminated string before groovy has a
chance to change the parts of the source code which are causing the trouble.
Stefanie
Marc Guillemot wrote:
> 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.