📄 webframetest.java
字号:
public void testGetSubframes() throws Exception { WebResponse response = _wc.getResponse( getHostPath() + "/Frames.html" ); assertEquals( "red subframe", _wc.getFrameContents( "red" ), response.getSubframeContents( "red" ) ); } public void testNestedSubFrames() throws Exception { defineResource( "SuperFrames.html", "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" + "<FRAMESET cols=\"50%,50%\">" + " <FRAME src=\"Frames.html\" name=\"crimson\">" + " <FRAME src=\"Form.html\" name=\"blue\">" + "</FRAMESET></HTML>" ); WebResponse response = _wc.getResponse( getHostPath() + "/SuperFrames.html" ); WebResponse frameContents = _wc.getFrameContents( "red" ); WebResponse subframeContents = response.getSubframeContents( "crimson" ).getSubframeContents( "red" ); assertEquals( "crimson.red subframe", frameContents, subframeContents ); } public void testNestedCrossFrameLinks() throws Exception { defineResource( "SuperFrames.html", "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" + "<FRAMESET cols=\"50%,50%\">" + " <FRAME src=\"Frames.html\" name=\"red\">" + " <FRAME src=\"Frames.html\" name=\"blue\">" + "</FRAMESET></HTML>" ); _wc.getResponse( getHostPath() + "/SuperFrames.html" ); FrameSelector nestedRedFrame = _wc.getFrameContents( "red" ).getSubframeContents( "red" ).getFrame(); FrameSelector nestedBlueFrame = _wc.getFrameContents( "red" ).getSubframeContents( "blue" ).getFrame(); _wc.getResponse( _wc.getFrameContents( nestedRedFrame ).getLinks()[0].getRequest() ); WebResponse frameContent = _wc.getFrameContents( nestedBlueFrame ).getLinks()[0].click(); assertTrue( "Second response not the same as source frame contents", frameContent == _wc.getFrameContents( nestedRedFrame ) ); assertEquals( "URL for second request", getHostPath() + "/Linker.html", frameContent.getURL().toExternalForm() ); } public void testCrossLevelLinks() throws Exception { defineResource( "SuperFrames.html", "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" + "<FRAMESET cols=\"50%,50%\">" + " <FRAME src=\"Frames.html\" name=\"red\">" + " <FRAME src=\"Frames.html\" name=\"blue\">" + "</FRAMESET></HTML>" ); _wc.getResponse( getHostPath() + "/SuperFrames.html" ); FrameSelector nestedRedFrame = _wc.getFrameContents( "red" ).getSubframeContents( "red" ).getFrame(); _wc.getFrameContents( nestedRedFrame ).getLinks()[0].click(); WebResponse frameContent = _wc.getResponse( _wc.getFrameContents( nestedRedFrame ).getLinks()[0].getRequest() ); assertTrue( "Second response not the same as source frame contents", frameContent == _wc.getFrameContents( "_top" ) ); assertEquals( "URL for second request", getHostPath() + "/Form.html", frameContent.getURL().toExternalForm() ); assertEquals( "Number of active frames", 1, _wc.getFrameNames().length ); } public void testLinkToTopFrame() throws Exception { WebResponse response = _wc.getResponse( getHostPath() + "/Frames.html" ); response = _wc.getResponse( _wc.getFrameContents( "red" ).getLinks()[0].getRequest() ); response = _wc.getResponse( response.getLinks()[0].getRequest() ); assertTrue( "Second response not the same as source frame contents", response == _wc.getFrameContents( "_top" ) ); assertEquals( "URL for second request", getHostPath() + "/Form.html", response.getURL().toExternalForm() ); assertMatchingSet( "Frames defined for the conversation", new String[] { "_top" }, _wc.getFrameNames() ); } public void testEmptyFrame() throws Exception { defineResource( "HalfFrames.html", "<HTML><HEAD><TITLE>Initial</TITLE></HEAD>" + "<FRAMESET cols=\"20%,80%\">" + " <FRAME src=\"Linker.html\" name=\"red\">" + " <FRAME name=blue>" + "</FRAMESET></HTML>" ); _wc.getResponse( getHostPath() + "/HalfFrames.html" ); WebResponse response = _wc.getFrameContents( "blue" ); assertNotNull( "Loaded nothing for the empty frame", response ); assertEquals( "Num links", 0, response.getLinks().length ); } public void testSelfTargetLink() throws Exception { defineWebPage( "Linker", "This is a trivial page with <a href=Target.html target=_self>one link</a>" ); _wc.getResponse( getHostPath() + "/Frames.html" ); WebResponse response = _wc.getResponse( _wc.getFrameContents( "red" ).getLinks()[0].getRequest() ); assertMatchingSet( "Frames defined for the conversation", new String[] { "_top", "red", "blue" }, _wc.getFrameNames() ); assertTrue( "Second response not the same as source frame contents", response == _wc.getFrameContents( "red" ) ); assertEquals( "URL for second request", getHostPath() + "/Target.html", response.getURL().toExternalForm() ); } public void testSelfTargetForm() throws Exception { defineWebPage( "Linker", "<form action=redirect.html target=_self><input type=text name=sample value=z></form>" ); defineResource( "redirect.html?sample=z", "", HttpURLConnection.HTTP_MOVED_PERM ); addResourceHeader( "redirect.html?sample=z", "Location: " + getHostPath() + "/Target.html" ); _wc.getResponse( getHostPath() + "/Frames.html" ); WebResponse response = _wc.getResponse( _wc.getFrameContents( "red" ).getForms()[0].getRequest() ); assertMatchingSet( "Frames defined for the conversation", new String[] { "_top", "red", "blue" }, _wc.getFrameNames() ); assertTrue( "Second response not the same as source frame contents", response == _wc.getFrameContents( "red" ) ); assertEquals( "URL for second request", getHostPath() + "/Target.html", response.getURL().toExternalForm() ); } public void testSubFrameRedirect() throws Exception { defineResource( "Linker.html", "", HttpURLConnection.HTTP_MOVED_PERM ); addResourceHeader( "Linker.html", "Location: " + getHostPath() + "/Target.html" ); _wc.getResponse( getHostPath() + "/Frames.html" ); assertMatchingSet( "Frames defined for the conversation", new String[] { "_top", "red", "blue" }, _wc.getFrameNames() ); assertTrue( "Did not redirect", _wc.getFrameContents( "red" ).getURL().toExternalForm().endsWith( "Target.html" ) ); } private void defineNestedFrames() throws Exception { defineResource( "Topmost.html", "<HTML><HEAD><TITLE>Topmost</TITLE></HEAD>" + "<FRAMESET cols=\"20%,80%\">" + " <FRAME src=\"Target.html\" name=\"red\">" + " <FRAME src=\"Inner.html\" name=\"blue\">" + "</FRAMESET></HTML>" ); defineResource( "Inner.html", "<HTML><HEAD><TITLE>Inner</TITLE></HEAD>" + "<FRAMESET rows=\"20%,80%\">" + " <FRAME src=\"Form.html\" name=\"green\">" + "</FRAMESET></HTML>" ); } public void testGetNestedFrameByName() throws Exception { defineNestedFrames(); _wc.getResponse( getHostPath() + "/Topmost.html" ); _wc.getFrameContents( "green" ); } public void testLinkWithAncestorTarget() throws Exception { defineNestedFrames(); _wc.getResponse( getHostPath() + "/Topmost.html" ); WebResponse innerResponse = _wc.getFrameContents( "blue" ).getSubframeContents( "green" ); innerResponse.getLinks()[0].click(); assertEquals( "Title of 'red' frame", "Linker", _wc.getFrameContents( "red" ).getTitle() ); } public void testIFrameDetection() throws Exception { defineWebPage( "Frame", "This is a trivial page with <a href='mailto:russgold@httpunit.org'>one link</a>" + "and <iframe name=center src='Contents.html'><form name=hidden></form></iframe>" ); defineWebPage( "Contents", "This is another page with <a href=Form.html>one link</a>" ); defineWebPage( "Form", "This is a page with a simple form: " + "<form action=submit><input name=name><input type=submit></form>"); WebResponse response = _wc.getResponse( getHostPath() + "/Frame.html" ); WebRequest[] requests = response.getFrameRequests(); assertEquals( "Number of links in main frame", 1, response.getLinks().length ); assertEquals( "Number of forms in main frame", 0, response.getForms().length ); assertEquals( "Number of frame requests", 1, requests.length ); assertEquals( "Target for iframe request", "center", requests[0].getTarget() ); WebResponse contents = getFrameWithURL( _wc, "Contents" ); assertNotNull( "Contents not found", contents ); assertEquals( "Number of links in iframe", 1, _wc.getFrameContents( "center" ).getLinks().length ); } public void testIFrameDisabled() throws Exception { defineWebPage( "Frame", "This is a trivial page with <a href='mailto:russgold@httpunit.org'>one link</a>" + "and <iframe name=center src='Contents.html'><form name=hidden></form></iframe>" ); defineWebPage( "Contents", "This is another page with <a href=Form.html>one link</a>" ); _wc.getClientProperties().setIframeSupported( false ); WebResponse response = _wc.getResponse( getHostPath() + "/Frame.html" ); WebRequest[] requests = response.getFrameRequests(); assertEquals( "Number of links in main frame", 1, response.getLinks().length ); assertEquals( "Number of forms in main frame", 1, response.getForms().length ); assertEquals( "Number of frame requests", 0, requests.length ); } /** * Verifies that an open call from a subframe can specify another frame name. */ public void testOpenIntoSubframe() throws Exception { defineResource( "Frames.html", "<html><head><frameset>" + " <frame name='banner'>" + " <frame src='main.html' name='main'>" + "</frameset></html>" ); defineResource( "target.txt", "You made it!" ); defineWebPage( "main", "<button id='button' onclick=\"window.open( 'target.txt', 'banner' )\">" ); _wc.getResponse( getHostPath() + "/Frames.html" ); ((Button) _wc.getFrameContents( "main" ).getElementWithID( "button" )).click(); assertEquals( "Num open windows", 1, _wc.getOpenWindows().length ); assertEquals( "New banner", "You made it!", _wc.getFrameContents( "banner" ).getText() ); assertNotNull( "Original button no longer there", _wc.getFrameContents( "main" ).getElementWithID( "button" ) ); } /** * Verifies that an open call from a subframe can specify another frame name. */ public void testSelfOpenFromSubframe() throws Exception { defineResource( "Frames.html", "<html><head><frameset>" + " <frame name='banner' src='banner.html'>" + " <frame name='main' src='main.html'>" + "</frameset></html>" ); defineResource( "target.txt", "You made it!" ); defineWebPage( "main", "<button id='button2' onclick=\"window.open( 'target.txt', 'banner' )\">" ); defineWebPage( "banner", "<button id='button1' onclick=\"window.open( 'target.txt', '_self' )\">" ); _wc.getResponse( getHostPath() + "/Frames.html" ); ((Button) _wc.getFrameContents( "banner" ).getElementWithID( "button1" )).click(); assertEquals( "Num open windows", 1, _wc.getOpenWindows().length ); assertEquals( "New banner", "You made it!", _wc.getFrameContents( "banner" ).getText() ); assertNotNull( "Second frame no longer there", _wc.getFrameContents( "main" ).getElementWithID( "button2" ) ); } /** * Verifies that an open call from a subframe can specify another frame name. */ public void testFrameWithHashSource() throws Exception { defineResource( "Frames.html", "<html><head><frameset>" + " <frame name='banner' src='#'>" + " <frame name='main' src='main.html'>" + "</frameset></html>" ); defineResource( "target.txt", "You made it!" ); defineWebPage( "main", "<a id='banner' href='target.txt'>banner</a>" ); _wc.getResponse( getHostPath() + "/Frames.html" ); WebLink link = (WebLink) _wc.getFrameContents( "main" ).getElementWithID( "banner" ); assertNotNull( "No link found", link ); } private WebConversation _wc;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -