1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
77
78
79
80
81
82
83
84
85
86
87
88
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
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 }