Source Code Cross Referenced for ClickLinkTest.java in  » Testing » webtest » com » canoo » webtest » steps » request » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » webtest » com.canoo.webtest.steps.request 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright © 2002-2005 Canoo Engineering AG, Switzerland.
002:        package com.canoo.webtest.steps.request;
003:
004:        import java.util.ArrayList;
005:        import java.util.Collections;
006:        import java.util.List;
007:
008:        import com.canoo.webtest.engine.StepExecutionException;
009:        import com.canoo.webtest.engine.StepFailedException;
010:        import com.canoo.webtest.self.TestBlock;
011:        import com.canoo.webtest.self.ThrowAssert;
012:        import com.canoo.webtest.steps.BaseStepTestCase;
013:        import com.canoo.webtest.steps.Step;
014:        import com.gargoylesoftware.htmlunit.AlertHandler;
015:        import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
016:        import com.gargoylesoftware.htmlunit.WebClient;
017:        import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
018:        import com.gargoylesoftware.htmlunit.html.HtmlPage;
019:
020:        /**
021:         * Test cases for {@link ClickLink}.
022:         *
023:         * @author unknown
024:         * @author Marc Guillemot
025:         * @author Paul King, ASERT
026:         * @author Denis N. Antonioli
027:         */
028:        public class ClickLinkTest extends BaseStepTestCase {
029:            private static final String NBSP = "\u00a0";
030:            private ClickLink fStep;
031:
032:            protected Step createStep() {
033:                return new ClickLink();
034:            }
035:
036:            protected void setUp() throws Exception {
037:                super .setUp();
038:                fStep = (ClickLink) getStep();
039:            }
040:
041:            public static void testNbsp() {
042:                ClickLink step = new ClickLink();
043:
044:                step.setLabel("a" + NBSP + "b");
045:                // TODO migration: why should this nbsp be transformed to a normal space?
046:                // assertEquals("a b", step.getLabel());
047:            }
048:
049:            // <clickLink htmlId="notExisting" />
050:            public void testClickNonExistingLink() throws Exception {
051:                final String htmlContent = wrapContent("No link");
052:                getContext().setDefaultResponse(htmlContent);
053:                fStep.setHtmlId("notExisting");
054:                final HtmlPage page = getDummyPage(htmlContent);
055:                assertNull(fStep.findClickableElement(page));
056:            }
057:
058:            // <clickLink htmlId="dummyLink" />
059:            public void testLocateWebLinkById() throws Exception {
060:                final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.html'>dummy</a>");
061:                fStep.setHtmlId("dummyLink");
062:                final HtmlPage page = getDummyPage(htmlContent);
063:                assertNotNull(fStep.findClickableElement(page));
064:            }
065:
066:            // <clickLink htmlId="idNotAnchor" />
067:            public void testLocateNonLinkFails() throws Exception {
068:                final String htmlContent = wrapContent("<span id='idNotAnchor'>dummy</span>");
069:                final HtmlPage page = getDummyPage(htmlContent);
070:                fStep.setHtmlId("idNotAnchor");
071:                String msg = ThrowAssert.assertThrows(
072:                        StepFailedException.class, new TestBlock() {
073:                            public void call() throws Exception {
074:                                fStep.findClickableElement(page);
075:                            }
076:                        });
077:                assertTrue(msg.indexOf("not a link") != -1);
078:            }
079:
080:            // <clickLink label="dummy" />
081:            public void testLocateWebLinkByLabel() throws Exception {
082:                final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.html'>dummy</a>");
083:
084:                final HtmlPage page = getDummyPage(htmlContent);
085:                fStep.setLabel("dummy");
086:                assertNotNull(fStep.findClickableElement(page));
087:            }
088:
089:            // <clickLink label="dummy" href="foo"/>
090:            public void testLocateWebLinkByLabelAndHref() throws Exception {
091:                final String htmlContent = wrapContent("<a href='foo.html'>dummy</a>, <a href='foo2.html'>foo</a>");
092:                final HtmlPage page = getDummyPage(htmlContent);
093:
094:                final ClickLink step = (ClickLink) getStep();
095:
096:                step.setLabel("dummy");
097:                step.setHref("foo");
098:                assertEquals("foo.html", ((HtmlAnchor) step
099:                        .findClickableElement(page)).getHrefAttribute());
100:                step.setLabel("foo");
101:                step.setHref("foo");
102:                assertEquals("foo2.html", ((HtmlAnchor) step
103:                        .findClickableElement(page)).getHrefAttribute());
104:            }
105:
106:            // <clickLink label="dummyalt" />
107:            public void testLocateWebLinkByMatchingImageAlt() throws Exception {
108:                final String htmlContent = wrapContent("<img src='dummy.gif' alt='dummyalt'/>"
109:                        + "<table><tr><td><img src='dummy.gif' alt='dummyalt'/></td></tr></table>"
110:                        + "<a href='dummy.html'><img src='dummy.gif' alt='dummyalt'/></a>");
111:                final HtmlPage page = getDummyPage(htmlContent);
112:                fStep.setLabel("dummyalt");
113:                assertNotNull(fStep.findClickableElement(page));
114:            }
115:
116:            // <clickLink href="dummy.html" />
117:            public void testLocateWebLinkByHref() throws Exception {
118:                final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.html'>dummy</a>");
119:                final HtmlPage page = getDummyPage(htmlContent);
120:                fStep.setHref("dummy.html");
121:                assertNotNull(fStep.findClickableElement(page));
122:            }
123:
124:            // <clickLink href="param=value" />
125:            public void testLocateWebLinkByHrefParams() throws Exception {
126:                final String htmlContent = wrapContent("<a id='dummyLink' href='dummy.jsp?param=value&this=that'>dummy</a>");
127:                fStep.setHref("param=value");
128:                final HtmlPage page = getDummyPage(htmlContent);
129:                assertNotNull(fStep.findClickableElement(page));
130:            }
131:
132:            // <clickLink xpath="//a[@class = 'foo']" />
133:            public void testLocateWebLinkByXPath() throws Exception {
134:                final String htmlContent = wrapContent("<a href='dummy.jsp'>dummy1</a> <a href='dummy.jsp' class='foo'>dummy2</a>");
135:                fStep.setXpath("//a[@class = 'foo']");
136:                final HtmlPage page = getDummyPage(htmlContent);
137:                assertEquals("dummy2", fStep.findClickableElement(page)
138:                        .asText());
139:            }
140:
141:            // <clickLink xpath="//a[@class = 'foo']" />
142:            public void testExecuteXPath() throws Exception {
143:                final String htmlContent = wrapContent("<a href='dummy.jsp'>dummy1</a> <a href='dummy.jsp' class='foo' onclick='alert(\"foo\")'>dummy2</a>");
144:                fStep.setXpath("//a[@class = 'foo']");
145:                final HtmlPage page = getDummyPage(htmlContent);
146:                final WebClient client = page.getWebClient();
147:                final List collectedAlerts = new ArrayList();
148:                final AlertHandler alertHandler = new CollectingAlertHandler(
149:                        collectedAlerts);
150:                client.setAlertHandler(alertHandler);
151:
152:                fStep.execute();
153:
154:                final List expectedAlerts = Collections.singletonList("foo");
155:                assertEquals(expectedAlerts, collectedAlerts);
156:
157:            }
158:
159:            // <clickLink />
160:            public void testNoAttributes() throws Exception {
161:                String msg = ThrowAssert.assertThrows(
162:                        StepExecutionException.class, new TestBlock() {
163:                            public void call() throws Exception {
164:                                fStep.verifyParameters();
165:                            }
166:                        });
167:
168:                assertEquals(
169:                        "\"htmlId\" or \"xpath\" or \"label\" or \"href\" must be set!",
170:                        msg);
171:            }
172:
173:            /**
174:             * Tests that an exception is thrown when label and/or href is used combined with htmlid
175:             */
176:            public void testVerifyParameterHtmlIdNotCombined() {
177:                fStep.setHtmlId("idNotAnchor");
178:                fStep.setLabel("foo");
179:
180:                TestBlock block = new TestBlock() {
181:                    public void call() throws Exception {
182:                        fStep.verifyParameters();
183:                    }
184:                };
185:
186:                // with htmlid and label
187:                ThrowAssert.assertThrows(StepExecutionException.class, block);
188:
189:                // with htmlid, label and href
190:                fStep.setHref("foo");
191:                ThrowAssert.assertThrows(StepExecutionException.class, block);
192:
193:                // with htmlid and href
194:                fStep.setLabel(null);
195:                ThrowAssert.assertThrows(StepExecutionException.class, block);
196:
197:                // with htmlid and xpath
198:                fStep.setHref(null);
199:                fStep.setXpath("//a");
200:                ThrowAssert.assertThrows(StepExecutionException.class, block);
201:            }
202:
203:            /**
204:             * Tests that an exception is thrown when label and/or href is used combined with xpath
205:             */
206:            public void testVerifyParametersXPath() {
207:                fStep.setXpath("//a");
208:
209:                TestBlock block = new TestBlock() {
210:                    public void call() throws Exception {
211:                        fStep.verifyParameters();
212:                    }
213:                };
214:
215:                // with xpath and label
216:                fStep.setLabel("foo");
217:                ThrowAssert.assertThrows(StepExecutionException.class, block);
218:
219:                // with xpath, label and href
220:                fStep.setHref("foo");
221:                ThrowAssert.assertThrows(StepExecutionException.class, block);
222:
223:                // with xpath and href
224:                fStep.setLabel(null);
225:                ThrowAssert.assertThrows(StepExecutionException.class, block);
226:            }
227:
228:            public void testErrorWithXmlPageUnknownLabel() throws Exception {
229:                fStep.setLabel("foo");
230:                assertErrorOnExecuteIfCurrentPageIsXml(fStep);
231:            }
232:
233:            public void testErrorWithXmlPageInvalidXPath() throws Exception {
234:                final String htmlContent = wrapContent("<span>dummy</span>");
235:                final HtmlPage page = getDummyPage(htmlContent);
236:                fStep.setXpath("//span");
237:                String msg = ThrowAssert.assertThrows(
238:                        StepFailedException.class, new TestBlock() {
239:                            public void call() throws Exception {
240:                                fStep.findClickableElement(page);
241:                            }
242:                        });
243:                assertTrue(msg.indexOf("not a link") != -1);
244:                assertTrue(msg.indexOf("span tag") != -1);
245:            }
246:
247:            private static String wrapContent(String content) {
248:                return "<html><head><title>foo</title></head><body>" + content
249:                        + "</body></html>";
250:            }
251:
252:            public void testNestedText() throws Exception {
253:                testNestedTextEquivalent(getStep(), "label");
254:            }
255:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.