📄 scriptingtest.java
字号:
} ); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertEquals( "New window message", "You made it!", ((WebWindow) windowsOpened.get( 0 )).getCurrentPage().getText() ); response.getLinks()[1].click(); assertEquals( "Number of window openings", 1, windowsOpened.size() ); assertEquals( "Changed window message", "You changed it!", ((WebWindow) windowsOpened.get( 0 )).getCurrentPage().getText() ); } public void testOpenedWindowProperties() throws Exception { defineResource( "Target.html", "<html><head><script language='JavaScript'>" + "function show_properties() {" + " alert( 'name=' + window.name );" + " alert( 'opener name=' + window.opener.name );" + "}" + "</script></head><body onload='show_properties()'>" + "</body></html>" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body onload=\"window.name='main'; alert ('opener ' + (window.opener ? 'found' : 'not defined') );\">" + "<a href='#' onClick=\"window.open( '" + getHostPath() + "/Target.html', 'sample' );\">go</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "main window name", "main", wc.getMainWindow().getName() ); assertEquals( "main window alert", "opener not defined", wc.popNextAlert() ); response.getLinks()[0].click(); assertEquals( "1st alert", "name=sample", wc.popNextAlert() ); assertEquals( "2nd alert", "opener name=main", wc.popNextAlert() ); } public void testFrameProperties() throws Exception { HttpUnitOptions.setExceptionsThrownOnScriptError( false ); defineWebPage( "Linker", "This is a trivial page with <a href=Target.html>one link</a>" ); defineResource( "Target.html", "<html><head><script language='JavaScript'>" + "function show_properties() {" + " alert( 'name=' + window.name );" + " alert( 'top url=' + window.top.location );" + " alert( '1st frame=' + top.frames[0].name );" + " alert( '2nd frame=' + window.parent.blue.name );" + " alert( 'parent url=' + window.parent.location );" + " alert( 'top.parent=' + top.parent.location );" + " alert( 'indexed frame=' + top.frames['red'].name );" + "}" + "</script></head><body>" + "<a href=# onclick='show_properties()'>show</a>" + "</body></html>" ); defineWebPage( "Form", "This is a page with a simple form: " + "<form action=submit><input name=name><input type=submit></form>" + "<a href=Linker.html target=red>a link</a>"); defineResource( "Frames.html", "<html><head><title>Initial</title></head>" + "<frameset cols='20%,80%'>" + " <frame src='Linker.html' name='red'>" + " <frame src=Target.html name=blue>" + "</frameset></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/Frames.html" ); WebResponse blue = wc.getFrameContents( "blue" ); blue.getLinkWith( "show" ).click(); assertEquals( "1st alert", "name=blue", wc.popNextAlert() ); assertEquals( "2nd alert", "top url=" + getHostPath() + "/Frames.html", wc.popNextAlert() ); assertEquals( "3rd alert", "1st frame=red", wc.popNextAlert() ); assertEquals( "4th alert", "2nd frame=blue", wc.popNextAlert() ); assertEquals( "5th alert", "parent url=" + getHostPath() + "/Frames.html", wc.popNextAlert() ); assertEquals( "6th alert", "top.parent=" + getHostPath() + "/Frames.html", wc.popNextAlert() ); assertEquals( "7th alert", "indexed frame=red", wc.popNextAlert() ); } public void testLocationProperty() throws Exception { defineResource( "Target.html", "You made it!" ); defineResource( "location.js", "function show() {" + "alert('Window location is ' + window.location);" + "alert('Document location is ' + document.location);" + "alert('Window location.href is ' + window.location.href);" + "}" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title>" + "<script language='JavaScript' src='location.js'></script>" + "</head>" + "<body onLoad='show()'>" + "<a href='#' onMouseOver=\"window.location='" + getHostPath() + "/Target.html';\">go</a>" + "<a href='#' onMouseOver=\"document.location='" + getHostPath() + "/Target.html';\">go</a>" + "<a href='#' onMouseOver=\"document.location.replace('" + getHostPath() + "/Target.html');\">go</a>" + "</body>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message 1", "Window location is " + getHostPath() + "/OnCommand.html", wc.popNextAlert() ); assertEquals( "Alert message 2", "Document location is " + getHostPath() + "/OnCommand.html", wc.popNextAlert() ); assertEquals( "Alert message 3", "Window location.href is " + getHostPath() + "/OnCommand.html", wc.popNextAlert() ); response.getLinks()[0].mouseOver(); assertEquals( "2nd page URL", getHostPath() + "/Target.html", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "2nd page", "You made it!", wc.getCurrentPage().getText() ); response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[1].mouseOver(); assertEquals( "3rd page URL", getHostPath() + "/Target.html", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "3rd page", "You made it!", wc.getCurrentPage().getText() ); response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[2].mouseOver(); assertEquals( "4th page URL", getHostPath() + "/Target.html", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "4th page", "You made it!", wc.getCurrentPage().getText() ); response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getScriptableObject().doEvent( "window.location.href='" + getHostPath() + "/Target.html'" ); assertEquals( "5th page URL", getHostPath() + "/Target.html", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "5th page", "You made it!", wc.getCurrentPage().getText() ); } public void testLocationPropertyOnLoad() throws Exception { defineResource( "Target.html", "You made it!" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title>" + "</head>" + "<body onLoad=\"document.location='" + getHostPath() + "/Target.html';\">" + "</body>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "current page URL", getHostPath() + "/Target.html", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "current page", "You made it!", wc.getCurrentPage().getText() ); assertEquals( "returned page URL", getHostPath() + "/Target.html", response.getURL().toExternalForm() ); assertEquals( "returned page", "You made it!", response.getText() ); } public void testLocationReadableSubproperties() throws Exception { defineResource( "Target.html", "You made it!" ); defineResource( "location.js", "function show() {" + "alert('host is ' + window.location.host);" + "alert('hostname is ' + document.location.hostname);" + "alert('port is ' + window.location.port);" + "alert('pathname is ' + window.location.pathname);" + "alert('protocol is ' + document.location.protocol);" + "alert('search is ' + window.location.search);" + "}" ); defineResource( "simple/OnCommand.html?point=center", "<html><head><title>Amazing!</title>" + "<script language='JavaScript' src='/location.js'></script>" + "</head>" + "<body onLoad='show()'>" + "</body>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/simple/OnCommand.html?point=center" ); assertEquals( "Alert message 1", "host is " + getHostPath().substring( 7 ), wc.popNextAlert() ); assertEquals( "Alert message 2", "hostname is localhost", wc.popNextAlert() ); assertEquals( "Alert message 3", "port is " + getHostPort(), wc.popNextAlert() ); assertEquals( "Alert message 4", "pathname is /simple/OnCommand.html", wc.popNextAlert() ); assertEquals( "Alert message 5", "protocol is http:", wc.popNextAlert() ); assertEquals( "Alert message 6", "search is ?point=center", wc.popNextAlert() ); } public void testLocationWriteableSubproperties() throws Exception { defineResource( "Target.html", "You made it!" ); defineResource( "OnCommand.html?where=here", "You found it!" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title>" + "</head>" + "<body'>" + "<a href='#' onMouseOver=\"window.location.pathname='/Target.html';\">go</a>" + "<a href='#' onMouseOver=\"document.location.search='?where=here';\">go</a>" + "</body>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].mouseOver(); assertEquals( "2nd page URL", getHostPath() + "/Target.html", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "2nd page", "You made it!", wc.getCurrentPage().getText() ); response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[1].mouseOver(); assertEquals( "3rd page URL", getHostPath() + "/OnCommand.html?where=here", wc.getCurrentPage().getURL().toExternalForm() ); assertEquals( "3rd page", "You found it!", wc.getCurrentPage().getText() ); } public void testScriptDisabled() throws Exception { HttpUnitOptions.setScriptingEnabled( false ); defineResource( "nothing.html", "Should get here" ); defineResource( "OnCommand.html", "<html><head></head>" + "<body>" + "<form name='realform'><input name='color' value='blue'></form>" + "<a href='nothing.html' onClick=\"document.realform.color.value='green';return false;\">green</a>" + "</body></html>" ); WebConversation wc = new WebConversation(); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); WebForm form = response.getFormWithName( "realform" ); WebLink link = response.getLinks()[0]; assertEquals( "initial parameter value", "blue", form.getParameterValue( "color" ) ); link.click(); assertEquals( "unchanged parameter value", "blue", form.getParameterValue( "color" ) ); assertEquals( "Expected result", "Should get here", wc.getCurrentPage().getText() ); } public void testNavigatorObject() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -