Best JavaScript code snippet using cypress
jquery.trap.js
Source:jquery.trap.js
1/*!2Copyright (c) 2011, 2012 Julien Wajsberg <felash@gmail.com>3All rights reserved.4Official repository: https://github.com/julienw/jquery-trap-input5License is there: https://github.com/julienw/jquery-trap-input/blob/master/LICENSE6This is version 1.2.0.7*/8(function( $, undefined ){9/*10(this comment is after the first line of code so that uglifyjs removes it)11Redistribution and use in source and binary forms, with or without12modification, are permitted without condition.13Although that's not an obligation, I would appreciate that you provide a14link to the official repository.15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS16IS" AND ANY EXPRESS OR IMPLIED WARRANTIES ARE DISCLAIMED.17*/18/*jshint boss: true, bitwise: true, curly: true, expr: true, newcap: true, noarg: true, nonew: true, latedef: true, regexdash: true */19 var DATA_ISTRAPPING_KEY = "trap.isTrapping";20 function onkeypress(e) {21 if (e.keyCode === 9) {22 var goReverse = !!(e.shiftKey);23 if (processTab(this, e.target, goReverse)) {24 e.preventDefault();25 e.stopPropagation();26 }27 }28 }29 // will return true if we could process the tab event30 // otherwise, return false31 function processTab(container, elt, goReverse) {32 var $focussable = getFocusableElementsInContainer(container),33 curElt = elt,34 index, nextIndex, prevIndex, lastIndex;35 do {36 index = $focussable.index(curElt);37 nextIndex = index + 1;38 prevIndex = index - 1;39 lastIndex = $focussable.length - 1;40 switch(index) {41 case -1:42 return false; // that's strange, let the browser do its job43 case 0:44 prevIndex = lastIndex;45 break;46 case lastIndex:47 nextIndex = 0;48 break;49 }50 if (goReverse) {51 nextIndex = prevIndex;52 }53 curElt = $focussable.get(nextIndex);54 if (!curElt || curElt === elt) { return true; }55 try {56 curElt.focus();57 } catch(e) { // IE sometimes throws when an element is not visible58 return true;59 }60 } while ($focussable.length > 1 && elt === elt.ownerDocument.activeElement);61 return true;62 }63 function filterKeepSpeciallyFocusable() {64 return this.tabIndex > 0;65 }66 function filterKeepNormalElements() {67 return !this.tabIndex; // true if no tabIndex or tabIndex == 068 }69 function sortFocusable(a, b) {70 return (a.t - b.t) || (a.i - b.i);71 }72 function getFocusableElementsInContainer(container) {73 var $container = $(container);74 var result = [],75 cnt = 0;76 fixIndexSelector.enable && fixIndexSelector.enable();77 // leaving away command and details for now78 $container.find("a[href], link[href], [draggable=true], [contenteditable=true], :input:enabled, [tabindex=0]")79 .filter(":visible")80 .filter(filterKeepNormalElements)81 .each(function(i, val) {82 result.push({83 v: val, // value84 t: 0, // tabIndex85 i: cnt++ // index for stable sort86 });87 });88 $container89 .find("[tabindex]")90 .filter(":visible")91 .filter(filterKeepSpeciallyFocusable)92 .each(function(i, val) {93 result.push({94 v: val, // value95 t: val.tabIndex, // tabIndex96 i: cnt++ // index97 });98 });99 fixIndexSelector.disable && fixIndexSelector.disable();100 result = $.map(result.sort(sortFocusable), // needs stable sort101 function(val) {102 return val.v;103 }104 );105 return $(result);106 }107 function trap() {108 this.keydown(onkeypress);109 this.data(DATA_ISTRAPPING_KEY, true);110 return this;111 }112 function untrap() {113 this.unbind('keydown', onkeypress);114 this.removeData(DATA_ISTRAPPING_KEY);115 return this;116 }117 function isTrapping() {118 return !!this.data(DATA_ISTRAPPING_KEY);119 }120 $.fn.extend({121 trap: trap,122 untrap: untrap,123 isTrapping: isTrapping124 });125 // jQuery 1.6.x tabindex attr hooks management126 // this triggers problems for tabindex attribute127 // selectors in IE7-128 // see https://github.com/julienw/jquery-trap-input/issues/3129 var fixIndexSelector = {};130 if ($.find.find && $.find.attr !== $.attr) {131 // jQuery uses Sizzle (this is jQuery >= 1.3)132 // sizzle uses its own attribute handling (in jq 1.6.x and below)133 (function() {134 var tabindexKey = "tabindex";135 var sizzleAttrHandle = $.expr.attrHandle;136 // this function comes directly from jQuery 1.7.2 (propHooks.tabIndex.get)137 // we have to put it here if we want to support jQuery < 1.6 which138 // doesn't have an attrHooks object to reference.139 function getTabindexAttr(elem) {140 // elem.tabIndex doesn't always return the correct value when it hasn't been explicitly set141 // http://fluidproject.org/blog/2008/01/09/getting-setting-and-removing-tabindex-values-with-javascript/142 var attributeNode = elem.getAttributeNode(tabindexKey);143 return attributeNode && attributeNode.specified ?144 parseInt( attributeNode.value, 10 ) :145 undefined;146 }147 function fixSizzleAttrHook() {148 // in jQ <= 1.6.x, we add to Sizzle the attrHook from jQuery's attr method149 sizzleAttrHandle[tabindexKey] = sizzleAttrHandle.tabIndex = getTabindexAttr;150 }151 function unfixSizzleAttrHook() {152 delete sizzleAttrHandle[tabindexKey];153 delete sizzleAttrHandle.tabIndex;154 }155 fixIndexSelector = {156 enable: fixSizzleAttrHook,157 disable: unfixSizzleAttrHook158 };159 })();160 }...
utilities.js
Source:utilities.js
...14 // Set it again for good measure15 el.setAttribute( "checked", "checked" );16 el.setAttribute( "id", "id" );17 el.setAttribute( "value", "on" );18 strictEqual( Sizzle.attr( el, "nonexistent" ), null, "nonexistent" );19 strictEqual( Sizzle.attr( el, "id" ), "id", "existent" );20 strictEqual( Sizzle.attr( el, "value" ), "on", "value" );21 strictEqual( Sizzle.attr( el, "checked" ), "checked", "boolean" );22 strictEqual( Sizzle.attr( el, "href" ), null, "interpolation risk" );23 strictEqual( Sizzle.attr( el, "constructor" ), null,24 "Object.prototype property \"constructor\" (negative)" );25 strictEqual( Sizzle.attr( el, "watch" ), null,26 "Gecko Object.prototype property \"watch\" (negative)" );27 el.setAttribute( "constructor", "foo" );28 el.setAttribute( "watch", "bar" );29 strictEqual( Sizzle.attr( el, "constructor" ), "foo",30 "Object.prototype property \"constructor\"" );31 strictEqual( Sizzle.attr( el, "watch" ), "bar",32 "Gecko Object.prototype property \"watch\"" );33}34test("Sizzle.attr (HTML)", function() {35 testAttr();36});37test("Sizzle.attr (XML)", function() {38 testAttr( jQuery.parseXML("<root/>") );39});40test("Sizzle.contains", function() {41 expect( 16 );42 var container = document.getElementById("nonnodes"),43 element = container.firstChild,44 text = element.nextSibling,45 nonContained = container.nextSibling,...
Using AI Code Generation
1describe('My First Test', () => {2 it('Does not do much!', () => {3 expect(true).to.equal(true)4 })5 })6 describe('My First Test', () => {7 it('Does not do much!', () => {8 expect(true).to.equal(true)9 })10 })11 describe('My First Test', () => {12 it('Does not do much!', () => {13 expect(true).to.equal(true)14 })15 })16 describe('My First Test', () => {17 it('Does not do much!', () => {18 expect(true).to.equal(true)19 })20 })
Using AI Code Generation
1Cypress.Commands.add('attr', { prevSubject: true }, (subject, attribute) => {2 return cy.wrap(subject).invoke('attr', attribute)3})4Cypress.Commands.add('css', { prevSubject: true }, (subject, css) => {5 return cy.wrap(subject).invoke('css', css)6})7Cypress.Commands.add('data', { prevSubject: true }, (subject, data) => {8 return cy.wrap(subject).invoke('data', data)9})10Cypress.Commands.add('prop', { prevSubject: true }, (subject, prop) => {11 return cy.wrap(subject).invoke('prop', prop)12})13Cypress.Commands.add('val', { prevSubject: true }, (subject, val) => {14 return cy.wrap(subject).invoke('val', val)15})16Cypress.Commands.add('height', { prevSubject: true }, (subject, height) => {17 return cy.wrap(subject).invoke('height', height)18})19Cypress.Commands.add('width', { prevSubject: true }, (subject, width) => {20 return cy.wrap(subject).invoke('width', width)21})22Cypress.Commands.add('offset', { prevSubject: true }, (subject, offset) => {23 return cy.wrap(subject).invoke('offset', offset)24})25Cypress.Commands.add('position', { prevSubject: true }, (subject, position) => {26 return cy.wrap(subject).invoke('position', position)27})28Cypress.Commands.add('scrollTop', { prevSubject: true }, (subject, scrollTop) => {29 return cy.wrap(subject).invoke('scrollTop', scrollTop)30})
Using AI Code Generation
1export const getAttr = (selector, attribute) => cy.get(selector).invoke('attr', attribute);2export const getAttrFromIframe = (selector, attribute) => cy.get('iframe').iframe().get(selector).invoke('attr', attribute);3export const getAttrFromIframeByIndex = (index, selector, attribute) => cy.get('iframe').eq(index).iframe().get(selector).invoke('attr', attribute);4export const getAttrFromIframeByIndex2 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);5export const getAttrFromIframeByIndex3 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);6export const getAttrFromIframeByIndex4 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);7export const getAttrFromIframeByIndex5 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);8export const getAttrFromIframeByIndex6 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);9export const getAttrFromIframeByIndex7 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);10export const getAttrFromIframeByIndex8 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);11export const getAttrFromIframeByIndex9 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);12export const getAttrFromIframeByIndex10 = (index, selector, attribute) => cy.get(selector).eq(index).invoke('attr', attribute);
Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.
You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.
Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.
Get 100 minutes of automation test minutes FREE!!