📄 scriptingtest.java
字号:
"function viewProperties() { \n" + " alert( 'appName=' + navigator.appName );\n" + " alert( 'appCodeName=' + navigator.appCodeName )\n;" + " alert( 'appVersion=' + navigator.appVersion )\n;" + " alert( 'userAgent=' + navigator.userAgent )\n;" + " alert( 'platform=' + navigator.platform )\n;" + " alert( 'javaEnabled=' + navigator.javaEnabled() )\n;" + " alert( '# plugins=' + navigator.plugins.length )\n;" + "}" + "</script></head>\n" + "<body onLoad='viewProperties()'>\n" + "</body></html>" ); HttpUnitOptions.setExceptionsThrownOnScriptError( true ); WebConversation wc = new WebConversation(); wc.getClientProperties().setApplicationID( "Internet Explorer", "Mozilla", "4.0" ); wc.getClientProperties().setPlatform( "JVM" ); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message 1", "appName=Internet Explorer", wc.popNextAlert() ); assertEquals( "Alert message 2", "appCodeName=Mozilla", wc.popNextAlert() ); assertEquals( "Alert message 3", "appVersion=4.0", wc.popNextAlert() ); assertEquals( "Alert message 4", "userAgent=Mozilla/4.0", wc.popNextAlert() ); assertEquals( "Alert message 5", "platform=JVM", wc.popNextAlert() ); assertEquals( "Alert message 6", "javaEnabled=false", wc.popNextAlert() ); assertEquals( "Alert message 7", "# plugins=0", wc.popNextAlert() ); assertNull( "Alert should have been removed", wc.getNextAlert() ); } public void testScreenObject() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewProperties() { \n" + " alert( 'dimensions=' + screen.availWidth + 'x' + screen.availHeight );\n" + "}" + "</script></head>\n" + "<body onLoad='viewProperties()'>\n" + "</body></html>" ); HttpUnitOptions.setExceptionsThrownOnScriptError( true ); WebConversation wc = new WebConversation(); wc.getClientProperties().setAvailableScreenSize( 1024, 752 ); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message 1", "dimensions=1024x752", wc.popNextAlert() ); assertNull( "Alert should have been removed", wc.getNextAlert() ); } public void testStyleProperty() throws Exception { defineResource( "start.html", "<html><head><script language='JavaScript'>" + "function showDisplay( id ) {" + " var element = document.getElementById( id );\n" + " alert( 'element with id ' + id + ' has style.display ' + element.style.display );\n" + "}\n" + "function setDisplay( id, value ) {" + " var element = document.getElementById( id );\n" + " element.style.display = value;\n" + "}\n" + "function showVisibility( id ) {" + " var element = document.getElementById( id );\n" + " alert( 'element with id ' + id + ' has style.visibility ' + element.style.visibility );\n" + "}\n" + "function setVisibility( id, value ) {" + " var element = document.getElementById( id );\n" + " element.style.visibility = value;\n" + "}\n" + "function doAll() {\n" + " setDisplay('test','inline'); \n" + " showDisplay('test');\n" + " setDisplay('test','block'); \n" + " showDisplay('test');\n" + " setVisibility('test','hidden'); \n" + " showVisibility('test');\n" + " setVisibility('test','visible'); \n" + " showVisibility('test');\n" + "}\n" + "</script>" + "</head><body onLoad='doAll();'>" + "<div id='test'>foo</div></body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/start.html" ); assertEquals( "element with id test has style.display inline", wc.popNextAlert() ); assertEquals( "element with id test has style.display block", wc.popNextAlert() ); assertEquals( "element with id test has style.visibility hidden", wc.popNextAlert() ); assertEquals( "element with id test has style.visibility visible", wc.popNextAlert() ); } public void testTagNameNodeNameProperties() throws Exception { defineResource( "start.html", "<html><head><script language='JavaScript'>\n" + "function showTagName(id) {\n" + " var element = document.getElementById( id );\n" + " alert( 'element id=' + id + ', tagName=' + element.tagName + ', nodeName=' + element.nodeName );\n" + "}\n" + "function doAll() {\n" + " showTagName('body_id')\n" + " showTagName('iframe_id')\n" + " showTagName('div_id')\n" + "}\n" + "</script>\n" + "</head><body id='body_id' onLoad='doAll();'>\n" + "<div id='div_id'><iframe id='iframe_id' /></div>\n" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/start.html" ); assertEquals( "element id=body_id, tagName=BODY, nodeName=BODY", wc.popNextAlert() ); assertEquals( "element id=iframe_id, tagName=IFRAME, nodeName=IFRAME", wc.popNextAlert() ); assertEquals( "element id=div_id, tagName=DIV, nodeName=DIV", wc.popNextAlert() ); } public void testReadNoCookie() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewCookies() { \n" + " alert( 'cookies: ' + document.cookie );\n" + "}" + "</script></head>\n" + "<body onLoad='viewCookies()'>\n" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message 1", "cookies: ", wc.popNextAlert() ); assertNull( "Alert should have been removed", wc.getNextAlert() ); } public void testSimpleSetCookie() throws Exception { defineResource( "OnCommand.html", "<html><head></head>\n" + "<body onLoad='document.cookie=\"color=red;path=/\"'>\n" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Cookie 'color'", "red", wc.getCookieValue( "color" ) ); } public void testSetCookieToNull() throws Exception { defineResource( "OnCommand.html", "<html><script>" + "document.cookie = null;" + "</script></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); } public void testReadCookies() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "function viewCookies() { \n" + " alert( 'cookies: ' + document.cookie );\n" + "}" + "</script></head>\n" + "<body onLoad='viewCookies()'>\n" + "</body></html>" ); addResourceHeader( "OnCommand.html", "Set-Cookie: age=12"); WebConversation wc = new WebConversation(); wc.putCookie( "height", "tall" ); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message 1", "cookies: age=12; height=tall", wc.popNextAlert() ); assertNull( "Alert should have been removed", wc.getNextAlert() ); } public void testButtonWithoutForm() throws Exception { defineWebPage( "OnCommand", "<button id='mybutton' onclick='alert( \"I heard you!\" )'>" + "<input id='yourbutton' type='button' onclick='alert( \"Loud and Clear.\" )'>"); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); ((Button) response.getElementWithID( "mybutton" )).click(); assertEquals( "Alert message 1", "I heard you!", wc.popNextAlert() ); ((Button) response.getElementWithID( "yourbutton" )).click(); assertEquals( "Alert message 2", "Loud and Clear.", wc.popNextAlert() ); } public void testJavascriptDetectionTrick() throws Exception { defineResource( "NoScript.html", "No javascript here" ); defineResource( "HasScript.html", "Javascript is enabled!" ); defineResource( "Start.html", "<html><head>" + " <noscript>" + " <meta http-equiv='refresh' content='0;url=NoScript.html>'" + " </noscript></head>" + "<body onload='document.form.submit()'>" + "<form name='form' action='HasScript.html'></form>" + "</body></html>" ); WebConversation wc = new WebConversation(); wc.getClientProperties().setAutoRefresh( true ); WebResponse response = wc.getResponse( getHostPath() + "/Start.html" ); assertEquals( "Result page", "Javascript is enabled!", response.getText() ); HttpUnitOptions.setScriptingEnabled( false ); response = wc.getResponse( getHostPath() + "/Start.html" ); assertEquals( "Result page", "No javascript here", response.getText() ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -