]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
a089699c AD |
8 | if(!dojo._hasResource["dojo.robotx"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dojo.robotx"] = true; | |
2f01fe57 AD |
10 | dojo.provide("dojo.robotx"); |
11 | dojo.require("dojo.robot"); | |
81bea17a AD |
12 | |
13 | ||
2f01fe57 | 14 | dojo.experimental("dojo.robotx"); |
a089699c AD |
15 | |
16 | // loads an external app into an iframe and points dojo.doc to the iframe document, allowing the robot to control it | |
17 | // to use: set robotURL in djConfig to the URL you want to load | |
18 | // dojo.require this file | |
19 | ||
2f01fe57 | 20 | (function(){ |
a089699c | 21 | |
81bea17a | 22 | var iframe = null; |
a089699c AD |
23 | |
24 | var groupStarted=dojo.connect(doh, '_groupStarted', function(){ | |
25 | dojo.disconnect(groupStarted); | |
26 | iframe.style.visibility="visible"; | |
2f01fe57 | 27 | }); |
a089699c | 28 | |
81bea17a AD |
29 | var attachIframe = function(){ |
30 | dojo.addOnLoad(function(){ | |
31 | var emptyStyle = { | |
32 | overflow: dojo.isWebKit? 'hidden' : 'visible', | |
33 | margin: '0px', | |
34 | borderWidth: '0px', | |
35 | height: '100%', | |
36 | width: '100%' | |
37 | }; | |
38 | dojo.style(document.documentElement, emptyStyle); | |
39 | dojo.style(document.body, emptyStyle); | |
40 | document.body.appendChild(iframe); | |
41 | var base=document.createElement('base'); | |
42 | base.href=iframe.src; | |
43 | document.getElementsByTagName("head")[0].appendChild(base); | |
44 | }); | |
45 | }; | |
46 | ||
47 | // Prevent race conditions between iframe loading and robot init. | |
48 | // If iframe is allowed to load while the robot is typing, sync XHRs can prevent the robot from completing its initialization. | |
49 | var robotReady=false; | |
50 | var robotFrame=null; | |
51 | var _run=doh.robot._run; | |
52 | doh.robot._run=function(frame){ | |
53 | // Called from robot when the robot completed its initialization. | |
54 | robotReady = true; | |
55 | robotFrame = frame; | |
56 | doh.robot._run=_run; | |
57 | // If initRobot was already called, then attach the iframe. | |
58 | if(iframe.src){ attachIframe(); } | |
59 | } | |
60 | ||
a089699c | 61 | var onIframeLoad=function(){ |
81bea17a | 62 | // initial load handler: update the document and start the tests |
a089699c AD |
63 | doh.robot._updateDocument(); |
64 | onIframeLoad = null; | |
65 | var scrollRoot = (document.compatMode == 'BackCompat')? document.body : document.documentElement; | |
66 | var consoleHeight = document.getElementById('robotconsole').offsetHeight; | |
67 | if(consoleHeight){ | |
68 | iframe.style.height = (scrollRoot.clientHeight - consoleHeight)+"px"; | |
69 | } | |
81bea17a AD |
70 | // If dojo is present in the test case, then at least make a best effort to wait for it to load. |
71 | // The test must handle other race conditions like initial data queries by itself. | |
72 | if(iframe.contentWindow.dojo){ | |
73 | iframe.contentWindow.dojo.addOnLoad(function(){ | |
74 | doh.robot._run(robotFrame); | |
75 | }); | |
76 | }else{ | |
77 | doh.robot._run(robotFrame); | |
78 | } | |
2f01fe57 | 79 | }; |
a089699c AD |
80 | |
81 | var iframeLoad=function(){ | |
82 | if(onIframeLoad){ | |
83 | onIframeLoad(); | |
84 | } | |
85 | var unloadConnect = dojo.connect(dojo.body(), 'onunload', function(){ | |
86 | dojo.global = window; | |
87 | dojo.doc = document; | |
88 | dojo.disconnect(unloadConnect); | |
89 | }); | |
2f01fe57 | 90 | }; |
a089699c AD |
91 | |
92 | // write the firebug console to a place it will fit | |
93 | dojo.config.debugContainerId = "robotconsole"; | |
94 | dojo.config.debugHeight = dojo.config.debugHeight || 200; | |
95 | document.write('<div id="robotconsole" style="position:absolute;left:0px;bottom:0px;width:100%;"></div>'); | |
96 | ||
97 | // write the iframe | |
98 | //document.writeln('<iframe id="robotapplication" style="visibility:hidden; border:0px none; padding:0px; margin:0px; position:absolute; left:0px; top:0px; width:100%; height:100%; z-index: 1;" src="'+dojo.config.robotURL+'" onload="iframeLoad();" ></iframe>'); | |
99 | iframe = document.createElement('iframe'); | |
100 | iframe.setAttribute("ALLOWTRANSPARENCY","true"); | |
101 | iframe.scrolling = dojo.isIE? "yes" : "auto"; | |
102 | dojo.style(iframe,{visibility:'hidden', border:'0px none', padding:'0px', margin:'0px', position:'absolute', left:'0px', top:'0px', width:'100%', height:'100%'}); | |
103 | if(iframe['attachEvent'] !== undefined){ | |
104 | iframe.attachEvent('onload', iframeLoad); | |
2f01fe57 | 105 | }else{ |
a089699c | 106 | dojo.connect(iframe, 'onload', iframeLoad); |
2f01fe57 | 107 | } |
a089699c | 108 | |
81bea17a AD |
109 | |
110 | ||
111 | ||
a089699c AD |
112 | dojo.mixin(doh.robot,{ |
113 | _updateDocument: function(){ | |
114 | dojo.setContext(iframe.contentWindow, iframe.contentWindow.document); | |
115 | var win = dojo.global; | |
116 | if(win["dojo"]){ | |
117 | // allow the tests to subscribe to topics published by the iframe | |
118 | dojo._topics = win.dojo._topics; | |
119 | } | |
120 | ||
121 | }, | |
122 | ||
123 | initRobot: function(/*String*/ url){ | |
124 | // summary: | |
125 | // Opens the application at the specified URL for testing, redirecting dojo to point to the application environment instead of the test environment. | |
126 | // | |
127 | // url: | |
128 | // URL to open. Any of the test's dojo.doc calls (e.g. dojo.byId()), and any dijit.registry calls (e.g. dijit.byId()) will point to elements and widgets inside this application. | |
129 | // | |
81bea17a | 130 | |
a089699c | 131 | iframe.src=url; |
81bea17a AD |
132 | // see above note about race conditions |
133 | if(robotReady){ | |
134 | attachIframe(); | |
135 | ||
136 | } | |
a089699c AD |
137 | }, |
138 | ||
139 | waitForPageToLoad: function(/*Function*/ submitActions){ | |
140 | // summary: | |
141 | // Notifies DOH that the doh.robot is about to make a page change in the application it is driving, | |
142 | // returning a doh.Deferred object the user should return in their runTest function as part of a DOH test. | |
143 | // | |
144 | // description: | |
145 | // Notifies DOH that the doh.robot is about to make a page change in the application it is driving, | |
146 | // returning a doh.Deferred object the user should return in their runTest function as part of a DOH test. | |
147 | // Example: | |
148 | // runTest:function(){ | |
149 | // return waitForPageLoad(function(){ doh.robot.keyPress(dojo.keys.ENTER, 500); }); | |
150 | // } | |
151 | // | |
152 | // submitActions: | |
153 | // The doh.robot will execute the actions the test passes into the submitActions argument (like clicking the submit button), | |
154 | // expecting these actions to create a page change (like a form submit). | |
155 | // After these actions execute and the resulting page loads, the next test will start. | |
156 | // | |
157 | ||
158 | var d = new doh.Deferred(); | |
159 | // create iframe event handler to track submit progress | |
160 | onIframeLoad = function(){ | |
161 | onIframeLoad = null; | |
162 | // set dojo.doc on every page change to point to the iframe doc so the robot works | |
163 | doh.robot._updateDocument(); | |
164 | d.callback(true); | |
165 | }; | |
166 | submitActions(); | |
167 | return d; | |
168 | } | |
169 | ||
2f01fe57 | 170 | }); |
2f01fe57 | 171 | })(); |
a089699c | 172 | |
2f01fe57 | 173 | } |