Best JavaScript code snippet using ava
HtmlElementExecutor.js
Source:HtmlElementExecutor.js
1FBL.ns(function() { with (FBL) {2/*************************************************************************************/3var fcModel = Firecrow.Interpreter.Model;4var ValueTypeHelper = Firecrow.ValueTypeHelper;56fcModel.HtmlElementExecutor =7{8 addDependencyIfImportantElement: function(htmlElement, globalObject, codeConstruct)9 {10 if(globalObject.satisfiesDomSlicingCriteria(htmlElement))11 {12 globalObject.browser.callImportantConstructReachedCallbacks(codeConstruct);13 }14 },1516 addDependenciesToAllDescendantsModifications: function(htmlElement, codeConstruct, globalObject)17 {18 fcModel.Object.prototype.addDependencyToAllModifications.call19 (20 {21 htmlElement:htmlElement,22 globalObject:globalObject,23 dependencyCreator: globalObject.dependencyCreator24 },25 codeConstruct,26 htmlElement.elementModificationPoints27 );2829 var childNodes = htmlElement.childNodes;3031 for(var i = 0, length = childNodes.length; i < length; i++)32 {33 this.addDependenciesToAllDescendantsModifications(childNodes[i], codeConstruct, globalObject);34 }35 },3637 addDependenciesToAllDescendantElements: function(htmlElement, codeConstruct, globalObject)38 {39 if(htmlElement == null || globalObject == null) { return; }4041 var childNodes = htmlElement.childNodes;42 var evaluationPositionId = globalObject.getPreciseEvaluationPositionId();4344 for(var i = 0, length = childNodes.length; i < length; i++)45 {46 var childNode = childNodes[i];47 globalObject.dependencyCreator.createDataDependency(htmlElement.modelElement, childNode.modelElement, evaluationPositionId);48 this.addDependenciesToAllDescendantElements(childNode, codeConstruct, globalObject);49 }50 },5152 executeInternalMethod: function(thisObject, functionObject, args, callExpression)53 {54 if(!functionObject.isInternalFunction) { fcModel.HtmlElement.notifyError("The function should be internal when executing html method!"); return; }5556 var functionObjectValue = functionObject.jsValue;57 var thisObjectValue = thisObject.jsValue;58 var functionName = functionObjectValue != null ? functionObjectValue.name : functionObject.iValue.name;59 var fcThisValue = thisObject.iValue;60 var globalObject = fcThisValue.globalObject;61 var jsArguments = globalObject.getJsValues(args)6263 switch(functionName)64 {65 case "getElementsByTagName":66 case "getElementsByClassName":67 case "querySelectorAll":68 return this._getElements(functionName, globalObject, args[0].jsValue, thisObjectValue, jsArguments, callExpression);69 case "querySelector":70 return this._getElement(functionName, globalObject, args[0].jsValue, thisObjectValue, jsArguments, callExpression);71 case "getAttribute":72 return this._getAttribute(functionName, thisObjectValue, jsArguments, globalObject, callExpression);73 case "getAttributeNode":74 return this._getAttributeNode(functionName, thisObjectValue, jsArguments, globalObject, callExpression);75 case "setAttribute":76 case "removeAttribute":77 return this._modifyAttribute(functionName, thisObjectValue, jsArguments, globalObject, callExpression);78 case "appendChild":79 case "removeChild":80 case "insertBefore":81 case "replaceChild":82 return this._modifyDOM(functionName, thisObjectValue, args, jsArguments, globalObject, callExpression);83 case "cloneNode":84 return this._cloneNode(functionName, thisObjectValue, jsArguments, globalObject, callExpression);85 case "addEventListener":86 this._registerEventHandler(fcThisValue, jsArguments, args[1], globalObject, callExpression);87 case "removeEventListener":88 this._removeEventHandler(thisObjectValue, globalObject, callExpression);89 return globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, undefined);90 case "matchesSelector":91 case "mozMatchesSelector":92 case "webkitMatchesSelector":93 globalObject.browser.logDomQueried(functionName, args[0].jsValue, callExpression);94 case "compareDocumentPosition":95 case "contains":96 return this._queryDocument(functionName, thisObjectValue, jsArguments, globalObject, callExpression);97 case "getBoundingClientRect":98 return this._getBoundingClientRectangle(functionName, thisObjectValue, jsArguments, globalObject, callExpression);99 case "getContext":100 if(ValueTypeHelper.isCanvasElement(thisObjectValue))101 {102 return fcModel.CanvasExecutor.executeCanvasMethod(thisObject, functionObject, args, callExpression);103 }104 case "click":105 case "reset":106 case "blur":107 case "focus":108 //TODO - problem with reset, my html nodes are created by document.createElement() and then109 //setting properties, and reset resets to the values in the HTML code, and not to the dynamically set ones110 if(thisObjectValue[functionName] != null)111 {112 try{thisObjectValue[functionName]();}catch(e){}113 thisObjectValue.elementModificationPoints.push({ codeConstruct: callExpression, evaluationPositionId: globalObject.getPreciseEvaluationPositionId()});114 fcModel.HtmlElementExecutor.addDependencyIfImportantElement(thisObjectValue, globalObject, callExpression);115 }116 break;117 default:118 fcModel.HtmlElement.notifyError("Unhandled internal method:" + functionName); return;119 }120 },121122 wrapToFcElements: function(items, globalObject, codeConstruct)123 {124 try125 {126 var fcItems = [];127128 for(var i = 0, length = items.length; i < length; i++)129 {130 var item = items[i];131 fcItems.push(this.wrapToFcElement(item, globalObject, codeConstruct));132 }133134 return globalObject.internalExecutor.createArray(codeConstruct, fcItems);135 }136 catch(e)137 {138 fcModel.HtmlElement.notifyError("HtmlElementExecutor - error when wrapping: " + e);139 }140 },141142 wrapToFcElement: function(item, globalObject, codeConstruct)143 {144 try145 {146 if(item == null) { return new fcModel.fcValue(item, item, codeConstruct); }147148 if(ValueTypeHelper.isHtmlElement(item) || ValueTypeHelper.isDocumentFragment(item) || ValueTypeHelper.isImageElement(item))149 {150 var fcHtmlElement = globalObject.document.htmlElementToFcMapping[item.fcHtmlElementId];151152 if(fcHtmlElement == null) { fcHtmlElement = new fcModel.HtmlElement(item, globalObject, codeConstruct); }153154 if(ValueTypeHelper.isImageElement(item))155 {156 fcHtmlElement.addProperty("__proto__", globalObject.fcHtmlImagePrototype);157 }158 else if (ValueTypeHelper.isCanvasElement(item))159 {160 fcHtmlElement.addProperty("__proto__", globalObject.fcCanvasPrototype);161 }162163 return new fcModel.fcValue(item, fcHtmlElement, codeConstruct);164 }165 else if (ValueTypeHelper.isTextNode(item))166 {167 return new fcModel.fcValue(item, new fcModel.TextNode(item, globalObject, codeConstruct), codeConstruct);168 }169 else if (ValueTypeHelper.isDocument(item))170 {171 return globalObject.jsFcDocument;172 }173 else174 {175 debugger;176 fcModel.HtmlElement.notifyError("HtmlElementExecutor - when wrapping should not be here: " + item);177 }178 }179 catch(e)180 {181 debugger;182 fcModel.HtmlElement.notifyError("HtmlElementExecutor - error when wrapping: " + e);183 }184 },185186 addDependencies: function(element, codeConstruct, globalObject)187 {188 try189 {190 if(element == null) { return; }191192 if(ValueTypeHelper.isArray(element))193 {194 for(var i = 0; i < element.length; i++)195 {196 this.addDependencies(element[i], codeConstruct, globalObject);197 }198199 return;200 }201202 var evaluationPositionId = globalObject.getPreciseEvaluationPositionId();203204 if(element.modelElement != null)205 {206 globalObject.dependencyCreator.createValueDataDependency(codeConstruct, element.modelElement, evaluationPositionId);207 }208209 if (element.creationPoint != null)210 {211 globalObject.dependencyCreator.createDataDependency212 (213 codeConstruct,214 element.creationPoint.codeConstruct,215 evaluationPositionId,216 element.creationPoint.evaluationPositionId217 );218 }219220 if(element.domInsertionPoint != null)221 {222 globalObject.dependencyCreator.createDataDependency223 (224 codeConstruct,225 element.domInsertionPoint.codeConstruct,226 evaluationPositionId,227 element.domInsertionPoint.evaluationPositionId228 );229 }230231 if(element.elementModificationPoints != null)232 {233 var elementModificationPoints = element.elementModificationPoints;234235 for(var i = 0, length = elementModificationPoints.length; i < length; i++)236 {237 var elementModificationPoint = elementModificationPoints[i];238239 globalObject.dependencyCreator.createDataDependency240 (241 codeConstruct,242 elementModificationPoint.codeConstruct,243 evaluationPositionId,244 elementModificationPoint.evaluationPositionId245 );246 }247 }248 }249 catch(e) { fcModel.HtmlElement.notifyError("Error when adding dependencies: " + e); }250 },251252 _getElements: function(functionName, globalObject, argument, thisObjectValue, jsArguments, callExpression)253 {254 var elements = [];255256 try257 {258 globalObject.browser.logDomQueried(functionName, argument, callExpression);259 elements = thisObjectValue[functionName].apply(thisObjectValue, jsArguments);260 }261 catch(e)262 {263 //TODO - IF statement (the else should always be, but the if should not)264 // is a jQuery hack, for some reason - should not throw an exception if thisObjectValue is documentFragment265 //and jsArguments is a simple selector266 if(ValueTypeHelper.isDocumentFragment(thisObjectValue) && jsArguments.length == 1267 && (jsArguments[0] == "*" || jsArguments[0] == "script"))268 {269 elements = this._getElementsFromDocumentFragment(thisObjectValue, jsArguments[0], functionName);270 }271 else272 {273 globalObject.executionContextStack.callExceptionCallbacks274 ({275 exceptionGeneratingConstruct: callExpression,276 isDomStringException: true277 });278 }279 }280281 for(var i = 0, length = elements.length; i < length; i++)282 {283 this.addDependencies(elements[i], callExpression, globalObject);284 }285286 var wrappedArray = this.wrapToFcElements(elements, globalObject, callExpression);287288 wrappedArray.iValue.markAsNodeList();289290 return wrappedArray;291 },292293 _getElement: function(functionName, globalObject, argument, thisObjectValue, jsArguments, callExpression)294 {295 var element = null;296297 try298 {299 globalObject.browser.logDomQueried(functionName, argument, callExpression);300 element = thisObjectValue[functionName].apply(thisObjectValue, jsArguments);301 }302 catch(e)303 {304 globalObject.executionContextStack.callExceptionCallbacks305 ({306 exceptionGeneratingConstruct: callExpression,307 isDomStringException: true308 });309 }310311 this.addDependencies(element, callExpression, globalObject);312313 return this.wrapToFcElement(element, globalObject, callExpression);314 },315316 _getElementsFromDocumentFragment: function(documentFragment, selector, functionName)317 {318 var elements = [];319320 for(var i = 0; i < documentFragment.childNodes.length; i++)321 {322 var childNode = documentFragment.childNodes[i];323 var matchesSelector = false;324325 if(childNode.webkitMatchesSelector) { matchesSelector = childNode.webkitMatchesSelector(selector); }326 else if(childNode.mozMatchesSelector) { matchesSelector = childNode.mozMatchesSelector(selector); }327 else if(childNode.oMatchesSelector) { matchesSelector = childNode.oMatchesSelector(selector); }328 else if(childNode.msMatchesSelector) { matchesSelector = childNode.msMatchesSelector(selector); }329330 if(matchesSelector)331 {332 elements.push(childNode);333 }334335 if(childNode[functionName])336 {337 var descendents = childNode[functionName].apply(childNode, [selector]);338339 for(var j = 0; j < descendents.length; j++)340 {341 elements.push(descendents[j]);342 }343 }344 }345346 return elements;347 },348349 _getAttribute: function(functionName, thisObjectValue, jsArguments, globalObject, callExpression)350 {351 var result = thisObjectValue[functionName].apply(thisObjectValue, jsArguments);352 this.addDependencies(thisObjectValue, callExpression, globalObject);353354 return globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result);355 },356357 _getAttributeNode: function(functionName, thisObjectValue, jsArguments, globalObject, callExpression)358 {359 try360 {361 var attribute = thisObjectValue[functionName].apply(thisObjectValue, jsArguments);362 }363 catch(e) { console.log("Exception when getAttributeNode - probably irrelevant"); }364365 if(attribute == null) { return globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, null); }366367 this.addDependencies(thisObjectValue, callExpression, globalObject);368369 return fcModel.Attr.wrapAttribute(attribute, globalObject, callExpression);370371 },372373 _modifyAttribute: function(functionName, thisObjectValue, jsArguments, globalObject, callExpression)374 {375 thisObjectValue[functionName].apply(thisObjectValue, jsArguments);376 thisObjectValue.elementModificationPoints.push({ codeConstruct: callExpression, evaluationPositionId: globalObject.getPreciseEvaluationPositionId()});377 fcModel.HtmlElementExecutor.addDependencyIfImportantElement(thisObjectValue, globalObject, callExpression);378379 if(jsArguments.length >= 2380 && (jsArguments[0] == "class" || jsArguments[0] == "id"))381 {382 globalObject.browser.createDependenciesBetweenHtmlNodeAndCssNodes(thisObjectValue.modelElement);383 }384385 return globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, undefined);386 },387388 _modifyDOM: function(functionName, thisObjectValue, args, jsArguments, globalObject, callExpression)389 {390 thisObjectValue[functionName].apply(thisObjectValue, jsArguments);391 thisObjectValue.elementModificationPoints.push({ codeConstruct: callExpression, evaluationPositionId: globalObject.getPreciseEvaluationPositionId()});392 fcModel.HtmlElementExecutor.addDependencyIfImportantElement(thisObjectValue, globalObject, callExpression);393394 if(functionName == "appendChild" || functionName == "insertBefore")395 {396 this._createCssDependencies(thisObjectValue, globalObject);397 }398399 for(var i = 0; i < args.length; i++)400 {401 var manipulatedElement = args[i].iValue;402403 if(manipulatedElement != null && !manipulatedElement.isComment) //Because of comments404 {405 try406 {407 manipulatedElement.notifyElementInsertedIntoDom(callExpression);408 }409 catch(e) { debugger;}410 }411 }412413 if(functionName == "replaceChild") { return args[args.length - 1]; }414415 return args[0];416 },417418 _createCssDependencies: function(htmlElement, globalObject)419 {420 if(htmlElement == null) { return; }421422 if(htmlElement.modelElement)423 {424 htmlElement.modelElement.domElement = htmlElement;425 }426427 globalObject.browser.createDependenciesBetweenHtmlNodeAndCssNodes(htmlElement.modelElement);428429 var children = htmlElement.childNodes;430 if(children == null || children.length == 0) { return; }431432 for(var i = 0; i < children.length; i++)433 {434 this._createCssDependencies(children[i], globalObject)435 }436 },437438 _cloneNode: function(functionName, thisObjectValue, jsArguments, globalObject, callExpression)439 {440 this.addDependenciesToAllDescendantsModifications(thisObjectValue, callExpression, globalObject);441442 var clonedNode = thisObjectValue[functionName].apply(thisObjectValue, jsArguments,443 {444 codeConstruct: callExpression,445 evaluationPositionId: globalObject.getPreciseEvaluationPositionId()446 });447448 this._copyModelElements(thisObjectValue, clonedNode);449450 return this.wrapToFcElement(clonedNode, globalObject, callExpression);451 },452453 _copyModelElements: function(originalElement, clonedElement, creationPoint)454 {455 clonedElement.modelElement = originalElement.modelElement;456 clonedElement.creationPoint = creationPoint;457458 for(var i = 0; i < originalElement.childNodes.length; i++)459 {460 this._copyModelElements(originalElement.childNodes[i], clonedElement.childNodes[i], creationPoint);461 }462 },463464 _registerEventHandler: function(fcThisValue, jsArguments, handler, globalObject, callExpression)465 {466 if(fcThisValue && fcThisValue.implementationObject && fcThisValue.implementationObject.modelElement)467 {468 globalObject.dependencyCreator.createDataDependency469 (470 fcThisValue.implementationObject.modelElement,471 callExpression,472 globalObject.getPreciseEvaluationPositionId()473 );474 }475476 globalObject.registerHtmlElementEventHandler477 (478 fcThisValue,479 jsArguments[0],480 handler,481 {482 codeConstruct: callExpression,483 evaluationPositionId: globalObject.getPreciseEvaluationPositionId()484 }485 );486 },487488 _removeEventHandler: function(thisObjectValue, globalObject, callExpression)489 {490 fcModel.HtmlElementExecutor.addDependencyIfImportantElement(thisObjectValue, globalObject, callExpression);491 thisObjectValue.elementModificationPoints.push({ codeConstruct: callExpression, evaluationPositionId: globalObject.getPreciseEvaluationPositionId()});492 },493494 _queryDocument: function(functionName, thisObjectValue, jsArguments, globalObject, callExpression)495 {496 var result = false;497 try498 {499 result = thisObjectValue[functionName].apply(thisObjectValue, jsArguments);500 }501 catch(e)502 {503 globalObject.executionContextStack.callExceptionCallbacks504 ({505 exceptionGeneratingConstruct: callExpression,506 isDomStringException: true507 });508 }509510 return globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result);511 },512513 _getBoundingClientRectangle: function(functionName, thisObjectValue, jsArguments, globalObject, callExpression)514 {515 try516 {517 var result = thisObjectValue[functionName].apply(thisObjectValue, jsArguments);518519 var nativeObj =520 {521 bottom: globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result.bottom),522 top: globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result.top),523 left: globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result.left),524 right: globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result.right),525 height: globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result.height),526 width: globalObject.internalExecutor.createInternalPrimitiveObject(callExpression, result.width)527 };528529 var fcObj = fcModel.Object.createObjectWithInit(globalObject, callExpression, nativeObj);530531 fcObj.addProperty("bottom", nativeObj.bottom, callExpression);532 fcObj.addProperty("top", nativeObj.top, callExpression);533 fcObj.addProperty("left", nativeObj.left, callExpression);534 fcObj.addProperty("right", nativeObj.right, callExpression);535 fcObj.addProperty("height", nativeObj.height, callExpression);536 fcObj.addProperty("width", nativeObj.width, callExpression);537538 return new fcModel.fcValue(nativeObj, fcObj, callExpression);539 }540 catch(e) { fcModel.HtmlElement.notifyError("Error when adding dependencies: " + e); }541542 return new fcModel.fcValue(undefined, null, callExpression);543 },544545 notifyError: function(message) { alert("HtmlElementExecutor - " + message); }546}547/*************************************************************************************/
...
assertionVisitor.mjs
Source:assertionVisitor.mjs
1import { addPageToTest, looksLike } from '../../../utils/utils.mjs';2import { types } from 'recast';3const builders = types.builders;4const updateAssertion = (node, filePath) => {5 const pageLocator = filePath.includes('pageObjects')6 ? builders.thisExpression()7 : builders.identifier('page');8 const assertion = 'toHaveText';9 const args = node.expression.argument.arguments;10 const selector = args[0];11 const valueToCheck = [args[2]];12 const reverse = args[3];13 const nth = args[4];14 const locator = nth15 ? [16 builders.callExpression(17 builders.memberExpression(18 builders.callExpression(19 builders.memberExpression(pageLocator, builders.identifier('locator')),20 [selector]21 ),22 builders.identifier('nth')23 ),24 [nth]25 ),26 ]27 : [28 builders.callExpression(29 builders.memberExpression(pageLocator, builders.identifier('locator')),30 [args[0]]31 ),32 ];33 if (!reverse || (reverse?.type === 'BooleanLiteral' && reverse.value === true)) {34 const expect = builders.awaitExpression(35 builders.callExpression(36 builders.memberExpression(37 builders.callExpression(builders.identifier('expect'), locator),38 builders.identifier(assertion)39 ),40 valueToCheck41 )42 );43 node.expression = expect;44 } else if (reverse?.type === 'BooleanLiteral' && reverse.value === false) {45 const expect = builders.awaitExpression(46 builders.callExpression(47 builders.memberExpression(48 builders.memberExpression(49 builders.callExpression(builders.identifier('expect'), locator),50 builders.identifier('not')51 ),52 builders.identifier(assertion)53 ),54 []55 )56 );57 node.expression = expect;58 } else if (reverse?.type === 'Identifier') {59 const expect = builders.ifStatement(60 reverse,61 builders.blockStatement([62 builders.expressionStatement(63 builders.awaitExpression(64 builders.callExpression(65 builders.memberExpression(66 builders.callExpression(builders.identifier('expect'), locator),67 builders.identifier(assertion)68 ),69 valueToCheck70 )71 )72 ),73 ]),74 builders.blockStatement([75 builders.expressionStatement(76 builders.awaitExpression(77 builders.callExpression(78 builders.memberExpression(79 builders.memberExpression(80 builders.callExpression(builders.identifier('expect'), locator),81 builders.identifier('not')82 ),83 builders.identifier(assertion)84 ),85 []86 )87 )88 ),89 ])90 );91 node.expression = expect;92 }93};94const updateEquality = node => {95 const arg1 = node.expression.arguments[0];96 const arg2 = node.expression.arguments[1];97 node.expression = builders.callExpression(98 builders.memberExpression(99 builders.callExpression(builders.identifier('expect'), [arg1]),100 builders.identifier('toBe')101 ),102 [arg2]103 );104};105const updateTruthyAssertion = node => {106 const arg1 = node.expression.arguments[0];107 const message = node.expression.arguments[1];108 const reverse = node.expression.arguments[2];109 if (arg1?.type === 'BinaryExpression' && arg1.operator === '===') {110 node.expression = builders.callExpression(111 builders.memberExpression(112 builders.callExpression(builders.identifier('expect'), [arg1.left]),113 builders.identifier('toBe')114 ),115 [arg1.right]116 );117 }118 if (arg1?.type === 'BinaryExpression' && arg1.operator === '!==') {119 node.expression = builders.callExpression(120 builders.memberExpression(121 builders.memberExpression(122 builders.callExpression(builders.identifier('expect'), [arg1.left]),123 builders.identifier('not')124 ),125 builders.identifier('toBe')126 ),127 [arg1.right]128 );129 }130 if (arg1?.type === 'BinaryExpression' && arg1.operator === '<=') {131 node.expression = builders.callExpression(132 builders.memberExpression(133 builders.callExpression(builders.identifier('expect'), [arg1.left]),134 builders.identifier('toBeLessThanOrEqual')135 ),136 [arg1.right]137 );138 }139 if (arg1?.type === 'Identifier' && !reverse) {140 node.expression = builders.callExpression(141 builders.memberExpression(142 builders.callExpression(builders.identifier('expect'), [arg1, message]),143 builders.identifier('toBe')144 ),145 [builders.booleanLiteral(true)]146 );147 }148 if (arg1.type === 'CallExpression' && arg1?.callee?.property?.name === 'includes' && !reverse) {149 node.expression = builders.callExpression(150 builders.memberExpression(151 builders.callExpression(builders.identifier('expect'), [arg1.callee.object]),152 builders.identifier('toContain')153 ),154 arg1.arguments155 );156 }157};158const fieldsVisibility = (elem, isVis, filePath) => {159 const pageLocator = filePath.includes('pageObjects')160 ? builders.thisExpression()161 : builders.identifier('page');162 if (isVis) {163 return builders.expressionStatement(164 builders.awaitExpression(165 builders.callExpression(166 builders.memberExpression(167 builders.callExpression(builders.identifier('expect'), [168 builders.callExpression(169 builders.memberExpression(170 pageLocator,171 builders.identifier('locator')172 ),173 [elem]174 ),175 ]),176 builders.identifier('toBeDisabled')177 ),178 []179 )180 )181 );182 } else {183 return builders.expressionStatement(184 builders.awaitExpression(185 builders.callExpression(186 builders.memberExpression(187 builders.memberExpression(188 builders.callExpression(builders.identifier('expect'), [189 builders.callExpression(190 builders.memberExpression(191 pageLocator,192 builders.identifier('locator')193 ),194 [elem]195 ),196 ]),197 builders.identifier('not')198 ),199 builders.identifier('toBeDisabled')200 ),201 []202 )203 )204 );205 }206};207const updateFieldsAssertion = (node, filePath) => {208 const body = node.body.reduce((body, curr) => {209 const isArray = looksLike(curr, {210 expression: {211 argument: {212 callee: {213 property: {214 name: n => n === 'checkFieldsDisabled',215 },216 },217 arguments: args => args[0].type === 'ArrayExpression',218 },219 },220 });221 const isVariable = looksLike(curr, {222 expression: {223 argument: {224 callee: {225 property: {226 name: n => n === 'checkFieldsDisabled',227 },228 },229 },230 },231 });232 if (isArray) {233 const isVis = curr.expression.argument.arguments?.[1]?.value ?? true;234 const array = curr.expression.argument.arguments[0].elements.map(elem =>235 fieldsVisibility(elem, isVis, filePath)236 );237 return [...body, ...array];238 } else if (isVariable) {239 const isVis = curr.expression.argument.arguments?.[1]?.value ?? true;240 const forStatement = builders.forOfStatement(241 builders.variableDeclaration('const', [242 builders.variableDeclarator(builders.identifier('selector')),243 ]),244 curr.expression.argument.arguments[0],245 builders.blockStatement([246 fieldsVisibility(builders.identifier('selector'), isVis, filePath),247 ])248 );249 return [...body, forStatement];250 } else {251 return [...body, curr];252 }253 }, []);254 node.body = body;255};256export default {257 getVisitors() {258 return {259 visitBlockStatement(path) {260 const isAssertion = looksLike(path.node, {261 body: b =>262 b.some(node =>263 looksLike(node, {264 expression: {265 callee: {266 property: {267 name: n => n === 'truthyAssertion',268 },269 },270 },271 })272 ),273 });274 if (isAssertion) {275 // updateTruthyAssertion(path.node);276 // updateEquality(path.node);277 // updateAssertion(path.node, filePath);278 // updateFieldsAssertion(path.node, filePath);279 // addPageToTest(path);280 }281 this.traverse(path);282 },283 visitExpressionStatement(path) {284 const isAssertion = looksLike(path.node, {285 expression: {286 callee: {287 property: {288 name: n => n === 'truthyAssertion',289 },290 },291 },292 });293 if (isAssertion) {294 updateTruthyAssertion(path.node);295 // updateEquality(path.node);296 // updateAssertion(path.node, filePath);297 // updateFieldsAssertion(path.node, filePath);298 // addPageToTest(path);299 }300 this.traverse(path);301 },302 };303 },304 getIfVisitors() {305 return {306 visitBlockStatement(path) {307 const body = [];308 for (const node of path.node.body) {309 if (node.type === 'IfStatement' && node.test.type === 'Identifier') {310 const index = body.findIndex(311 val =>312 val?.test?.type === 'Identifier' &&313 val?.test.name === node.test.name314 );315 if (index > -1) {316 console.log(index);317 const consequent = builders.blockStatement([318 ...body[index].consequent.body,319 ...node.consequent.body,320 ]);321 const alternate = [];322 console.log(node.alternate);323 if (body[index]?.alternate?.body.length > 0) {324 alternate.push(...body[index].alternate.body);325 }326 if (node?.alternate?.body.length > 0) {327 alternate.push(...node.alternate.body);328 }329 const newIf =330 alternate.length > 0331 ? builders.ifStatement(332 node.test,333 consequent,334 builders.blockStatement(alternate)335 )336 : builders.ifStatement(node.test, consequent);337 body[index] = newIf;338 } else {339 body.push(node);340 }341 } else {342 body.push(node);343 }344 }345 path.node.body = body;346 this.traverse(path);347 },348 };349 },...
no-sizzle.js
Source:no-sizzle.js
1'use strict'2const rule = require('../rules/no-sizzle')3const RuleTester = require('eslint').RuleTester4const error = 'Selector extensions are not allowed'5const ruleTester = new RuleTester()6ruleTester.run('no-sizzle', rule, {7 valid: [8 'find(":input")',9 'div.find(":input")',10 '$(this).on("custom:input")',11 '$(this).on("custom:selected")',12 '$(this).find(".selected")',13 '$(this).find(":checked")',14 '$(this).find("input")',15 '$(this).find(":first-child")',16 '$(this).find(":first-child div")',17 '$(this).find(":last-child")',18 '$(this).find(":last-child div")',19 '$(this).find($())',20 '$(this).find(function() {})',21 '$(this).find()',22 '$(function() {})'23 ],24 invalid: [25 {26 code: '$(":animated")',27 errors: [{message: error, type: 'CallExpression'}]28 },29 {30 code: '$(":button")',31 errors: [{message: error, type: 'CallExpression'}]32 },33 {34 code: '$(":checkbox")',35 errors: [{message: error, type: 'CallExpression'}]36 },37 {38 code: '$(":eq")',39 errors: [{message: error, type: 'CallExpression'}]40 },41 {42 code: '$(":even")',43 errors: [{message: error, type: 'CallExpression'}]44 },45 {46 code: '$(":file")',47 errors: [{message: error, type: 'CallExpression'}]48 },49 {50 code: '$(":first")',51 errors: [{message: error, type: 'CallExpression'}]52 },53 {54 code: '$(":gt")',55 errors: [{message: error, type: 'CallExpression'}]56 },57 {58 code: '$(":has")',59 errors: [{message: error, type: 'CallExpression'}]60 },61 {62 code: '$(":header")',63 errors: [{message: error, type: 'CallExpression'}]64 },65 {66 code: '$(":hidden")',67 errors: [{message: error, type: 'CallExpression'}]68 },69 {70 code: '$(":image")',71 errors: [{message: error, type: 'CallExpression'}]72 },73 {74 code: '$(":input")',75 errors: [{message: error, type: 'CallExpression'}]76 },77 {78 code: '$(":last")',79 errors: [{message: error, type: 'CallExpression'}]80 },81 {82 code: '$(":lt")',83 errors: [{message: error, type: 'CallExpression'}]84 },85 {86 code: '$(":odd")',87 errors: [{message: error, type: 'CallExpression'}]88 },89 {90 code: '$(":parent")',91 errors: [{message: error, type: 'CallExpression'}]92 },93 {94 code: '$(":password")',95 errors: [{message: error, type: 'CallExpression'}]96 },97 {98 code: '$(":radio")',99 errors: [{message: error, type: 'CallExpression'}]100 },101 {102 code: '$(":reset")',103 errors: [{message: error, type: 'CallExpression'}]104 },105 {106 code: '$(":selected")',107 errors: [{message: error, type: 'CallExpression'}]108 },109 {110 code: '$(":submit")',111 errors: [{message: error, type: 'CallExpression'}]112 },113 {114 code: '$(":text")',115 errors: [{message: error, type: 'CallExpression'}]116 },117 {118 code: '$(":visible")',119 errors: [{message: error, type: 'CallExpression'}]120 },121 {122 code: '$("div").children(":first")',123 errors: [{message: error, type: 'CallExpression'}]124 },125 {126 code: '$("div").closest(":first")',127 errors: [{message: error, type: 'CallExpression'}]128 },129 {130 code: '$("div").filter(":first")',131 errors: [{message: error, type: 'CallExpression'}]132 },133 {134 code: '$("div").find(":first")',135 errors: [{message: error, type: 'CallExpression'}]136 },137 {138 code: '$("div").has(":first")',139 errors: [{message: error, type: 'CallExpression'}]140 },141 {142 code: '$("div").is(":first")',143 errors: [{message: error, type: 'CallExpression'}]144 },145 {146 code: '$("div").next(":first")',147 errors: [{message: error, type: 'CallExpression'}]148 },149 {150 code: '$("div").nextAll(":first")',151 errors: [{message: error, type: 'CallExpression'}]152 },153 {154 code: '$("div").nextUntil(":first")',155 errors: [{message: error, type: 'CallExpression'}]156 },157 {158 code: '$("div").not(":first")',159 errors: [{message: error, type: 'CallExpression'}]160 },161 {162 code: '$("div").parent(":first")',163 errors: [{message: error, type: 'CallExpression'}]164 },165 {166 code: '$("div").parents(":first")',167 errors: [{message: error, type: 'CallExpression'}]168 },169 {170 code: '$("div").parentsUntil(":first")',171 errors: [{message: error, type: 'CallExpression'}]172 },173 {174 code: '$("div").prev(":first")',175 errors: [{message: error, type: 'CallExpression'}]176 },177 {178 code: '$("div").prevAll(":first")',179 errors: [{message: error, type: 'CallExpression'}]180 },181 {182 code: '$("div").prevUntil(":first")',183 errors: [{message: error, type: 'CallExpression'}]184 },185 {186 code: '$("div").siblings(":first")',187 errors: [{message: error, type: 'CallExpression'}]188 },189 {190 code: '$("div:first")',191 errors: [{message: error, type: 'CallExpression'}]192 },193 {194 code: '$("div:first").find("p")',195 errors: [{message: error, type: 'CallExpression'}]196 },197 {198 code: '$("div").find("p:first").addClass("test").find("p")',199 errors: [{message: error, type: 'CallExpression'}]200 },201 {202 code: '$("div").find(":first")',203 errors: [{message: error, type: 'CallExpression'}]204 },205 {206 code: '$("div").find("div:animated")',207 errors: [{message: error, type: 'CallExpression'}]208 },209 {210 code: '$div.find("form input:checkbox")',211 errors: [{message: error, type: 'CallExpression'}]212 }213 ]...
Using AI Code Generation
1var esprima = require('esprima');2var escodegen = require('escodegen');3var fs = require('fs');4var code = fs.readFileSync('test.js', 'utf-8');5var ast = esprima.parse(code, {range: true});6var func = escodegen.generate(ast);7var func = escodegen.generate(ast, {8 format: {9 indent: {10 },11 },12 moz: {13 },14});15console.log(func);16var esprima = require('esprima');17var escodegen = require('escodegen');18var fs = require('fs');19var code = fs.readFileSync('test.js', 'utf-8');20var ast = esprima.parse(code, {range: true});21var func = escodegen.generate(ast);22var func = escodegen.generate(ast, {23 format: {24 indent: {25 },26 },27 moz: {28 },
Using AI Code Generation
1const {parse} = require("@babel/parser");2const traverse = require("@babel/traverse").default;3const generator = require("@babel/generator").default;4function square(n) {5 return n * n;6}7`;8const ast = parse(code);9traverse(ast, {10 CallExpression(path) {11 if(path.node.callee.name === 'square') {12 path.node.callee.name = 'multiply';13 }14 }15});16const {parse} = require("@babel/parser");17const traverse = require("@babel/traverse").default;18const generator = require("@babel/generator").default;19function square(n) {20 return n * n;21}22`;23const ast = parse(code);24traverse(ast, {25 CallExpression(path) {26 if(path.node.callee.name === 'square') {27 path.node.callee.name = 'multiply';28 }29 }30});31const {parse} = require("@babel/parser");32const traverse = require("@babel/traverse").default;33const generator = require("@babel/generator").default;34function square(n) {35 return n * n;36}37`;38const ast = parse(code);39traverse(ast, {40 CallExpression(path) {41 if(path.node.callee.name === 'square') {42 path.node.callee.name = 'multiply';43 }44 }45});46const {parse} = require("@babel/parser");47const traverse = require("@babel/traverse").default;48const generator = require("@babel/generator").default;49function square(n) {50 return n * n;51}52`;53const ast = parse(code);54traverse(ast, {
Using AI Code Generation
1var esprima = require("esprima");2var fs = require("fs");3var code = fs.readFileSync('test.js', 'utf8');4var ast = esprima.parse(code, { range: true });5var result = esprima.parse(code, { range: true });6var count = 0;7var count1 = 0;8var count2 = 0;9var count3 = 0;10var count4 = 0;11var count5 = 0;12var count6 = 0;13var count7 = 0;14var count8 = 0;15var count9 = 0;16var count10 = 0;17var count11 = 0;18var count12 = 0;19var count13 = 0;20var count14 = 0;21var count15 = 0;22var count16 = 0;23var count17 = 0;24var count18 = 0;25var count19 = 0;26var count20 = 0;27var count21 = 0;28var count22 = 0;29var count23 = 0;30var count24 = 0;31var count25 = 0;32var count26 = 0;33var count27 = 0;34var count28 = 0;35var count29 = 0;36var count30 = 0;37var count31 = 0;38var count32 = 0;39var count33 = 0;40var count34 = 0;41var count35 = 0;42var count36 = 0;43var count37 = 0;44var count38 = 0;45var count39 = 0;46var count40 = 0;47var count41 = 0;48var count42 = 0;49var count43 = 0;50var count44 = 0;51var count45 = 0;52var count46 = 0;53var count47 = 0;54var count48 = 0;55var count49 = 0;56var count50 = 0;57var count51 = 0;58var count52 = 0;59var count53 = 0;60var count54 = 0;61var count55 = 0;62var count56 = 0;63var count57 = 0;64var count58 = 0;65var count59 = 0;66var count60 = 0;67var count61 = 0;
Using AI Code Generation
1const { types } = require("@babel/core");2const { default: template } = require("@babel/template");3const t = types;4module.exports = function({ types: t }) {5 return {6 visitor: {7 CallExpression(path) {8 if (path.node.callee.name === "console") {9 path.remove();10 }11 },12 },13 };14};15{16 "./test.js", {17 }18}19const { types } = require("@babel/core");20const { default: template } = require("@babel/template");21const t = types;22module.exports = function({ types: t }) {23 return {24 visitor: {25 CallExpression(path) {26 if (path.node.callee.name === "console") {27 path.remove();28 }29 },30 },31 };32};33{34 "./test.js", {35 }36}37const { types } = require("@babel/core");38const { default: template } = require("@babel/template");39const t = types;40module.exports = function({ types: t }) {41 return {42 visitor: {43 CallExpression(path) {44 if (path.node.callee.name === "console") {45 path.remove();46 }47 },48 },49 };50};51{52 "./test.js", {53 }54}55const { types } = require("@babel/core");56const { default: template } = require("@babel/template");57const t = types;58module.exports = function({ types: t }) {59 return {60 visitor: {61 CallExpression(path) {62 if (path.node.callee.name === "console") {63 path.remove();64 }65 },66 },67 };68};
Using AI Code Generation
1let ast = jscodeshift(file.source);2ast.find(jscodeshift.CallExpression).forEach(path => {3 console.log(path.value);4});5let ast = jscodeshift(file.source);6ast.find(jscodeshift.CallExpression).forEach(path => {7 console.log(path.value);8});9let ast = jscodeshift(file.source);10ast.find(jscodeshift.CallExpression).forEach(path => {11 console.log(path.value);12});13let ast = jscodeshift(file.source);14ast.find(jscodeshift.CallExpression).forEach(path => {15 console.log(path.value);16});17let ast = jscodeshift(file.source);18ast.find(jscodeshift.CallExpression).forEach(path => {19 console.log(path.value);20});21let ast = jscodeshift(file.source);22ast.find(jscodeshift.CallExpression).forEach(path => {23 console.log(path.value);24});25let ast = jscodeshift(file.source);26ast.find(jscodeshift.CallExpression).forEach(path => {27 console.log(path.value);28});29let ast = jscodeshift(file.source);30ast.find(jscodeshift.CallExpression).forEach(path => {31 console.log(path.value);32});33let ast = jscodeshift(file.source);34ast.find(jscodeshift.CallExpression).forEach(path => {35 console.log(path.value);36});
Using AI Code Generation
1const { parse } = require('path/to/acorn');2const { generate } = require('path/to/estree-generator');3const code = 'console.log("Hello World!")';4const ast = parse(code);5const generatedCode = generate(ast);6console.log(generatedCode);
Using AI Code Generation
1var esprima = require('esprima');2var escodegen = require('escodegen');3var estraverse = require('estraverse');4var fs = require('fs');5var code = fs.readFileSync('test.js', 'utf-8');6var ast = esprima.parse(code, {loc: true});7var code = escodegen.generate(ast);8var funcName = "";9var funcName2 = "";10var funcName3 = "";11var funcName4 = "";12var funcName5 = "";13var funcName6 = "";14var funcName7 = "";15var funcName8 = "";16var funcName9 = "";17var funcName10 = "";18var funcName11 = "";19var funcName12 = "";20var funcName13 = "";21var funcName14 = "";22var funcName15 = "";23var funcName16 = "";24var funcName17 = "";25var funcName18 = "";26var funcName19 = "";27var funcName20 = "";28var funcName21 = "";29var funcName22 = "";30var funcName23 = "";31var funcName24 = "";32var funcName25 = "";33var funcName26 = "";34var funcName27 = "";35var funcName28 = "";36var funcName29 = "";37var funcName30 = "";38var funcName31 = "";39var funcName32 = "";40var funcName33 = "";41var funcName34 = "";42var funcName35 = "";43var funcName36 = "";44var funcName37 = "";45var funcName38 = "";46var funcName39 = "";47var funcName40 = "";48var funcName41 = "";49var funcName42 = "";50var funcName43 = "";51var funcName44 = "";52var funcName45 = "";53var funcName46 = "";54var funcName47 = "";55var funcName48 = "";56var funcName49 = "";57var funcName50 = "";58var funcName51 = "";59var funcName52 = "";60var funcName53 = "";61var funcName54 = "";62var funcName55 = "";63var funcName56 = "";64var funcName57 = "";65var funcName58 = "";66var funcName59 = "";67var funcName60 = "";68var funcName61 = "";69var funcName62 = "";70var funcName63 = "";71var funcName64 = "";72var funcName65 = "";73var funcName66 = "";74var funcName67 = "";
Using AI Code Generation
1const jscodeshift = require('jscodeshift');2const jscodeshift = require('jscodeshift');3jscodeshift('function add(a, b) { return a + b; }')4 .find(jscodeshift.CallExpression)5 .forEach(path => {6 console.log(path.value.callee.name);7 });8const jscodeshift = require('jscodeshift');9const jscodeshift = require('jscodeshift');10jscodeshift('function add(a, b) { return a + b; }')11 .find(jscodeshift.CallExpression)12 .forEach(path => {13 console.log(path.value.callee.name);14 });15const jscodeshift = require('jscodeshift');16const jscodeshift = require('jscodeshift');17jscodeshift('function add(a, b) { return a + b; }')18 .find(jscodeshift.CallExpression)19 .forEach(path => {20 console.log(path.value.callee.name);21 });22const jscodeshift = require('jscodeshift');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!