📄 webpagetest.java
字号:
String refreshURL = getHostPath() + "/SimplePage.html"; String page = "<html><head><title>Sample</title>" + "<meta Http-equiv=refresh content='5'></head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Refresh URL", refreshURL, simplePage.getRefreshRequest().getURL().toExternalForm() ); assertEquals( "Refresh delay", 5, simplePage.getRefreshDelay() ); } public void testAutoRefresh() throws Exception { String refreshURL = getHostPath() + "/NextPage.html"; String page = "<html><head><title>Sample</title>" + "<meta Http_equiv=refresh content='2;" + refreshURL + "'></head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); defineWebPage( "NextPage", "Not much here" ); WebConversation wc = new WebConversation(); wc.getClientProperties().setAutoRefresh( true ); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertNull( "No refresh request should have been found", simplePage.getRefreshRequest() ); } /** * Test the meta tag content retrieval **/ public void testMetaTag() throws Exception { String page = "<html><head><title>Sample</title>" + "<meta Http-equiv=\"Expires\" content=\"now\"/>\n" + "<meta name=\"robots\" content=\"index,follow\"/>" + "<meta name=\"keywords\" content=\"test\"/>" + "<meta name=\"keywords\" content=\"demo\"/>" + "</head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertMatchingSet( "robots meta tag", new String[] {"index,follow"}, simplePage.getMetaTagContent("name","robots")); assertMatchingSet( "keywords meta tag",new String[] {"test","demo"},simplePage.getMetaTagContent("name","keywords")); assertMatchingSet( "Expires meta tag",new String[] {"now"},simplePage.getMetaTagContent("http-equiv","Expires")); } /** * test the stylesheet retrieval **/ public void testGetExternalStylesheet() throws Exception { String page = "<html><head><title>Sample</title>" + "<link rev=\"made\" href=\"/Me@mycompany.com\"/>" + "<link type=\"text/css\" rel=\"stylesheet\" href=\"/style.css\"/>" + "</head>\n" + "<body>This has no data\n" + "</body></html>\n"; defineResource( "SimplePage.html", page ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertEquals( "Stylesheet","/style.css",simplePage.getExternalStyleSheet()); } /** * This test verifies that an IO exception is thrown when only a partial response is received. */ public void testTruncatedPage() throws Exception { HttpUnitOptions.setCheckContentLength( true ); String page = "abcdefghijklmnop"; defineResource( "alphabet.html", page, "text/plain" ); addResourceHeader( "alphabet.html", "Connection: close" ); addResourceHeader( "alphabet.html", "Content-length: 26" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/alphabet.html" ); try { WebResponse simplePage = wc.getResponse( request ); String alphabet = simplePage.getText(); assertEquals( "Full string", "abcdefghijklmnopqrstuvwxyz", alphabet ); } catch (IOException e) { } } public void testGetElementByID() throws Exception { defineResource( "SimplePage.html", "<html><head><title>A Sample Page</title></head>\n" + "<body><form id='aForm'><input name=color></form>" + "have <a id='link1' href='/other.html'>an <b>active</b> link</A>\n" + "<img id='23' src='/images/arrow.gif' ALT='Next -->' WIDTH=1 HEIGHT=4>\n" + "</body></html>\n" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertImplements( "element with id 'aForm'", simplePage.getElementWithID( "aForm" ), WebForm.class ); assertImplements( "element with id 'link1'", simplePage.getElementWithID( "link1" ), WebLink.class ); assertImplements( "element with id '23'", simplePage.getElementWithID( "23" ), WebImage.class ); } public void testGetElementsByName() throws Exception { defineResource( "SimplePage.html", "<html><head><title>A Sample Page</title></head>\n" + "<body><form name='aForm'><input name=color></form>" + "have <a id='link1' href='/other.html'>an <b>active</b> link</A>\n" + "<img id='23' src='/images/arrow.gif' ALT='Next -->' WIDTH=1 HEIGHT=4>\n" + "</body></html>\n" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertImplement( "element with name 'aForm'", simplePage.getElementsWithName( "aForm" ), WebForm.class ); assertImplement( "element with name 'color'", simplePage.getElementsWithName( "color" ), FormControl.class ); } public void testGetElementsByAttribute() throws Exception { defineResource( "SimplePage.html", "<html><head><title>A Sample Page</title></head>\n" + "<body><form class='first' name='aForm'><input name=color></form>" + "have <a id='link1' href='/other.html'>an <b>active</b> link</A>\n" + "<img id='23' src='/images/arrow.gif' ALT='Next -->' WIDTH=1 HEIGHT=4>\n" + "</body></html>\n" ); WebConversation wc = new WebConversation(); WebRequest request = new GetMethodWebRequest( getHostPath() + "/SimplePage.html" ); WebResponse simplePage = wc.getResponse( request ); assertImplement( "elements with class 'first'", simplePage.getElementsWithAttribute( "class", "first" ), WebForm.class ); assertImplement( "elements with name 'color'", simplePage.getElementsWithAttribute( "name", "color" ), FormControl.class ); assertImplement( "elements with id 'link1'", simplePage.getElementsWithAttribute( "id", "link1" ), WebLink.class ); assertImplement( "elements with src '/images/arrow.gif'", simplePage.getElementsWithAttribute( "src", "/images/arrow.gif" ), WebImage.class ); } /** * Test the {@link WebResponse.ByteTagParser} to ensure that embedded JavaScript is skipped. */ public void testByteTagParser() throws Exception { final URL mainBaseURL = new URL(getHostPath() + "/Main/Base"); final URL targetBaseURL = new URL(getHostPath() + "/Target/Base"); final String targetWindow = "target"; final String document = "<html><head><title>main</title>\n" + scriptToWriteAnotherDocument(simpleDocument(targetBaseURL), targetWindow) + "<base href=\"" + mainBaseURL.toExternalForm() + "\">\n" + "</head>\n<body>\nThis is a <a href=\"Link\">relative link</a>.\n" + "</body>\n</html>\n"; WebResponse.ByteTagParser parser = new WebResponse.ByteTagParser(document.getBytes()); String[] expectedTags = {"html", "head", "title", "/title", "script", "/script", "base", "/head", "body", "a", "/a", "/body", "/html"}; for (int i = 0; i < expectedTags.length; i++) { final String tagName = parser.getNextTag().getName(); final String expectedTag = expectedTags[i]; assertEquals("Tag number "+i, expectedTag, tagName); } final WebResponse.ByteTag nextTag = parser.getNextTag(); assertNull("More tags than expected: " + nextTag + "...?", nextTag); } /** * Test whether a base tag embedded within JavaScript in the header of a page confuses the parser. */ public void testBaseTagWithinJavaScriptInHeader() throws Exception { final URL mainBaseURL = new URL(getHostPath() + "/Main/Base"); final URL targetBaseURL = new URL(getHostPath() + "/Target/Base"); final String targetWindow = "target"; defineResource("main.html", "<html><head><title>main</title>\n" + scriptToWriteAnotherDocument(simpleDocument(targetBaseURL), targetWindow) + "<base href=\"" + mainBaseURL.toExternalForm() + "\">\n" + "</head>\n<body>\nThis is a <a href=\"Link\">relative link</a>.\n" + "</body>\n</html>\n"); WebConversation wc = new WebConversation(); final WebResponse response = wc.getResponse(getHostPath() + "/main.html"); assertEquals("Base URL of link in main document", mainBaseURL, response.getLinkWith("relative link").getBaseURL()); final WebResponse targetResponse = wc.getOpenWindow(targetWindow).getCurrentPage(); assertEquals("Base URL of link in target document", targetBaseURL, targetResponse.getLinkWith("relative link").getBaseURL()); } /** * Test whether a base tag embedded within JavaScript in the body of a page confuses the parser. */ public void testBaseTagWithinJavaScriptInBody() throws Exception { final URL mainBaseURL = new URL(getHostPath() + "/Main/Base"); final URL targetBaseURL = new URL(getHostPath() + "/Target/Base"); final String targetWindow = "target"; defineResource("main.html", "<html><head><title>main</title>\n" + "<base href=\"" + mainBaseURL.toExternalForm() + "\">\n" + "</head>\n<body>\nThis is a <a href=\"Link\">relative link</a>.\n" + scriptToWriteAnotherDocument(simpleDocument(targetBaseURL), targetWindow) + "</body>\n</html>\n"); WebConversation wc = new WebConversation(); final WebResponse response = wc.getResponse(getHostPath() + "/main.html"); assertEquals("Base URL of link in main document", mainBaseURL, response.getLinkWith("relative link").getBaseURL()); final WebResponse targetResponse = wc.getOpenWindow(targetWindow).getCurrentPage(); assertEquals("Base URL of link in target document", targetBaseURL, targetResponse.getLinkWith("relative link").getBaseURL()); } /** * Create a fragment of HTML defining JavaScript that writes a document into a different window. * @param document the document to be written * @param targetWindow the name of the target window to open * @return a fragment of HTML text */ private String scriptToWriteAnotherDocument(String document, String targetWindow) { StringBuffer buff = new StringBuffer(); buff.append("<script language=\"JavaScript\">\n"); buff.append("target = window.open('', '" + targetWindow + "');\n"); buff.append("target.document.write('" + document + "');\n"); buff.append("target.document.close();\n"); buff.append("</script>\n"); return buff.toString(); } /** * Create a simple document with or without a 'base' tag and containing a relative link. * @param baseUrl the base URL to insert, or null for no base tag * @return the text of a very simple document */ private String simpleDocument(final URL baseUrl) { return "<html><head><title>Simple Page</title>" + (baseUrl == null ? "" : "<base href=\"" + baseUrl.toExternalForm() + "\"></base>") + "</head><body>This is a simple page with a <a href=\"Link\">relative link</a>.</body></html>"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -