Skip to content

Commit

Permalink
chore: remove selenium-atoms
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuineng committed Nov 7, 2023
1 parent 8867167 commit 597c16f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
44 changes: 20 additions & 24 deletions lib/controllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const fs = require('fs');
const path = require('path');
const assert = require('assert');
const { sync: mkdirp } = require('mkdirp');
const { getByName: getAtom } = require('selenium-atoms');
const { errors } = require('webdriver-dfn-error-code');

const _ = require('./helper');
Expand All @@ -15,23 +14,27 @@ const implicitWaitForCondition = function(func) {
return _.waitForCondition(func, this?.implicitWaitMs);
};

const sendJSCommand = async function(atom, args, inDefaultFrame) {
const frames = !inDefaultFrame && this.pageIframe ? [ this.pageIframe ] : [];
const atomScript = getAtom(atom);
let script;
if (frames.length) {
const elem = getAtom('get_element_from_cache');
const frame = frames[0];
script = `(function (window) { var document = window.document;
return (${atomScript}); })((${elem.toString('utf8')})(${JSON.stringify(frame)}))`;
} else {
script = `(${atomScript})`;
}
const command = `${script}(${args.map(JSON.stringify).join(',')})`;
const sendJSCommand = async function(script) {
const atomScript = `(function(){return function(){var e=this;
function h(a){var b=typeof a;if("object"==b)if(a){if(a instanceof Array)return"array";if(a instanceof Object)return b;var c=Object.prototype.toString.call(a);if("[object Window]"==c)return"object";if("[object Array]"==c||"number"==typeof a.length&&"undefined"!=typeof a.splice&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("splice"))return"array";if("[object Function]"==c||"undefined"!=typeof a.call&&"undefined"!=typeof a.propertyIsEnumerable&&!a.propertyIsEnumerable("call"))return"function"}else return"null";else if("function"==
b&&"undefined"==typeof a.call)return"object";return b}function aa(a){var b=h(a);return"array"==b||"object"==b&&"number"==typeof a.length}function ba(a){var b=typeof a;return"object"==b&&null!=a||"function"==b}var l=Date.now||function(){return+new Date};var ca=window;function m(a,b){this.code=a;this.b=n[a]||p;this.message=b||"";var c=this.b.replace(/((?:^|\\s+)[a-z])/g,function(a){return a.toUpperCase().replace(/^[\\s\xa0]+/g,"")}),d=c.length-5;if(0>d||c.indexOf("Error",d)!=d)c+="Error";this.name=c;c=Error(this.message);c.name=this.name;this.stack=c.stack||""}
(function(){var a=Error;function b(){}b.prototype=a.prototype;m.c=a.prototype;m.prototype=new b;m.prototype.constructor=m;m.b=function(b,d,g){for(var f=Array(arguments.length-2),k=2;k<arguments.length;k++)f[k-2]=arguments[k];return a.prototype[d].apply(b,f)}})();var p="unknown error",n={15:"element not selectable",11:"element not visible"};n[31]=p;n[30]=p;n[24]="invalid cookie domain";n[29]="invalid element coordinates";n[12]="invalid element state";n[32]="invalid selector";n[51]="invalid selector";
n[52]="invalid selector";n[17]="javascript error";n[405]="unsupported operation";n[34]="move target out of bounds";n[27]="no such alert";n[7]="no such element";n[8]="no such frame";n[23]="no such window";n[28]="script timeout";n[33]="session not created";n[10]="stale element reference";n[21]="timeout";n[25]="unable to set cookie";n[26]="unexpected alert open";n[13]=p;n[9]="unknown command";m.prototype.toString=function(){return this.name+": "+this.message};var q=String.prototype.trim?function(a){return a.trim()}:function(a){return a.replace(/^[\\s\xa0]+|[\\s\xa0]+$/g,"")};
function r(a,b){for(var c=0,d=q(String(a)).split("."),g=q(String(b)).split("."),f=Math.max(d.length,g.length),k=0;0==c&&k<f;k++){var H=d[k]||"",I=g[k]||"",u=RegExp("(\\\\d*)(\\\\D*)","g"),sa=RegExp("(\\\\d*)(\\\\D*)","g");do{var v=u.exec(H)||["","",""],w=sa.exec(I)||["","",""];if(0==v[0].length&&0==w[0].length)break;c=t(0==v[1].length?0:parseInt(v[1],10),0==w[1].length?0:parseInt(w[1],10))||t(0==v[2].length,0==w[2].length)||t(v[2],w[2])}while(0==c)}return c}function t(a,b){return a<b?-1:a>b?1:0};function x(a,b){for(var c=a.length,d=Array(c),g="string"==typeof a?a.split(""):a,f=0;f<c;f++)f in g&&(d[f]=b.call(void 0,g[f],f,a));return d};var y;a:{var z=e.navigator;if(z){var A=z.userAgent;if(A){y=A;break a}}y=""}function B(a){return-1!=y.indexOf(a)};function da(a,b){var c={},d;for(d in a)b.call(void 0,a[d],d,a)&&(c[d]=a[d]);return c}function C(a,b){var c={},d;for(d in a)c[d]=b.call(void 0,a[d],d,a);return c}function D(a,b){return null!==a&&b in a}function ea(a,b){for(var c in a)if(b.call(void 0,a[c],c,a))return c};function E(){return B("Opera")||B("OPR")}function F(){return(B("Chrome")||B("CriOS"))&&!E()&&!B("Edge")};function G(){return B("iPhone")&&!B("iPod")&&!B("iPad")};var fa=E(),J=B("Trident")||B("MSIE"),ga=B("Edge"),K=B("Gecko")&&!(-1!=y.toLowerCase().indexOf("webkit")&&!B("Edge"))&&!(B("Trident")||B("MSIE"))&&!B("Edge"),ha=-1!=y.toLowerCase().indexOf("webkit")&&!B("Edge");function ia(){var a=y;if(K)return/rv\\:([^\\);]+)(\\)|;)/.exec(a);if(ga)return/Edge\\/([\\d\\.]+)/.exec(a);if(J)return/\\b(?:MSIE|rv)[: ]([^\\);]+)(\\)|;)/.exec(a);if(ha)return/WebKit\\/(\\S+)/.exec(a)}function ja(){var a=e.document;return a?a.documentMode:void 0}
var L=function(){if(fa&&e.opera){var a;var b=e.opera.version;try{a=b()}catch(c){a=b}return a}a="";(b=ia())&&(a=b?b[1]:"");return J&&(b=ja(),b>parseFloat(a))?String(b):a}(),M={},ka=e.document,la=ka&&J?ja()||("CSS1Compat"==ka.compatMode?parseInt(L,10):5):void 0;var ma=B("Firefox"),na=G()||B("iPod"),oa=B("iPad"),N=B("Android")&&!(F()||B("Firefox")||E()||B("Silk")),pa=F(),qa=B("Safari")&&!(F()||B("Coast")||E()||B("Edge")||B("Silk")||B("Android"))&&!(G()||B("iPad")||B("iPod"));function O(a){return(a=a.exec(y))?a[1]:""}var ra=function(){if(ma)return O(/Firefox\\/([0-9.]+)/);if(J||ga||fa)return L;if(pa)return O(/Chrome\\/([0-9.]+)/);if(qa&&!(G()||B("iPad")||B("iPod")))return O(/Version\\/([0-9.]+)/);if(na||oa){var a;if(a=/Version\\/(\\S+).*Mobile\\/(\\S+)/.exec(y))return a[1]+"."+a[2]}else if(N)return(a=O(/Android\\s+([0-9.]+)/))?a:O(/Version\\/([0-9.]+)/);return""}();var P,ta;function Q(a){R?ta(a):N?r(ua,a):r(ra,a)}var R=function(){if(!K)return!1;var a=e.Components;if(!a)return!1;try{if(!a.classes)return!1}catch(b){return!1}var c=a.classes,a=a.interfaces,d=c["@mozilla.org/xpcom/version-comparator;1"].getService(a.nsIVersionComparator),c=c["@mozilla.org/xre/app-info;1"].getService(a.nsIXULAppInfo),g=c.platformVersion,f=c.version;P=function(a){return 0<=d.compare(g,""+a)};ta=function(a){d.compare(f,""+a)};return!0}(),S;
if(N){var va=/Android\\s+([0-9\\.]+)/.exec(y);S=va?va[1]:"0"}else S="0";var ua=S;N&&Q(2.3);N&&Q(4);qa&&Q(6);function wa(){}
function T(a,b,c){if(null==b)c.push("null");else{if("object"==typeof b){if("array"==h(b)){var d=b;b=d.length;c.push("[");for(var g="",f=0;f<b;f++)c.push(g),T(a,d[f],c),g=",";c.push("]");return}if(b instanceof String||b instanceof Number||b instanceof Boolean)b=b.valueOf();else{c.push("{");g="";for(d in b)Object.prototype.hasOwnProperty.call(b,d)&&(f=b[d],"function"!=typeof f&&(c.push(g),xa(d,c),c.push(":"),T(a,f,c),g=","));c.push("}");return}}switch(typeof b){case "string":xa(b,c);break;case "number":c.push(isFinite(b)&&
!isNaN(b)?String(b):"null");break;case "boolean":c.push(String(b));break;case "function":c.push("null");break;default:throw Error("Unknown type: "+typeof b);}}}var ya={'"':'\\\\"',"\\\\":"\\\\\\\\","/":"\\\\/","\\b":"\\\\b","\\f":"\\\\f","\\n":"\\\\n","\\r":"\\\\r","\\t":"\\\\t","\x0B":"\\\u000b"},za=/\uffff/.test("\uffff")?/[\\\\\\"\x00-\x1f\x7f-\uffff]/g:/[\\\\\\"\x00-\x1f\x7f-\xff]/g;
function xa(a,b){b.push('"',a.replace(za,function(a){var b=ya[a];b||(b="\\\\u"+(a.charCodeAt(0)|65536).toString(16).substr(1),ya[a]=b);return b}),'"')};ha||K&&(R?P(3.5):J?0<=r(la,3.5):M[3.5]||(M[3.5]=0<=r(L,3.5)))||J&&(R?P(8):J?r(la,8):M[8]||(M[8]=0<=r(L,8)));function U(a){switch(h(a)){case "string":case "number":case "boolean":return a;case "function":return a.toString();case "array":return x(a,U);case "object":if(D(a,"nodeType")&&(1==a.nodeType||9==a.nodeType)){var b={};b.ELEMENT=Aa(a);return b}if(D(a,"document"))return b={},b.WINDOW=Aa(a),b;if(aa(a))return x(a,U);a=da(a,function(a,b){return"number"==typeof b||"string"==typeof b});return C(a,U);default:return null}}
function V(a,b){return"array"==h(a)?x(a,function(a){return V(a,b)}):ba(a)?"function"==typeof a?a:D(a,"ELEMENT")?Ba(a.ELEMENT,b):D(a,"WINDOW")?Ba(a.WINDOW,b):C(a,function(a){return V(a,b)}):a}function Ca(a){a=a||document;var b=a.$wdc_;b||(b=a.$wdc_={},b.a=l());b.a||(b.a=l());return b}function Aa(a){var b=Ca(a.ownerDocument),c=ea(b,function(b){return b==a});c||(c=":wdc:"+b.a++,b[c]=a);return c}
function Ba(a,b){a=decodeURIComponent(a);var c=b||document,d=Ca(c);if(!D(d,a))throw new m(10,"Element does not exist in cache");var g=d[a];if(D(g,"setInterval")){if(g.closed)throw delete d[a],new m(23,"Window has been closed.");return g}for(var f=g;f;){if(f==c.documentElement)return g;f=f.parentNode}delete d[a];throw new m(10,"Element is no longer attached to the DOM");};function Da(a,b,c,d){d=d||ca;var g;try{a:{var f=a;if("string"==typeof f)try{a=new d.Function(f);break a}catch(k){if(J&&d.execScript){d.execScript(";");a=new d.Function(f);break a}throw k;}a=d==window?f:new d.Function("return ("+f+").apply(null,arguments);")}var H=V(b,d.document),I=a.apply(null,H);g={status:0,value:U(I)}}catch(u){g={status:D(u,"code")?u.code:13,value:{message:u.message}}}c&&(a=[],T(new wa,g,a),g=a.join(""));return g}var W=["_"],X=e;W[0]in X||!X.execScript||X.execScript("var "+W[0]);
for(var Y;W.length&&(Y=W.shift());){var Z;if(Z=!W.length)Z=void 0!==Da;Z?X[Y]=Da:X[Y]?X=X[Y]:X=X[Y]={}};; return this._.apply(null,arguments);}.apply({navigator:typeof window!=undefined?window.navigator:null,document:typeof window!=undefined?window.document:null}, arguments);})`;

const command = `${atomScript}(${JSON.stringify(script)})`;

let res;
await implicitWaitForCondition.call(this, async () => {
res = await this.page.evaluate(command);
res = await (this.pageIframe || this.page).evaluate(command);
return !!res;
});

Expand Down Expand Up @@ -305,18 +308,11 @@ controllers.title = async function() {
*
* @module execute
* @param script script
* @param [args] script argument array
* @return {Promise.<string>}
*/
controllers.execute = async function(script, args) {
if (!args) {
args = [];
}
controllers.execute = async function(script) {

const value = await sendJSCommand.call(this, 'execute_script', [
script,
args,
], true);
const value = await sendJSCommand.call(this, script);

if (Array.isArray(value)) {
return value.map(convertAtoms2Element.bind(this));
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"lodash": "^4.17.21",
"mkdirp": "^1.0.4",
"playwright": "^1.38.0",
"selenium-atoms": "^1.0.4",
"webdriver-dfn-error-code": "^1.0.4",
"xlogger": "^1.0.6"
},
Expand Down

0 comments on commit 597c16f

Please sign in to comment.