UNPKG

siesta-lite

Version:

Stress-free JavaScript unit testing and functional testing tool, works in NodeJS and browsers

378 lines (272 loc) 11.9 kB
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>The source code</title> <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="../resources/prettify/prettify.js"></script> <style type="text/css"> .highlight { display: block; background-color: #ddd; } </style> <script type="text/javascript"> function highlight() { document.getElementById(location.hash.replace(/#/, "")).className = "highlight"; } </script> </head> <body onload="prettyPrint(); highlight();"> <pre class="prettyprint lang-js">/* Siesta 5.6.1 Copyright(c) 2009-2022 Bryntum AB https://bryntum.com/contact https://bryntum.com/products/siesta/license */ !function () { var ID = 1 Class(&#39;Siesta.Recorder.Action&#39;, { has : { id : function () { return ID++ }, action : null, value : null, /* Possible type of targets: - &#39;xy&#39; XY coordinates - &#39;css&#39; css selector - &#39;cq&#39; component query - &#39;csq&#39; composite query - &#39;user&#39; user-provided text */ target : null, // used by &quot;dragTo&quot; toTarget : null, by : null, waitForPageLoad : false, options : null, // Should be a Siesta.Recorder.Event sourceEvent : null, sourceEventTargetReachableAtCenter : false, timestamp : function () { return new Date().getTime() } }, methods : { initialize : function () { var target = this.target if (target &amp;&amp; !(target instanceof Siesta.Recorder.Target)) { this.target = new Siesta.Recorder.Target({ targets : target }) } var toTarget = this.toTarget if (toTarget &amp;&amp; !(toTarget instanceof Siesta.Recorder.Target)) this.toTarget = new Siesta.Recorder.Target({ targets : toTarget }) }, setAction : function (newAction) { this.action = newAction if (!this.hasTarget()) { this.target &amp;&amp; this.target.clear() this.toTarget &amp;&amp; this.toTarget.clear() } }, hasTarget : function () { var action = this.action || &#39;&#39;; return this.isMouseAction() || (action == &#39;selectText&#39;); }, getTarget : function (asInstance) { var target = this.target return asInstance ? target : (target &amp;&amp; target.getTarget()) }, getTimestamp : function () { return this.sourceEvent &amp;&amp; this.sourceEvent.timestamp || this.timestamp; }, isMouseAction : function () { return (this.action || &#39;&#39;).toLowerCase() in { click : 1, contextmenu : 1, dblclick : 1, drag : 1, mousedown : 1, mouseup : 1, movecursorto : 1, wheel : 1 } }, resetValues : function () { this.target = null this.value = null this.toTarget = null this.by = null this.options = null this.sourceEvent = null this.waitForPageLoad = false }, parseOffset : function (offsetString) { var values = offsetString.split(&#39;,&#39;); if (values.length &lt; 2) return; if (!values[ 0 ].match(&#39;%&#39;)) { values[ 0 ] = parseInt(values[ 0 ], 10); if (isNaN(values[ 0 ])) return; } if (!values[ 1 ].match(&#39;%&#39;)) { values[ 1 ] = parseInt(values[ 1 ], 10); if (isNaN(values[ 1 ])) return; } return values; }, clearTargetOffset : function (keepOld) { this.setTargetOffset(null, keepOld) }, restoreTargetOffset : function () { var target = this.target if (target &amp;&amp; target.__SAVED_OFFSET__) { target.setOffset(target.__SAVED_OFFSET__) delete target.__SAVED_OFFSET__ } }, setTargetOffset : function (value, keepOld) { var target = this.target if (target) { if (keepOld) { var targetDesc = target.getTarget() target.__SAVED_OFFSET__ = targetDesc.offset } target.setOffset(value) } }, getTargetOffset : function () { var target = this.target if (target) return target.getOffset() }, objectToSource : function (obj) { var me = this; var result = &#39;&#39;; var prependComma; var convertFn = function (key, value) { value = value || obj[key]; if (value instanceof Array) { return key + &#39; : &#39; + JSON.stringify(value); } else if (value &amp;&amp; typeof value === &#39;object&#39;) { return key + &#39; : &#39; + me.objectToSource(value); } else { return key + &#39; : &#39; + (typeof (value) === &#39;string&#39; ? &#39;&quot;&#39; + value + &#39;&quot;&#39; : value); } }; if (obj.action == &#39;type&#39;) { result = convertFn(&#39;type&#39;, obj.text); prependComma = true; delete obj.action; delete obj.text; } else if (obj.target) { // Use brief action description, do this manually to make sure action + target is // the first item in the object descriptor result = convertFn(obj.action, obj.target); prependComma = true; delete obj.target; delete obj.action; } var keys = Object.keys(obj); if (keys.length &gt; 0) { result = result + (prependComma ? &#39;, &#39; : &#39;&#39;) + keys.map(function(key) { return convertFn(key); }).join(&#39;, &#39;); } return &#39;{ &#39; + result + &#39; }&#39;; }, asCode : function () { var step = this.asStep() if (!step) return null return typeof step == &#39;function&#39; ? step : this.objectToSource(step) }, asStep : function (test) { var actionName = this.action if (!actionName) return null var step = { action : this.action }; var target = this.getTarget() var value = this.value var hasTarget = this.hasTarget() if (hasTarget || actionName == &#39;type&#39; || actionName == &#39;scrollTo&#39;) { if (!target) { // If target is required but not filled in, just leave it blank step.target = &#39;&#39;; } else if (target.type == &#39;cq&#39;) { var splitPos = target.target.indexOf(&#39;-&gt;&#39;); step.target = splitPos &gt; 0 ? target.target.split(&#39;-&gt;&#39;).splice(1, 0, &#39;&gt;&gt;&#39;).join() : &#39;&gt;&gt;&#39; + target.target } else { step.target = target.target } if (target &amp;&amp; target.offset) step.offset = target.offset.slice() } if (this.options &amp;&amp; !Joose.O.isEmpty(this.options)) { step.options = this.options; } if (actionName.match(/^waitFor/)) { switch (actionName) { case &#39;waitForFn&#39;: // After this statement, t will be available in the evaled function below just as a regular local variable if (test) var t = test; return { waitFor : eval(&quot;(function() {\n &quot; + value.replace(/\n/g, &quot;\n &quot;) + &quot;\n })&quot;) }; case &#39;waitForMs&#39;: var val = parseInt(value, 10); return { waitForMs : val }; default: var obj = {}; obj[ actionName ] = value || []; return obj; } } else { switch (actionName) { case &#39;click&#39;: case &#39;dblclick&#39;: case &#39;contextmenu&#39;: case &#39;mousedown&#39;: case &#39;mouseup&#39;: case &#39;moveCursorTo&#39;: case &#39;wheel&#39;: break; case &#39;scrollTo&#39;: return { scrollTo : [ step.target, value[ 0 ], value[ 1 ] ] }; case &#39;moveCursorAlongPath&#39;: case &#39;moveMouseAlongPath&#39;: var step = {}; step[ actionName ] = [ value ]; return step case &#39;screenshot&#39;: step = { screenshot : value }; break; case &#39;moveCursorBy&#39;: var by = this.value.split(&#39;,&#39;); by[0] = parseInt(by[0], 10); by[1] = parseInt(by[1], 10); step = { action : &#39;moveCursor&#39;, by : by}; break; case &#39;type&#39;: step.text = value; break; case &#39;drag&#39;: var toTarget = this.toTarget if (toTarget &amp;&amp; !toTarget.isTooGeneric()) { step.to = toTarget.target; if (toTarget.offset) step.toOffset = toTarget.offset; break; } step.by = this.by; break; case &#39;fn&#39;: // After this statement, t will be available in the evaled function below just as a regular local variable if (test) var t = test; return eval(&quot;(function(next) {\n &quot; + value.replace(/\n/g, &quot;\n &quot;) + &quot;\n next();\n })&quot;); default: var step = {}; step[ actionName ] = value; return step; } } return this.waitForPageLoad ? { waitForPageLoad : [], trigger : step } : step; } // eof `asStep` } }); }(); </pre> </body> </html>