import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flexunit.framework.Assert;
import mx.utils.StringUtil;
import org.flexunit.async.Async;
public class WebTest {
private static const URL:String = "http://www.example.com/index.html";
[BeforeClass(async,timeout="3000")]
public static function preTestingConfirmServerReady():void {
const urlRequest:URLRequest = new URLRequest(URL);
const urlLoader:URLLoader = new URLLoader();
try {
urlLoader.load(urlRequest);
} catch (error:Error) {
Assert.fail("urlLoader threw: "+ error);
}
urlLoader.addEventListener(Event.COMPLETE,
Async.asyncHandler(WebTest, // N.b. ClassName in-place of "this"
function(event:Event, passThrough:*):void{
trace("urlLoader-COMPLETE: "+ event +" = "+ urlLoader.data);
Assert.assertNotNull(urlLoader.data);
Assert.assertTrue(0 !== String(urlLoader.data).length);
},
2000, null,
function(event:Event):void{
const timeoutMessage:String = StringUtil.substitute("TestCase-FATAL: preTestingConfirmServerReady failed to contact test URL \"{0}\" most tests will fail", URL);
trace(timeoutMessage);
Assert.fail(timeoutMessage);
}
), false, 0, true
);
Async.failOnEvent(WebTest, urlLoader, IOErrorEvent.IO_ERROR, 2000);
Async.failOnEvent(WebTest, urlLoader, SecurityErrorEvent.SECURITY_ERROR, 2000);
}
[Test]
public function testSomething():void {
Assert.assertTrue("your test here, this just forces this BeforeClass example to run standalone", true);
}
}
|