1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.jameleon.plugin.watij;
21
22 import net.sf.jameleon.SessionTag;
23 import net.sf.jameleon.exception.JameleonScriptException;
24 import net.sf.jameleon.util.JameleonUtility;
25 import watij.runtime.ie.IE;
26
27 /***
28 * A Session tag for the Watij plug-in.
29 *
30 * An example of its use might:
31 *
32 * <pre><source>
33 * <testcase xmlns="jelly:jameleon">
34 * <watij-session baseUrl="http://www.google.com" beginSession="true">
35 * <watij-assert-title
36 * functionId="Check that title is Google."
37 * title="Google"/>
38 * </watij-session>
39 * </testcase>
40 * </source></pre>
41 *
42 * @jameleon.function name="watij-session"
43 */
44 public class WatijSessionTag extends SessionTag {
45
46 /***
47 * @jameleon.attribute contextName="baseUrl"
48 */
49 protected String baseUrl;
50 /***
51 * @jameleon.attribute contextName="beginAt"
52 */
53 protected String beginAt;
54 /***
55 * A handle on the Selenium server session
56 */
57 protected IE session;
58 /***
59 * A handle on the currently active IE
60 */
61 protected IE currentIE;
62
63 /***
64 * Gets the base URL, including the protocol (http://), the domain name and any path informtion.
65 *
66 * @return the base URL.
67 */
68 public String getBaseUrl(){
69 return baseUrl;
70 }
71
72 /***
73 * Gets the trailing path of the URL to begin the browser at if set.
74 *
75 * @return the trailing path of the URL to begin the browser at if set.
76 */
77 public String getBeginAt(){
78 return beginAt;
79 }
80
81 /***
82 * Gets the url to request
83 *
84 * @return The URL to use in startApplication.
85 */
86 protected String getRequestUrl(){
87 String url = getBaseUrl();
88 if (getBeginAt() != null) {
89 url += getBeginAt();
90 }
91 return url;
92 }
93
94 /***
95 * Gets the Selenium session used to communicate with the web application
96 *
97 * @return Selenium
98 */
99 public IE getSession(){
100 if (currentIE == null) {
101 currentIE = session;
102 }
103 return currentIE;
104 }
105
106 /***
107 * Gets the Selenium session used to communicate with the web application
108 *
109 * @return Selenium
110 */
111 public IE getParentSession(){
112 return session;
113 }
114
115 protected void createSession(){
116 session = new IE();
117 currentIE = session;
118 try{
119 session.start();
120 }catch(Exception e){
121 throw new JameleonScriptException("problem starting browser", e, this);
122 }
123 }
124
125 /***
126 * Sets the current instance of IE to work with.
127 */
128 public void setCurrentIE(IE currentIE){
129 this.currentIE = currentIE;
130 }
131
132 public void setUpSession(){
133 createSession();
134 }
135
136 public void tearDownSession(){
137 if (session != null) {
138 try{
139 for (int i = 0; i < session.childBrowserCount(); i++) {
140 session.childBrowser(i).close();
141 }
142 session.close();
143 }catch(Exception e){
144 traceMsg("Problem closing watij-session:");
145 traceMsg(JameleonUtility.getStack(e));
146 }
147 session = null;
148 currentIE = null;
149 }
150 }
151
152 /***
153 * Loads the URL given by <b>beginAt</b> and <b>baseUrl</b>.
154 */
155 public void startApplication(){
156 if (getBaseUrl() != null) {
157 try{
158 session.goTo(getRequestUrl());
159 }catch(Exception e){
160 throw new JameleonScriptException("Could not navigate to: "+getRequestUrl(), e, this);
161 }
162 } else {
163 throw new JameleonScriptException("'baseUrl' is a required field if beginSession is set to 'true'!", this);
164 }
165 }
166
167 }