| 1 | define([ |
| 2 | "require", |
| 3 | "doh/main", |
| 4 | "./aspect", |
| 5 | "./dom-construct", |
| 6 | "./dom-style", |
| 7 | "./_base/kernel", |
| 8 | "./_base/lang", |
| 9 | "./on", |
| 10 | "./robot", |
| 11 | "./sniff", |
| 12 | "./_base/window" |
| 13 | ], function(require, doh, aspect, construct, style, kernel, lang, on, robot, has, win){ |
| 14 | |
| 15 | kernel.experimental("dojo.robotx"); |
| 16 | |
| 17 | // module: |
| 18 | // dojo.robotx |
| 19 | // description: |
| 20 | // loads an external app into an iframe and points dojo.doc to the iframe document, allowing the robot to control it |
| 21 | // to use: set robotURL in djConfig to the URL you want to load |
| 22 | // dojo.require this file |
| 23 | |
| 24 | // The iframe containing the external app |
| 25 | var iframe = null; |
| 26 | |
| 27 | // urlLoaded is a Deferred that will be resolved whenever the iframe passed to initRobot() finishes loading, or reloads |
| 28 | var urlLoaded; |
| 29 | |
| 30 | function attachIframe(url){ |
| 31 | // summary: |
| 32 | // Create iframe to load external app at specified url. Iframe gets onload handler to call onIframeLoad() |
| 33 | // when specified URL finishes loading, and also if the iframe loads a different URL in the future. |
| 34 | // returns: |
| 35 | // A Deferred that fires when everything has finished initializing |
| 36 | |
| 37 | require(["./domReady!"], function(){ |
| 38 | var emptyStyle = { |
| 39 | overflow: "hidden", |
| 40 | margin: "0px", |
| 41 | borderWidth: "0px", |
| 42 | height: "100%", |
| 43 | width: "100%" |
| 44 | }; |
| 45 | style.set(document.documentElement, emptyStyle); |
| 46 | style.set(document.body, emptyStyle); |
| 47 | |
| 48 | // Create the iframe for the external document. |
| 49 | // console.log("creating iframe for external document"); |
| 50 | iframe = document.createElement("iframe"); |
| 51 | iframe.setAttribute("ALLOWTRANSPARENCY","true"); |
| 52 | iframe.scrolling = has("ie") ? "yes" : "auto"; |
| 53 | var scrollRoot = document.compatMode == "BackCompat" ? document.body : document.documentElement; |
| 54 | style.set(iframe, { |
| 55 | border: "0px none", |
| 56 | padding: "0px", |
| 57 | margin: "0px", |
| 58 | width: "100%", |
| 59 | height: "100%" |
| 60 | }); |
| 61 | iframe.src = url; |
| 62 | |
| 63 | // Code to handle load event on iframe. Seems like this should happen before setting iframe src on line above? |
| 64 | // Also, can't we use on() in all cases, even for old IE? |
| 65 | if(iframe.attachEvent !== undefined){ |
| 66 | iframe.attachEvent("onload", onIframeLoad); |
| 67 | }else{ |
| 68 | on(iframe, "load", onIframeLoad); |
| 69 | } |
| 70 | |
| 71 | construct.place(iframe, win.body(), "first"); |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | function onIframeLoad(){ |
| 76 | // summary: |
| 77 | // Load handler when iframe specified to initRobot() finishes loading, or when it reloads. |
| 78 | // It resolves the urlLoaded Deferred to make the rests of the tests runs. |
| 79 | |
| 80 | robot._updateDocument(); |
| 81 | |
| 82 | // If dojo is present in the test case, then at least make a best effort to wait for it to load. |
| 83 | // The test must handle other race conditions like initial data queries or asynchronous parses by itself. |
| 84 | if(iframe.contentWindow.require){ |
| 85 | iframe.contentWindow.require(["dojo/ready"], function(ready){ |
| 86 | ready(Infinity, function(){ |
| 87 | setTimeout(function(){ |
| 88 | urlLoaded.resolve(true); |
| 89 | }, 500); // 500ms fudge factor; otherwise focus doesn't work on IE8, see ValidationTextBox.js, TimeTextBox.js, etc. |
| 90 | }); |
| 91 | }); |
| 92 | }else{ |
| 93 | urlLoaded.resolve(true); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | lang.mixin(robot, { |
| 98 | _updateDocument: function(){ |
| 99 | // summary: |
| 100 | // Called every time a new page is loaded into the iframe, to setup variables |
| 101 | // Point dojo.global, dojo.publish, etc. to refer to iframe. |
| 102 | // Remove for 2.0? |
| 103 | |
| 104 | kernel.setContext(iframe.contentWindow, iframe.contentWindow.document); |
| 105 | |
| 106 | // Also set pointers inside robot, for easy access via AMD (where there is no dojo variable) |
| 107 | robot.window = iframe.contentWindow; |
| 108 | robot.doc = iframe.contentWindow.document; |
| 109 | |
| 110 | // TODO: shouldn't this wait until dojo has finished loading in the iframe? See require code in onIframeLoad(). |
| 111 | var win = kernel.global; |
| 112 | if(win.dojo){ |
| 113 | // allow the tests to subscribe to topics published by the iframe |
| 114 | kernel.publish = win.dojo.publish; |
| 115 | kernel.subscribe = win.dojo.subscribe; |
| 116 | kernel.connectPublisher = win.dojo.connectPublisher; |
| 117 | } |
| 118 | }, |
| 119 | |
| 120 | initRobot: function(/*String*/ url){ |
| 121 | // summary: |
| 122 | // Opens the application at the specified URL for testing, redirecting dojo to point to the application |
| 123 | // environment instead of the test environment. |
| 124 | // url: |
| 125 | // URL to open. Any of the test's dojo.doc calls (e.g. dojo.byId()), and any dijit.registry calls |
| 126 | // (e.g. dijit.byId()) will point to elements and widgets inside this application. |
| 127 | |
| 128 | doh.registerGroup("initialize robot", { |
| 129 | name: "load " + url, |
| 130 | timeout: 100000, // could take more than 10s so setting to 100s |
| 131 | runTest: function(){ |
| 132 | // Setup module level urlLoaded Deferred that will be resolved by onIframeLoad(), after the iframe |
| 133 | // has finished loading |
| 134 | urlLoaded = new doh.Deferred(); |
| 135 | attachIframe(url); |
| 136 | |
| 137 | return urlLoaded; |
| 138 | } |
| 139 | }); |
| 140 | }, |
| 141 | |
| 142 | waitForPageToLoad: function(/*Function*/ submitActions){ |
| 143 | // summary: |
| 144 | // Notifies DOH that the doh.robot is about to make a page change in the application it is driving, |
| 145 | // returning a doh.Deferred object the user should return in their runTest function as part of a DOH test. |
| 146 | // example: |
| 147 | // | runTest: function(){ |
| 148 | // | return waitForPageLoad(function(){ doh.robot.keyPress(keys.ENTER, 500); }); |
| 149 | // | } |
| 150 | // submitActions: |
| 151 | // The doh.robot will execute the actions the test passes into the submitActions argument (like clicking the submit button), |
| 152 | // expecting these actions to create a page change (like a form submit). |
| 153 | // After these actions execute and the resulting page loads, the next test will start. |
| 154 | |
| 155 | // Setup a new Deferred that onIframeLoad() will resolve when the iframe finishes loading |
| 156 | urlLoaded = new doh.Deferred(); |
| 157 | |
| 158 | submitActions(); |
| 159 | |
| 160 | return urlLoaded; |
| 161 | } |
| 162 | |
| 163 | }); |
| 164 | |
| 165 | return robot; |
| 166 | }); |