📄 weblinktest.java
字号:
package com.meterware.httpunit;/********************************************************************************************************************* $Id: WebLinkTest.java,v 1.30 2004/06/28 23:18:17 russgold Exp $** Copyright (c) 2000-2004, Russell Gold** Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated* documentation files (the "Software"), to deal in the Software without restriction, including without limitation* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and* to permit persons to whom the Software is furnished to do so, subject to the following conditions:** The above copyright notice and this permission notice shall be included in all copies or substantial portions* of the Software.** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER* DEALINGS IN THE SOFTWARE.********************************************************************************************************************/import junit.framework.TestSuite;/** * Tests for the WebLink class. * * @author <a href="mailto:russgold@httpunit.org>Russell Gold</a> * @author <a href="mailto:bx@bigfoot.com>Benoit Xhenseval</a> **/public class WebLinkTest extends HttpUnitTest { public static void main(String args[]) { junit.textui.TestRunner.run( suite() ); } public static TestSuite suite() { return new TestSuite( WebLinkTest.class ); } public WebLinkTest( String name ) { super( name ); } public void setUp() throws Exception { super.setUp(); defineResource( "SimplePage.html", "<html><head><title>A Sample Page</title></head>\n" + "<body>This has no forms but it does\n" + "have <a href='/other.html#middle' id='activeID'>an <b>active</b> link</A>\n" + " and <a name=here>an anchor</a>\n" + "<a href='basic.html' name=\"nextLink\"><IMG SRC=\"/images/arrow.gif\" ALT=\"Next -->\" WIDTH=1 HEIGHT=4></a>\n" + "<a href='another.html' name='myLink'>some text</a>\n" + "</body></html>\n" ); WebConversation wc = new WebConversation(); _simplePage = wc.getResponse( getHostPath() + "/SimplePage.html" ); } public void testFindNoLinks() throws Exception { defineResource( "NoLinks.html", "<html><head><title>NoLinks</title></head><body>No links at all</body></html>" ); WebConversation wc = new WebConversation(); WebLink[] links = wc.getResponse( getHostPath() + "/NoLinks.html" ).getLinks(); assertNotNull( links ); assertEquals( 0, links.length ); } public void testLinks() throws Exception { WebLink[] links = _simplePage.getLinks(); assertNotNull( "Found no links", links ); assertEquals( "number of links in page", 3, links.length ); } public void testEmbeddedFontTags() throws Exception { defineResource( "FontPage.html", "<html><head><title>A Sample Page</title></head>\n" + "<table><tr><td><a href='/other.html' id='activeID'><font face='Arial'>an <b>active</b> link</font></A></td>\n" + "<td><a href='basic.html' name=\"nextLink\"><IMG SRC=\"/images/arrow.gif\" ALT=\"Next -->\" WIDTH=1 HEIGHT=4></a></td>\n" + "<td><a href='another.html' name='myLink'>some text</a></td>\n" + "</tr></table></body></html>\n" ); WebConversation wc = new WebConversation(); WebResponse wr = wc.getResponse( getHostPath() + "/FontPage.html" ); assertEquals( "Number of links found", 3, wr.getLinks().length ); } public void testLinkRequest() throws Exception { WebLink link = _simplePage.getLinks()[0]; WebRequest request = link.getRequest(); assertTrue( "Should be a get request", request instanceof GetMethodWebRequest ); assertEquals( getHostPath() + "/other.html", request.getURL().toExternalForm() ); } public void testLinkUrlAcrossLineBreaks() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "Initial", "<a id='midbreak' href='http://loc\nalhost/somewhere'</a>" + "<a id='endbreak' href='http://localhost/somewhere\n'</a>" ); WebResponse response = wc.getResponse( getHostPath() + "/Initial.html" ); assertEquals( "URL with break at end", "http://localhost/somewhere", response.getLinkWithID( "endbreak" ).getRequest().getURL().toExternalForm() ); assertEquals( "URL across linebreak", "http://localhost/somewhere", response.getLinkWithID( "midbreak" ).getRequest().getURL().toExternalForm() ); } public void testLinkReference() throws Exception { WebLink link = _simplePage.getLinks()[0]; assertEquals( "URLString", "/other.html", link.getURLString() ); } public void testGetLinkByText() throws Exception { WebLink link = _simplePage.getLinkWith( "no link" ); assertNull( "Non-existent link should not have been found", link ); link = _simplePage.getLinkWith( "an active link" ); assertNotNull( "an active link was not found", link ); assertEquals( "active link URL", getHostPath() + "/other.html", link.getRequest().getURL().toExternalForm() ); link = _simplePage.getLinkWithImageText( "Next -->" ); assertNotNull( "the image link was not found", link ); assertEquals( "image link URL", getHostPath() + "/basic.html", link.getRequest().getURL().toExternalForm() ); HttpUnitOptions.setImagesTreatedAsAltText( true ); link = _simplePage.getLinkWith( "Next -->" ); assertNotNull( "the image link was not found", link ); assertEquals( "image link URL", getHostPath() + "/basic.html", link.getRequest().getURL().toExternalForm() ); HttpUnitOptions.setImagesTreatedAsAltText( false ); link = _simplePage.getLinkWith( "Next -->" ); assertNull( "the image link was found based on its hidden alt attribute", link ); } public void testCustomMatching() throws Exception { WebLink link = _simplePage.getFirstMatchingLink( WebLink.MATCH_URL_STRING, "nothing" ); assertNull( "Non-existent link should not have been found", link ); link = _simplePage.getFirstMatchingLink( WebLink.MATCH_URL_STRING, "/other.html" ); assertNotNull( "an active link was not found", link ); assertEquals( "active link text", "an active link", link.getText() ); link = _simplePage.getFirstMatchingLink( WebLink.MATCH_URL_STRING, "basic" ); assertNotNull( "the image link was not found", link ); assertEquals( "image link URL", getHostPath() + "/basic.html", link.getRequest().getURL().toExternalForm() ); WebLink[] links = _simplePage.getMatchingLinks( WebLink.MATCH_URL_STRING, "other.ht" ); assertNotNull( "No link array returned", links ); assertEquals( "Number of links with URL containing 'other.ht'", 2, links.length ); } public void testGetLinkByIDAndName() throws Exception { WebLink link = _simplePage.getLinkWithID( "noSuchID" ); assertNull( "Non-existent link should not have been found", link ); link = _simplePage.getLinkWithID( "activeID" ); assertNotNull( "an active link was not found", link ); assertEquals( "active link URL", getHostPath() + "/other.html", link.getRequest().getURL().toExternalForm() ); link = _simplePage.getLinkWithName( "nextLink" ); assertNotNull( "the image link was not found", link ); assertEquals( "image link URL", getHostPath() + "/basic.html", link.getRequest().getURL().toExternalForm() ); } public void testFragmentIdentifier() throws Exception { WebLink link = (WebLink) _simplePage.getElementWithID( "activeID" ); assertNotNull( "the active link was not found", link ); assertEquals( "fragment identifier #1", "middle", link.getFragmentIdentifier() ); assertEquals( "fragment identifier #2", "", _simplePage.getLinks()[1].getFragmentIdentifier() ); } public void testLinkText() throws Exception { WebLink link = _simplePage.getLinks()[0]; assertEquals( "Link text", "an active link", link.getText() ); } public void testLinkImageAsText() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "HasImage", "<a href='somwhere.html' >\r\n<img src='blah.gif' alt='Blah Blah' >\r\n</a>" ); WebResponse initialPage = wc.getResponse( getHostPath() + "/HasImage.html" ); WebLink link = initialPage.getLinks()[0]; assertEquals( "Link text", "", link.getText().trim() ); initialPage.getLinkWithImageText("Blah Blah"); } public void testLinkFollowing() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "Initial", "Go to <a href=\"Next.html\">the next page.</a> <a name=\"bottom\">Bottom</a>" ); defineWebPage( "Next", "And go back to <a href=\"Initial.html#Bottom\">the first page.</a>" ); WebResponse initialPage = wc.getResponse( getHostPath() + "/Initial.html" ); assertEquals( "Num links in initial page", 1, initialPage.getLinks().length ); WebLink link = initialPage.getLinks()[0]; WebResponse nextPage = wc.getResponse( link.getRequest() ); assertEquals( "Title of next page", "Next", nextPage.getTitle() ); assertEquals( "Num links in next page", 1, nextPage.getLinks().length ); link = nextPage.getLinks()[0];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -