📄 weblinktest.java
字号:
link.click(); assertEquals( "Title of next page", "Initial", wc.getFrameContents( link.getTarget() ).getTitle() ); } public void testLinksWithFragmentsAndParameters() throws Exception { WebConversation wc = new WebConversation(); defineResource( "Initial.html?age=3", "<html><head><title>Initial</title></head><body>" + "Go to <a href=\"Next.html\">the next page.</a> <a name=\"bottom\">Bottom</a>" + "</body></html>" ); defineWebPage( "Next", "And go back to <a href=\"Initial.html?age=3#Bottom\">the first page.</a>" ); WebResponse initialPage = wc.getResponse( getHostPath() + "/Initial.html?age=3" ); 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]; WebResponse thirdPage = wc.getResponse( link.getRequest() ); assertEquals( "Title of next page", "Initial", thirdPage.getTitle() ); } public void testLinksWithSlashesInQuery() throws Exception { WebConversation wc = new WebConversation(); defineResource( "sample/Initial.html?age=3/5", "<html><head><title>Initial</title></head><body>" + "Go to <a href=\"Next.html\">the next page.</a>" + "</body></html>" ); defineWebPage( "sample/Next", "And go back to <a href=\"Initial.html?age=3/5\">the first page.</a>" ); WebResponse initialPage = wc.getResponse( getHostPath() + "/sample/Initial.html?age=3/5" ); 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", "sample/Next", nextPage.getTitle() ); assertEquals( "Num links in next page", 1, nextPage.getLinks().length ); } public void testDocumentBase() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "alternate/Target", "Found me!" ); defineResource( "Initial.html", "<html><head><title>Test for Base</title>" + " <base href='/alternate/'></head>" + " <body><a href=\"Target.html\">Go</a></body></html>" ); WebResponse initialPage = wc.getResponse( getHostPath() + "/Initial.html" ); assertEquals( "Num links in initial page", 1, initialPage.getLinks().length ); WebLink link = initialPage.getLinks()[0]; WebRequest request = link.getRequest(); assertEquals( "Destination for link", getHostPath() + "/alternate/Target.html", request.getURL().toExternalForm() ); WebResponse nextPage = wc.getResponse( request ); assertTrue( "Did not find the target", nextPage.getText().indexOf( "Found" ) >= 0 ); } public void testTargetBase() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "alternate/Target", "Found me!" ); defineResource( "Initial.html", "<html><head><title>Test for Base</title>" + " <base target=blue></head>" + " <body><a href=\"Target.html\">Go</a></body></html>" ); WebResponse initialPage = wc.getResponse( getHostPath() + "/Initial.html" ); assertEquals( "Num links in initial page", 1, initialPage.getLinks().length ); WebLink link = initialPage.getLinks()[0]; assertEquals( "Target for link", "blue", link.getTarget() ); } public void testParametersOnLinks() throws Exception { defineResource( "ParameterLinks.html", "<html><head><title>Param on Link Page</title></head>\n" + "<body>" + "<a href=\"/other.html\">no parameter link</A>\n" + "<a href=\"/other.html?param1=value1\">one parameter link</A>\n" + "<a href=\"/other.html?param1=value1¶m2=value2\">two parameters link</A>\n" + "<a href=\"/other.html?param1=value1¶m1=value3\">two values link</A>\n" + "<a href=\"/other.html?param1=value1¶m2=value2¶m1=value3\">two values link</A>\n" + "</body></html>\n" ); WebConversation wc = new WebConversation(); WebLink[] links = wc.getResponse( getHostPath() + "/ParameterLinks.html" ).getLinks(); assertNotNull( links ); assertEquals( "number of links", 5, links.length ); WebRequest request; // first link should not have any param request = links[0].getRequest(); assertNotNull( request); String[] names = request.getRequestParameterNames(); assertNotNull( names ); assertEquals( "Num parameters found", 0, names.length ); assertEquals("Non Existent parameter should be empty","",request.getParameter("nonexistent")); // second link should have one parameter checkLinkParameters( links[1], new String[] { "param1" }, new String[][] { { "value1" } }); // third link should have 2 parameters. !! Order of parameters cannot be guaranted. checkLinkParameters( links[2], new String[] { "param1", "param2" }, new String[][] { { "value1" }, { "value2" } }); // fourth link should have 1 parameter with 2 values. checkLinkParameters( links[3], new String[] { "param1" }, new String[][] { { "value1", "value3" } }); // fifth link should have 2 parameters with one with 2 values. checkLinkParameters( links[4], new String[] { "param1", "param2" }, new String[][] { { "value1", "value3" }, { "value2" } }); } private void checkLinkParameters( WebLink link, String[] expectedNames, String[][] expectedValues ) { WebRequest request = link.getRequest(); assertNotNull( request ); assertMatchingSet( "Parameter names", expectedNames, request.getRequestParameterNames() ); for (int i = 0; i < expectedValues.length; i++) { assertMatchingSet( expectedNames[i] + " values", expectedValues[i], request.getParameterValues( expectedNames[i] )); } } public void testEncodedLinkParameters() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "encodedLinks", "<html><head><title>Encode Test</title></head>" + "<body>" + "<a href=\"/request?%24dollar=%25percent&%23hash=%26ampersand\">request</a>" + "</body></html>" ); WebResponse mapPage = wc.getResponse( getHostPath() + "/encodedLinks.html" ); WebLink link = mapPage.getLinks()[0]; WebRequest wr = link.getRequest(); assertMatchingSet( "Request parameter names", new String[] { "$dollar", "#hash" }, wr.getRequestParameterNames() ); assertEquals( "Value of $dollar", "%percent", wr.getParameter( "$dollar" ) ); assertEquals( "Value of #hash", "&ersand", wr.getParameter( "#hash" ) ); } public void testValuelessLinkParameters() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "encodedLinks", "<html><head><title>Encode Test</title></head>" + "<body>" + "<a href=\"/request?arg1&valueless=\">request</a>" + "</body></html>" ); WebResponse mapPage = wc.getResponse( getHostPath() + "/encodedLinks.html" ); WebLink link = mapPage.getLinks()[0]; WebRequest wr = link.getRequest(); assertMatchingSet( "Request parameter names", new String[] { "arg1", "valueless" }, wr.getRequestParameterNames() ); assertEquals( "Value of arg1", null, wr.getParameter( "arg1" ) ); } public void testLinkParameterOrder() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "encodedLinks", "<html><head><title>Encode Test</title></head>" + "<body>" + "<a href='/request?arg0=0\n&arg1&arg0=2&valueless='>request</a>" + "</body></html>" ); WebResponse mapPage = wc.getResponse( getHostPath() + "/encodedLinks.html" ); WebLink link = mapPage.getLinks()[0]; WebRequest wr = link.getRequest(); assertMatchingSet( "Request parameter names", new String[] { "arg0", "arg1", "valueless" }, wr.getRequestParameterNames() ); assertMatchingSet( "Value of arg0", new String[] { "0", "2" }, wr.getParameterValues( "arg0" ) ); assertEquals( "Actual query", "arg0=0&arg1&arg0=2&valueless=", wr.getQueryString() ); } public void testLinkParameterValidation() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "encodedLinks", "<html><head><title>Encode Test</title></head>" + "<body>" + "<a href='/request?arg0=0&arg1&arg0=2&valueless='>request</a>" + "</body></html>" ); WebResponse mapPage = wc.getResponse( getHostPath() + "/encodedLinks.html" ); WebLink link = mapPage.getLinks()[0]; WebRequest wr = link.getRequest(); wr.setParameter( "arg0", new String[] { "0", "2" } ); try { wr.setParameter( "arg0", "3" ); fail( "Did not prevent change to link parameters" ); } catch (IllegalRequestParameterException e) {} } public void testImageMapLinks() throws Exception { WebConversation wc = new WebConversation(); defineWebPage( "pageWithMap", "Here is a page with <a href=\"somewhere\">a link</a>" + " and a map: <IMG src=\"navbar1.gif\" usemap=\"#map1\" alt=\"navigation bar\">" + "<map name=\"map1\">" + " <area href=\"guide.html\" alt=\"Guide\" shape=\"rect\" coords=\"0,0,118,28\">" + " <area href=\"search.html\" alt=\"Search\" shape=\"circle\" coords=\"184,200,60\">" + "</map>" ); WebResponse mapPage = wc.getResponse( getHostPath() + "/pageWithMap.html" ); WebLink[] links = mapPage.getLinks(); assertEquals( "number of links found", 3, links.length ); WebLink guide = mapPage.getLinkWith( "Guide" ); assertNotNull( "Did not find the guide area", guide ); assertEquals( "Relative URL", "guide.html", guide.getURLString() ); } private WebResponse _simplePage;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -