View Javadoc

1   /*
2       Jameleon Watij plug-in - A plug-in that uses Watij (http://www.watij.com/) to drive web sites
3       Copyright (C) 2008 Christian W. Hargraves (engrean@hotmail.com)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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   * &lt;testcase xmlns="jelly:jameleon"&gt;
34   *     &lt;watij-session baseUrl="http://www.google.com" beginSession="true"&gt;
35   *       &lt;watij-assert-title
36   *           functionId="Check that title is Google."
37   *           title="Google"/&gt;
38   *     &lt;/watij-session&gt;
39   * &lt;/testcase&gt;
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 }