📄 scriptingtest.java
字号:
assertEquals( "Confirmation prompt", "go on?", prompt ); assertEquals( "Default response", "no", defaultResponse ); return "yes"; } } ); wr.getLinkWithID( "go" ).click(); assertEquals( "Page after confirmation", "Got the next page!", wc.getCurrentPage().getText() ); } public void testFunctionCallOnLoad() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'>" + "<!-- hide this\n" + "function sayCheese() { alert( \"Cheese!\" ); }" + "// end hiding -->\n" + "</script></head>" + "<body'><script language='JavaScript'>\n" + "<!-- hide this\n" + "sayCheese();" + "-->" + "</script></body></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message", "Cheese!", wc.popNextAlert() ); } public void testComment() throws Exception { defineResource( "OnCommand.html", "<html><head><script language='JavaScript'><!--" + "//--></script><script language='JavaScript'>" + "\n" + "var n=0;" + "\n" + "parseInt(n,32);" + "</script></head></html>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); } public void testIncludedFunction() throws Exception { defineResource( "saycheese.js", "function sayCheese() { alert( \"Cheese!\" ); }" ); defineResource( "OnCommand.html", "<html><head><script language='JavaScript' src='saycheese.js'>" + "</script></head>" + "<body onLoad='sayCheese()'></body>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message", "Cheese!", wc.popNextAlert() ); } public void testIncludedFunctionWithBaseTag() throws Exception { defineResource( "scripts/saycheese.js", "function sayCheese() { alert( \"Cheese!\" ); }" ); defineResource( "OnCommand.html", "<html><head><base href='" + getHostPath() + "/scripts/OnCommand.html'><script language='JavaScript' src='saycheese.js'>" + "</script></head>" + "<body onLoad='sayCheese()'></body>" ); WebConversation wc = new WebConversation(); wc.getResponse( getHostPath() + "/OnCommand.html" ); assertEquals( "Alert message", "Cheese!", wc.popNextAlert() ); } public void testWindowOpen() throws Exception { defineResource( "Target.txt", "You made it!", "text/plain" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body><script language='JavaScript'>var otherWindow;</script>" + "<a href='#' onClick=\"otherWindow = window.open( '" + getHostPath() + "/Target.txt', 'sample' );\">go</a>" + "<a href='#' onClick=\"otherWindow.close();\">go</a>" + "<a href='#' onClick=\"alert( 'window is ' + (otherWindow.closed ? '' : 'not ') + 'closed' );\">go</a>" + "</body></html>" ); final ArrayList windowsOpened = new ArrayList(); WebConversation wc = new WebConversation(); wc.addWindowListener( new WebWindowListener() { public void windowOpened( WebClient client, WebWindow window ) { windowsOpened.add( window ); } public void windowClosed( WebClient client, WebWindow window ) {} } ); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertFalse( "No window opened", windowsOpened.isEmpty() ); final WebWindow openedWindow = (WebWindow) windowsOpened.get( 0 ); assertEquals( "New window message", "You made it!", openedWindow.getCurrentPage().getText() ); assertEquals( "New window name", "sample", openedWindow.getName() ); response.getLinks()[2].click(); assertEquals( "Alert message", "window is not closed", wc.popNextAlert() ); response.getLinks()[1].click(); assertTrue( "Window was not closed", openedWindow.isClosed() ); response.getLinks()[2].click(); assertEquals( "Alert message", "window is closed", wc.popNextAlert() ); } public void testWindowOpenWithEmptyName() throws Exception { defineResource( "Target.txt", "You made it!", "text/plain" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body><script language='JavaScript'>var otherWindow;</script>" + "<a href='#' onClick=\"otherWindow = window.open( '" + getHostPath() + "/Target.txt', '' );\">go</a>" + "<a href='#' onClick=\"otherWindow.close();\">go</a>" + "<a href='#' onClick=\"alert( 'window is ' + (otherWindow.closed ? '' : 'not ') + 'closed' );\">go</a>" + "</body></html>" ); final ArrayList windowsOpened = new ArrayList(); WebConversation wc = new WebConversation(); wc.addWindowListener( new WebWindowListener() { public void windowOpened( WebClient client, WebWindow window ) { windowsOpened.add( window ); } public void windowClosed( WebClient client, WebWindow window ) {} } ); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertFalse( "No window opened", windowsOpened.isEmpty() ); final WebWindow openedWindow = (WebWindow) windowsOpened.get( 0 ); assertEquals( "New window message", "You made it!", openedWindow.getCurrentPage().getText() ); assertEquals( "New window name", "", openedWindow.getName() ); response.getLinks()[2].click(); assertEquals( "Alert message", "window is not closed", wc.popNextAlert() ); response.getLinks()[1].click(); assertTrue( "Window was not closed", openedWindow.isClosed() ); response.getLinks()[2].click(); assertEquals( "Alert message", "window is closed", wc.popNextAlert() ); } public void testWindowOpenWithSelf() throws Exception { defineResource( "Target.txt", "You made it!", "text/plain" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body><script language='JavaScript'>var otherWindow;</script>" + "<a href='#' onClick=\"otherWindow = window.open( '" + getHostPath() + "/Target.txt', '_self' );\">go</a>" + "<a href='#' onClick=\"otherWindow.close();\">go</a>" + "<a href='#' onClick=\"alert( 'window is ' + (otherWindow.closed ? '' : 'not ') + 'closed' );\">go</a>" + "</body></html>" ); final ArrayList windowsOpened = new ArrayList(); WebConversation wc = new WebConversation(); wc.addWindowListener( new WebWindowListener() { public void windowOpened( WebClient client, WebWindow window ) { windowsOpened.add( window ); } public void windowClosed( WebClient client, WebWindow window ) {} } ); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertTrue( "Opened a new window", windowsOpened.isEmpty() ); assertEquals( "New window message", "You made it!", wc.getCurrentPage().getText() ); assertEquals( "Number of open windows", 1, wc.getOpenWindows().length ); } public void testJavascriptURLWithFragment() throws Exception { defineResource( "Target.txt", "You made it!", "text/plain" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body><script language='JavaScript'>function newWindow(hrefTarget) {" + " window.open(hrefTarget);" + "}</script>" + "<a href='javascript:newWindow( \"" + getHostPath() + "/Target.txt#middle\" );'>go</a>" + "</body></html>" ); final ArrayList windowsOpened = new ArrayList(); WebConversation wc = new WebConversation(); wc.addWindowListener( new WebWindowListener() { public void windowOpened( WebClient client, WebWindow window ) { windowsOpened.add( window ); } public void windowClosed( WebClient client, WebWindow window ) {} } ); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertFalse( "No window opened", windowsOpened.isEmpty() ); final WebWindow openedWindow = (WebWindow) windowsOpened.get( 0 ); assertEquals( "New window message", "You made it!", openedWindow.getCurrentPage().getText() ); } public void testWindowOpenNoContents() throws Exception { defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body>" + "<a href='#' onClick=\"window.open( null, 'sample' );\">go</a>" + "</body></html>" ); final ArrayList windowsOpened = new ArrayList(); WebConversation wc = new WebConversation(); wc.addWindowListener( new WebWindowListener() { public void windowOpened( WebClient client, WebWindow window ) { windowsOpened.add( window ); } public void windowClosed( WebClient client, WebWindow window ) {} } ); WebResponse response = wc.getResponse( getHostPath() + "/OnCommand.html" ); response.getLinks()[0].click(); assertFalse( "No window opened", windowsOpened.isEmpty() ); final WebWindow openedWindow = (WebWindow) windowsOpened.get( 0 ); assertEquals( "New window message", "", openedWindow.getCurrentPage().getText() ); assertEquals( "New window name", "sample", openedWindow.getName() ); assertEquals( "Window by name", openedWindow, wc.getOpenWindow( "sample" ) ); } public void testWindowReopen() throws Exception { defineResource( "Target.html", "You made it!" ); defineResource( "Revise.html", "You changed it!" ); defineResource( "OnCommand.html", "<html><head><title>Amazing!</title></head>" + "<body>" + "<a href='#' onClick=\"window.open( '" + getHostPath() + "/Target.html', 'sample' );\">go</a>" + "<a href='#' onClick=\"window.open( '" + getHostPath() + "/Revise.html', 'sample' );\">go</a>" + "</body></html>" ); final ArrayList windowsOpened = new ArrayList(); WebConversation wc = new WebConversation(); wc.addWindowListener( new WebWindowListener() { public void windowOpened( WebClient client, WebWindow window ) { windowsOpened.add( window ); } public void windowClosed( WebClient client, WebWindow window ) {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -