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