1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import java.net.URL; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.List; 44 45 import com.gargoylesoftware.htmlunit.CollectingAlertHandler; 46 import com.gargoylesoftware.htmlunit.MockWebConnection; 47 import com.gargoylesoftware.htmlunit.WebClient; 48 import com.gargoylesoftware.htmlunit.WebTestCase; 49 50 import com.gargoylesoftware.htmlunit.html.HtmlAnchor; 51 import com.gargoylesoftware.htmlunit.html.HtmlPage; 52 53 59 public class AnchorTest extends WebTestCase { 60 61 65 public AnchorTest( final String name ) { 66 super(name); 67 } 68 69 70 73 public void testAnchor_getAttribute_and_href() throws Exception { 74 final WebClient client = new WebClient(); 75 final MockWebConnection webConnection = new MockWebConnection( client ); 76 77 final String content 78 = "<html><head><title>AnchorTest</title><script>\n" 79 + "function doTest(anchorElement) {\n" 80 + " alert(anchorElement.href);\n" 81 + " alert(anchorElement.getAttribute('href'));\n" 82 + " anchorElement.href='testsite2.html';\n" 83 + " alert(anchorElement.href);\n" 84 + " alert(anchorElement.getAttribute('href'));\n" 85 + " alert(anchorElement.getAttribute('id'));\n" 86 + " alert(anchorElement.getAttribute('name'));\n" 87 + "}\n</script></head>" 88 + "<body>" 89 + "<a HREF='testsite1.html' id='13' name='testanchor' onClick='doTest(this);return false'>" 90 + "</body></html>"; 91 92 webConnection.setDefaultResponse( content ); 93 client.setWebConnection( webConnection ); 94 95 final List collectedAlerts = new ArrayList(); 96 client.setAlertHandler( new CollectingAlertHandler(collectedAlerts) ); 97 98 final HtmlPage page = (HtmlPage)(client.getPage( new URL("http://x"))); 99 100 final HtmlAnchor anchor = page.getAnchorByName("testanchor"); 101 102 anchor.click(); 103 104 final List expectedAlerts = Arrays.asList( new String[]{ 105 "testsite1.html", "testsite1.html", "testsite2.html", "testsite2.html", "13", "testanchor" 106 } ); 107 assertEquals( expectedAlerts, collectedAlerts ); 108 } 109 110 113 public void testOnclickToString() throws Exception { 114 final String content 115 = "<html><head><title>AnchorTest</title><script>\n" 116 + "function test() {\n" 117 + " for (var i=0; i<document.links.length; ++i)" 118 + " {\n" 119 + " var onclick = document.links[i].onclick;\n" 120 + " alert(onclick ? (onclick.toString().indexOf('alert(') != -1) : 'not defined');\n" 121 + " }\n" 122 + "}\n</script></head>" 123 + "<body onload='test()'>" 124 + "<a HREF='foo.html' onClick='alert(\"on click\")'>" 125 + "<a HREF='foo2.html'>" 126 + "</body></html>"; 127 128 129 final List collectedAlerts = new ArrayList(); 130 final List expectedAlerts = Arrays.asList( new String[]{ "true", "not defined" } ); 131 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 132 loadPage(content, collectedAlerts); 133 134 135 assertEquals( expectedAlerts, collectedAlerts ); 136 } 137 138 141 public void testDefaultConversionToString() throws Exception { 142 final String content 143 = "<html><head><title>AnchorTest</title><script>\n" 144 + "function test() {\n" 145 + " alert(document.getElementById('myAnchor'));\n" 146 + " for (var i=0; i<document.links.length; ++i)" 147 + " {\n" 148 + " alert(document.links[i]);\n" 149 + " }\n" 150 + "}</script></head>" 151 + "<body onload='test()'>" 152 + "<a name='start' id='myAnchor'/>" 153 + "<a HREF='foo.html'>foo</a>" 154 + "<a HREF='javascript:void(0)'>void</a>" 155 + "<a HREF='#'>#</a>" 156 + "</body></html>"; 157 158 159 final List collectedAlerts = new ArrayList(); 160 final List expectedAlerts = Arrays.asList( new String[]{ "", 161 "http://www.gargoylesoftware.com/foo.html", 162 "javascript:void(0)", 163 "http://www.gargoylesoftware.com/#"} ); 164 createTestPageForRealBrowserIfNeeded(content, expectedAlerts); 165 loadPage(content, collectedAlerts); 166 167 assertEquals( expectedAlerts, collectedAlerts ); 168 } 169 } 170 | Popular Tags |