| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package com.netscape.javascript.qa.drivers; |
| 7 |
|
| 8 |
import java.io.*; |
| 9 |
|
| 10 |
import netscape.javascript.JSObject; |
| 11 |
import com.netscape.javascript.qa.drivers.*; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class NavEnv implements TestEnvironment { |
| 25 |
TestFile file; |
| 26 |
TestSuite suite; |
| 27 |
NavDrv driver; |
| 28 |
private JSObject result; |
| 29 |
JSObject opener; |
| 30 |
JSObject testcases; |
| 31 |
JSObject location; |
| 32 |
|
| 33 |
boolean evaluatedSuccessfully = false; |
| 34 |
JSObject window; |
| 35 |
|
| 36 |
private String WINDOW_NAME; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public NavEnv( TestFile f, TestSuite s, NavDrv d ) { |
| 42 |
this.file = f; |
| 43 |
this.suite = s; |
| 44 |
this.driver =d; |
| 45 |
|
| 46 |
this.opener = (JSObject) JSObject.getWindow( d ); |
| 47 |
this.window = null; |
| 48 |
this.WINDOW_NAME = "js" + getRandomWindowName(); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public synchronized void runTest() { |
| 54 |
int i = 0; |
| 55 |
System.out.println( file.name ); |
| 56 |
try { |
| 57 |
createContext(); |
| 58 |
file.startTime = driver.getCurrentTime(); |
| 59 |
System.out.println( i++ ); |
| 60 |
executeTestFile(); |
| 61 |
System.out.println( i++ ); |
| 62 |
|
| 63 |
if ( evaluatedSuccessfully ) { |
| 64 |
System.out.println( i++ ); |
| 65 |
|
| 66 |
parseResult(); |
| 67 |
System.out.println( i++ ); |
| 68 |
|
| 69 |
} |
| 70 |
file.endTime = driver.getCurrentTime(); |
| 71 |
|
| 72 |
} catch ( Exception e ) { |
| 73 |
suite.passed = false; |
| 74 |
file.passed = false; |
| 75 |
file.exception = "file failed with exception: " + e ; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public Object createContext() { |
| 84 |
System.out.println( "opening window" ); |
| 85 |
opener.eval( WINDOW_NAME +" = window.open( '', '" + WINDOW_NAME + "' )" ); |
| 86 |
window = (JSObject) opener.getMember( WINDOW_NAME ); |
| 87 |
return window; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public Object executeTestFile() { |
| 96 |
System.out.println( "executeTestFile()" ); |
| 97 |
try { |
| 98 |
location = (JSObject) window.getMember( "location" ); |
| 99 |
System.out.println( file.name ); |
| 100 |
String s = driver.HTTP_PATH + suite.name +"/" + file.name; |
| 101 |
location.setMember( "href", driver.HTTP_PATH + suite.name +"/" + |
| 102 |
file.name ); |
| 103 |
evaluatedSuccessfully = waitForCompletion(); |
| 104 |
|
| 105 |
} catch ( Exception e ) { |
| 106 |
System.err.println( file.name + " failed with exception: " + e ); |
| 107 |
file.exception = e.toString(); |
| 108 |
if ( file.name.endsWith( "-n.js" ) ) { |
| 109 |
this.file.passed = true; |
| 110 |
evaluatedSuccessfully = true; |
| 111 |
} else { |
| 112 |
this.file.passed = false; |
| 113 |
this.suite.passed = false; |
| 114 |
evaluatedSuccessfully = false; |
| 115 |
} |
| 116 |
} |
| 117 |
return null; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public boolean waitForCompletion() { |
| 132 |
int counter = 0; |
| 133 |
if ( ! window.getMember( "completed" ).toString().equals("true") ) { |
| 134 |
while (!window.getMember("completed").toString().equals("true")) { |
| 135 |
try { |
| 136 |
if ( counter > 20 ) { |
| 137 |
file.passed = false; |
| 138 |
file.exception += "test failed to complete"; |
| 139 |
System.out.println( "test failed to complete" ); |
| 140 |
return false; |
| 141 |
} |
| 142 |
System.out.println("."); |
| 143 |
driver.sleep( 1000 ); |
| 144 |
counter++; |
| 145 |
} catch ( Exception e ) { |
| 146 |
System.out.println( "sleep failed: " + e ); |
| 147 |
return false; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public synchronized boolean parseResult() { |
| 160 |
try { |
| 161 |
JSObject testcases = (JSObject) window.getMember("testcases"); |
| 162 |
file.totalCases = ((Number) ((JSObject) testcases).getMember("length")).intValue(); |
| 163 |
System.out.println( "testcases.length is " + file.totalCases ); |
| 164 |
for ( int i = 0; i < file.totalCases; i++ ) { |
| 165 |
JSObject tc = (JSObject) ((JSObject) testcases).getSlot(i); |
| 166 |
|
| 167 |
TestCase nc = new TestCase( |
| 168 |
tc.getMember("passed") == null ? "null" : tc.getMember("passed").toString(), |
| 169 |
tc.getMember("name") == null ? "null " : tc.getMember("name").toString(), |
| 170 |
tc.getMember("description") == null ? "null " : tc.getMember("description").toString(), |
| 171 |
tc.getMember("expect") == null ? "null " : tc.getMember("expect").toString(), |
| 172 |
tc.getMember("actual") == null ? "null " : tc.getMember("actual").toString(), |
| 173 |
tc.getMember("reason") == null ? "null " : tc.getMember("reason").toString() |
| 174 |
); |
| 175 |
|
| 176 |
file.caseVector.addElement( nc ); |
| 177 |
|
| 178 |
if ( nc.passed.equals("false") ) { |
| 179 |
if ( file.name.endsWith( "-n.js" ) ) { |
| 180 |
this.file.passed = true; |
| 181 |
} else { |
| 182 |
this.file.passed = false; |
| 183 |
this.suite.passed = false; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} catch ( Exception e ) { |
| 188 |
System.out.println( e ); |
| 189 |
file.exception = e.toString(); |
| 190 |
if ( file.name.endsWith( "-n.html" ) ) { |
| 191 |
file.passed = true; |
| 192 |
} else { |
| 193 |
file.passed = false; |
| 194 |
suite.passed = false; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
if ( this.file.passed == false ) { |
| 200 |
try { |
| 201 |
this.file.bugnumber = window.getMember("BUGNUMBER").toString(); |
| 202 |
} catch ( Exception e ) { |
| 203 |
|
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public void close() { |
| 214 |
opener.eval( WINDOW_NAME +".close()" ); |
| 215 |
opener.eval( "delete " + WINDOW_NAME ); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public String getRandomWindowName() { |
| 222 |
return (Integer.toString((new Double(Math.random()*100000)).intValue())); |
| 223 |
} |
| 224 |
} |