Created
July 22, 2011 03:20
-
-
Save jpravetz/1098821 to your computer and use it in GitHub Desktop.
Example that show how automated clicking of a link in an HTML control does not work after running a timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" | |
xmlns:s="library://ns.adobe.com/flex/spark" | |
xmlns:mx="library://ns.adobe.com/flex/mx" | |
creationComplete="load()" | |
> | |
<s:VGroup width="100%" height="100%"> | |
<s:HGroup width="100%" verticalAlign="middle"> | |
<s:Label text="URL:" /> | |
<s:TextInput id="textUrl" width="100%" text="{htmlControl.location}" editable="false" /> | |
</s:HGroup> | |
<s:HGroup width="100%" verticalAlign="middle"> | |
<s:Button id="loadButton" label="Load" click="load()" /> | |
<s:Button id="test1Button" label="Wait then Click" click="waitThenClick()" /> | |
<s:Button id="test2Button" label="Loop then Click" click="loopThenClick()" /> | |
<s:Label id="statusLabel" /> | |
</s:HGroup> | |
<mx:HTML id="htmlControl" width="100%" height="100%" /> | |
</s:VGroup> | |
<fx:Script> | |
<![CDATA[ | |
private var _timer:Timer; | |
private function load():void | |
{ | |
htmlControl.location = "http://www.w3.org/MarkUp/"; | |
} | |
private function waitThenClick() : void | |
{ | |
statusLabel.text = "Run timer for 3 seconds then click"; | |
_timer = new Timer( 1000, 3 ); | |
_timer.addEventListener( TimerEvent.TIMER_COMPLETE, onTimerComplete ); | |
_timer.start(); | |
} | |
private function loopThenClick() : void | |
{ | |
wait( 3000 ); | |
click(); | |
} | |
private function onTimerComplete(event:Event) : void | |
{ | |
click(); | |
} | |
private function wait( ms:int ) : void | |
{ | |
var tend:int = getTimer() + ms; | |
while( getTimer() < tend ) | |
{ | |
var x:String = "This is a string " + Number("3.222").toString() + " and it is long"; | |
} | |
} | |
private function click() : void | |
{ | |
statusLabel.text = "Clicking now"; | |
var linkObject:Object = getFirstElementByAInnerHTML( 'HTML Working Group' ); | |
if( linkObject ) | |
{ | |
var event:Object = htmlControl.htmlLoader.window.document.createEvent('MouseEvent'); | |
event.initMouseEvent("click",true,true,htmlControl.htmlLoader.window.document.defaultView,0,0,0,0,0,false,false,false,false,0 ,null); | |
linkObject.dispatchEvent(event); | |
} | |
} | |
public function getFirstElementByAInnerHTML( text:String ):Object | |
{ | |
var allHTMLTags:Object = htmlControl.htmlLoader.window.document.getElementsByTagName('a'); | |
for( var idx:int=0; idx<allHTMLTags.length; ++idx ) | |
{ | |
if( allHTMLTags[idx].innerHTML.match( text ) ) | |
return allHTMLTags[idx]; | |
} | |
return null; | |
} | |
]]> | |
</fx:Script> | |
</s:WindowedApplication> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment