[Httpunit-commit] CVS: httpunit/src/com/meterware/httpunit ScriptEngine.java,1.1,1.2 ScriptableObjec
Brought to you by:
russgold
Update of /cvsroot/httpunit/httpunit/src/com/meterware/httpunit In directory usw-pr-cvs1:/tmp/cvs-serv24182/src/com/meterware/httpunit Modified Files: ScriptEngine.java ScriptableObject.java WebForm.java WebLink.java WebRequestSource.java WebResponse.java Log Message: Added support for link.onClick and form.onSubmit events Index: ScriptEngine.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ScriptEngine.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- ScriptEngine.java 24 Jul 2002 20:54:31 -0000 1.1 +++ ScriptEngine.java 6 Aug 2002 19:14:54 -0000 1.2 @@ -31,4 +31,7 @@ public void executeScript( String script ); + + public boolean performEvent( String eventScript ); + } Index: ScriptableObject.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/ScriptableObject.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- ScriptableObject.java 24 Jul 2002 20:54:31 -0000 1.2 +++ ScriptableObject.java 6 Aug 2002 19:14:55 -0000 1.3 @@ -33,9 +33,19 @@ /** * Executes the specified scripted event. **/ - public void doEvent( String eventScript ) { + public boolean doEvent( String eventScript ) { if (_scriptEngine == null) throw new IllegalStateException( "Script engine must be defined before running an event" ); - _scriptEngine.executeScript( eventScript ); + if (eventScript.length() == 0) return true; + return _scriptEngine.performEvent( eventScript ); + } + + + /** + * Executes the specified script. + **/ + public void runScript( String script ) { + if (_scriptEngine == null) throw new IllegalStateException( "Script engine must be defined before running an event" ); + _scriptEngine.executeScript( script ); } Index: WebForm.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebForm.java,v retrieving revision 1.53 retrieving revision 1.54 diff -u -r1.53 -r1.54 --- WebForm.java 5 Aug 2002 15:20:50 -0000 1.53 +++ WebForm.java 6 Aug 2002 19:14:55 -0000 1.54 @@ -47,7 +47,9 @@ * Submits this form using the web client from which it was originally obtained. **/ public WebResponse submit() throws IOException, SAXException { - return submitRequest(); + String event = NodeUtils.getNodeAttribute( getNode(), "onsubmit" ); + if (event.length() == 0 || getScriptableObject().doEvent( event )) return submitRequest(); + return getBaseResponse(); } @@ -285,7 +287,8 @@ * Returns an object which provides scripting access to this form. **/ public Scriptable getScriptableObject() { - return new Scriptable(); + if (_scriptable == null) _scriptable = new Scriptable(); + return _scriptable; } //---------------------------------- WebRequestSource methods -------------------------------- @@ -452,6 +455,9 @@ /** A map of parameter names to form parameter objects. **/ private Map _formParameters; + + /** The Scriptable object associated with this form. **/ + private Scriptable _scriptable; private Vector _buttonVector; Index: WebLink.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebLink.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- WebLink.java 5 Aug 2002 15:21:19 -0000 1.22 +++ WebLink.java 6 Aug 2002 19:14:56 -0000 1.23 @@ -79,7 +79,9 @@ * Submits a request as though the user had clicked on this link. Will also fire the 'onClick' event if defined. **/ public WebResponse click() throws IOException, SAXException { - return submitRequest(); + String event = NodeUtils.getNodeAttribute( getNode(), "onclick" ); + if (event.length() == 0 || getScriptableObject().doEvent( event )) return submitRequest(); + return getBaseResponse(); } Index: WebRequestSource.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebRequestSource.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- WebRequestSource.java 5 Aug 2002 15:20:51 -0000 1.10 +++ WebRequestSource.java 6 Aug 2002 19:14:56 -0000 1.11 @@ -174,7 +174,18 @@ **/ final protected WebResponse submitRequest() throws IOException, SAXException { - return _baseResponse.getClient().sendRequest( getRequest() ); + return getDestination().equals( "#" ) + ? _baseResponse + : _baseResponse.getClient().sendRequest( getRequest() ); + } + + + /** + * Returns the web response containing this request source. + */ + final + protected WebResponse getBaseResponse() { + return _baseResponse; } Index: WebResponse.java =================================================================== RCS file: /cvsroot/httpunit/httpunit/src/com/meterware/httpunit/WebResponse.java,v retrieving revision 1.71 retrieving revision 1.72 diff -u -r1.71 -r1.72 --- WebResponse.java 5 Aug 2002 17:34:25 -0000 1.71 +++ WebResponse.java 6 Aug 2002 19:14:57 -0000 1.72 @@ -501,7 +501,7 @@ public void load() throws SAXException { - doEvent( getReceivedPage().getScripts() ); + runScript( getReceivedPage().getScripts() ); doEvent( getReceivedPage().getOnLoadEvent() ); } } |