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  package net.sf.jameleon.plugin.watij;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import net.sf.jameleon.exception.JameleonScriptException;
25  import net.sf.jameleon.function.FunctionTag;
26  import net.sf.jameleon.util.Configurator;
27  import net.sf.jameleon.util.JameleonUtility;
28  
29  import watij.finders.Symbol;
30  import watij.finders.SymbolFactory;
31  import watij.runtime.ie.IE;
32  
33  public abstract class WatijFunctionTag extends FunctionTag{
34  
35      public static final String STORE_SOURCE_CONFIG_NAME = "watij.storeSourceOnStateChange";
36      protected boolean storeSourceOnStateChange;
37      protected WatijSessionTag sessionTag;
38      
39  
40      /***
41       * Gets a handle on the IE object from the Session tag.
42       * 
43       * @return IE
44       */
45      public IE ie(){
46          IE ie = null;
47          if (sessionTag != null) {
48              ie = sessionTag.getSession();
49          }
50          return ie;
51      }
52  
53      public IE getParentBrowser(){
54          IE ie = null;
55          if (sessionTag != null) {
56              ie = sessionTag.getParentSession();
57          }
58          return ie;
59      }
60  
61      /***
62       * Gets the references of the session and the test results from the parent TestCase
63       * This method gets called once all attributes are set from the macro language. Any required
64       * setup should go here.
65       */
66      public void setupEnvironment() {
67          super.setupEnvironment();
68          storeSourceOnStateChange = Boolean.valueOf(Configurator.getInstance().getValue(STORE_SOURCE_CONFIG_NAME,"true")).booleanValue();
69          sessionTag = (WatijSessionTag)findAncestorWithClass(WatijSessionTag.class);
70          if (sessionTag == null) {
71              throw new JameleonScriptException("a "+fp.getDefaultTagName() +" can only execute inside a watij-session tag", this);
72          }
73      }
74  
75  /*
76       private void tempMeth(){
77           WebResponse res = getCurrentWebResponse();
78           File stateFile = null;
79           if ( res != null ) {
80               String html = res.getContentAsString();
81               String charset = res.getContentCharSet();
82               String filename = fName+".html";
83               if ( html != null && html.length() > 0 ) {
84                   stateFile = new File(filename);
85                   if ( charset != null && charset.length() > 0 ) {
86                       JameleonUtility.recordResultsToFile(stateFile, html, charset);
87                   } else {
88                       JameleonUtility.recordResultsToFile(stateFile, html);
89                   }
90               }
91           }
92       }
93  */
94  
95      public void store(String fName, int event) throws IOException{
96          File newFile = new File(fName+".html");
97          if (!storeSourceOnStateChange) {
98              newFile = new File(fName +".png");
99          }
100         JameleonUtility.createDirStructure(newFile.getParentFile());
101         try{
102             if (storeSourceOnStateChange) {
103                 JameleonUtility.recordResultsToFile(newFile, ie().html());
104             }else{
105                 ie().windowCapture(newFile.getAbsolutePath());
106             }
107             getFunctionResults().setErrorFile(newFile);
108         }catch(Exception e){
109             e.printStackTrace();
110             traceMsg(JameleonUtility.getStack(e));
111         }
112     }
113 
114     ////////////////////////////////////////////////////////////////////////////////////////////////
115     // Helper Methods
116     ////////////////////////////////////////////////////////////////////////////////////////////////
117 
118     public void setCurrentIE(IE currentIE){
119         sessionTag.setCurrentIE(currentIE);
120     }
121 
122     /***
123      * Gets a Symbol from a String
124      * @return a Symbol representing the String
125      */
126     public Symbol getSymbolFromString(String symbol){
127         Symbol s = null;
128         if ("action".equals(symbol)) {
129             s = SymbolFactory.action;
130         }else if ("alt".equals(symbol)) {
131             s = SymbolFactory.alt;
132         }else if ("caption".equals(symbol)) {
133             s = SymbolFactory.caption;
134         }else if ("href".equals(symbol)) {
135             s = SymbolFactory.href;
136         }else if ("id".equals(symbol)) {
137             s = SymbolFactory.id;
138         }else if ("method".equals(symbol)) {
139             s = SymbolFactory.method;
140         }else if ("name".equals(symbol)) {
141             s = SymbolFactory.name;
142         }else if ("src".equals(symbol)) {
143             s = SymbolFactory.src;
144         }else if ("tag".equals(symbol)) {
145             s = SymbolFactory.tag;
146         }else if ("text".equals(symbol)) {
147             s = SymbolFactory.text;
148         }else if ("title".equals(symbol)) {
149             s = SymbolFactory.title;
150         }else if ("url".equals(symbol)) {
151             s = SymbolFactory.url;
152         }else if ("value".equals(symbol)) {
153             s = SymbolFactory.value;
154         }else if ("xpath".equals(symbol)) {
155             s = SymbolFactory.xpath;
156         }
157         if (s == null) {
158             throw new JameleonScriptException("symbol '"+symbol+"' is not a recognized symbol", this);
159         }
160         return s;
161     }
162 
163 }