Source Code Cross Referenced for SetSelectFieldTest.java in  » Testing » webtest » com » canoo » webtest » steps » form » 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.form 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // Copyright © 2002-2007 Canoo Engineering AG, Switzerland.
002:        package com.canoo.webtest.steps.form;
003:
004:        import java.util.ArrayList;
005:        import java.util.Collections;
006:        import java.util.List;
007:
008:        import com.canoo.webtest.engine.StepFailedException;
009:        import com.canoo.webtest.self.TestBlock;
010:        import com.canoo.webtest.self.ThrowAssert;
011:        import com.canoo.webtest.steps.BaseStepTestCase;
012:        import com.canoo.webtest.steps.Step;
013:        import com.gargoylesoftware.htmlunit.AlertHandler;
014:        import com.gargoylesoftware.htmlunit.CollectingAlertHandler;
015:        import com.gargoylesoftware.htmlunit.WebClient;
016:        import com.gargoylesoftware.htmlunit.html.HtmlForm;
017:        import com.gargoylesoftware.htmlunit.html.HtmlOption;
018:        import com.gargoylesoftware.htmlunit.html.HtmlPage;
019:        import com.gargoylesoftware.htmlunit.html.HtmlSelect;
020:
021:        /**
022:         * Test cases for {@link com.canoo.webtest.steps.form.SetSelectField}.
023:         * @author Marc Guillemot
024:         */
025:        public class SetSelectFieldTest extends BaseStepTestCase {
026:            private static final String HTML_CONTENT = "<html><head><title>foo</title></head>"
027:                    + "<body>"
028:                    + "<form name='testForm'>"
029:                    + "<select name='mySelect' id='mySelectId'>"
030:                    + "<option value='0'>0</option>"
031:                    + "<option value='1'>first</option>"
032:                    + "<option value='2' id='opt2'>second</option>"
033:                    + "<option value='3'>third</option>"
034:                    + "<option>4</option>"
035:                    + "<option selected>5</option>"
036:                    + "</select>"
037:                    + "</form>"
038:                    + "No access</body></html>";
039:            private SetSelectField fStep;
040:
041:            protected Step createStep() {
042:                return new SetSelectField();
043:            }
044:
045:            protected void setUp() throws Exception {
046:                super .setUp();
047:                fStep = (SetSelectField) getStep();
048:            }
049:
050:            public void testSelectOption() {
051:                final HtmlPage page = getDummyPage(HTML_CONTENT);
052:                final HtmlForm form = page.getFormByName("testForm");
053:                final HtmlSelect select = form.getSelectByName("mySelect");
054:
055:                // test with value
056:                fStep.setName("mySelect");
057:                fStep.setValue("2");
058:                HtmlOption option = fStep.findMatchingOption(select);
059:                fStep.updateOption(select, option);
060:
061:                assertEquals("second", option.asText());
062:                assertTrue(option.isSelected());
063:
064:                // test with value... for an option that has no value
065:                fStep.setValue("4");
066:                option = fStep.findMatchingOption(select);
067:                assertEquals("4", option.asText());
068:
069:                // test with index
070:                fStep.setValue(null);
071:                fStep.setOptionIndex("1");
072:                option = fStep.findMatchingOption(select);
073:                fStep.updateOption(select, option);
074:                assertEquals("first", option.asText());
075:                assertTrue(option.isSelected());
076:
077:                // test with text
078:                fStep.setOptionIndex(null);
079:                fStep.setText("third");
080:                option = fStep.findMatchingOption(select);
081:                fStep.updateOption(select, option);
082:                assertEquals("3", option.getValueAttribute());
083:                assertTrue(option.isSelected());
084:
085:                // check not finding option fails step
086:                ThrowAssert.assertThrows(StepFailedException.class,
087:                        new TestBlock() {
088:                            public void call() throws Throwable {
089:                                fStep.updateOption(select, null);
090:                            }
091:                        });
092:            }
093:
094:            public void testFailureConditions() throws Exception {
095:                fStep.setName("mySelect");
096:
097:                fStep.setOptionIndex("1");
098:                fStep.setValue("anything");
099:                fStep.setText("some text");
100:                assertErrorOnExecute(fStep, "multiple args not allowed",
101:                        SetSelectField.AT_MOST_ONE_VALUE_TEXT_OPTIONINDEX);
102:
103:                fStep.setValue(null);
104:                assertErrorOnExecute(fStep, "multiple args not allowed",
105:                        SetSelectField.AT_MOST_ONE_VALUE_TEXT_OPTIONINDEX);
106:
107:                fStep.setOptionIndex(null);
108:                fStep.verifyParameters();
109:                assertFalse(fStep.isRegex());
110:                assertFalse(fStep.isMultiSelect());
111:            }
112:
113:            /**
114:             * Test identification of select and option with xpath 
115:             */
116:            public void testXPath() {
117:                getContext().setDefaultResponse(HTML_CONTENT);
118:
119:                final HtmlPage page = getContext()
120:                        .getCurrentHtmlResponse(fStep);
121:                final HtmlSelect select = page.getFormByName("testForm")
122:                        .getSelectByName("mySelect");
123:                final HtmlOption option0 = select.getOption(0);
124:                final HtmlOption option1 = select.getOption(1);
125:                final HtmlOption option2 = select.getOption(2);
126:                final HtmlOption option3 = select.getOption(3);
127:
128:                // test with xpath for the option
129:                fStep.setXpath("//option[@value='3']");
130:                assertFalse(option3.isSelected());
131:                fStep.execute();
132:                assertTrue(option3.isSelected());
133:
134:                // test with xpath for the select and text for the option
135:                fStep.setXpath("//select");
136:                fStep.setText("first");
137:                fStep.execute();
138:                assertTrue(option1.isSelected());
139:
140:                // test with xpath for the select and text for the option
141:                fStep.setXpath("//select");
142:                fStep.setText(null);
143:                fStep.setValue("0");
144:                fStep.execute();
145:                assertTrue(option0.isSelected());
146:
147:                // test with xpath for the select and optionIndex for the option
148:                fStep.setXpath("//select");
149:                fStep.setText(null);
150:                fStep.setValue(null);
151:                fStep.setOptionIndex("2");
152:                fStep.execute();
153:                assertTrue(option2.isSelected());
154:            }
155:
156:            /**
157:             * Test identification of select and option with id 
158:             */
159:            public void testHtmlId() {
160:                getContext().setDefaultResponse(HTML_CONTENT);
161:
162:                final HtmlPage page = getContext()
163:                        .getCurrentHtmlResponse(fStep);
164:                final HtmlSelect select = page.getFormByName("testForm")
165:                        .getSelectByName("mySelect");
166:                final HtmlOption option1 = select.getOption(1);
167:                final HtmlOption option2 = select.getOption(2);
168:
169:                // test with id for the option
170:                fStep.setHtmlId("opt2");
171:                assertFalse(option2.isSelected());
172:                fStep.execute();
173:                assertTrue(option2.isSelected());
174:
175:                // test with id for the select and text for the option
176:                fStep.setHtmlId("mySelectId");
177:                fStep.setText("first");
178:                fStep.execute();
179:                assertTrue(option1.isSelected());
180:
181:                // test with id for the select and nothing for the option
182:                fStep.setHtmlId("mySelectId");
183:                fStep.setText(null);
184:                assertErrorOnExecute(fStep, "",
185:                        SetSelectField.MESSAGE_MISSING_OPTION_IDENTIFIER);
186:            }
187:
188:            /**
189:             * Regression test for bug WT-143: onchange was wrongly triggered twice
190:             */
191:            public void testOnchangeTriggeredTwice() {
192:                // add onchange handler to standard code
193:                final String html = HTML_CONTENT
194:                        .replaceFirst("id='mySelectId'",
195:                                "id='mySelectId' onchange='alert(this.selectedIndex)')");
196:                getContext().setDefaultResponse(html);
197:
198:                final HtmlPage page = getContext()
199:                        .getCurrentHtmlResponse(fStep);
200:                final WebClient client = page.getWebClient();
201:                final List collectedAlerts = new ArrayList();
202:                final AlertHandler alertHandler = new CollectingAlertHandler(
203:                        collectedAlerts);
204:                client.setAlertHandler(alertHandler);
205:
206:                fStep.setName("mySelect");
207:                fStep.setValue("3");
208:                fStep.execute();
209:
210:                final List expectedAlerts = Collections.singletonList("3");
211:                assertEquals(expectedAlerts, collectedAlerts);
212:            }
213:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.