Best JavaScript code snippet using cypress
project.js
Source:project.js
...440 const replacer = (match) => match.replace('//', '/')441 return [442 browserUrl,443 '#/tests',444 escapeFilenameInUrl(specUrl),445 ].join('/').replace(multipleForwardSlashesRe, replacer)446 }447 scaffold (cfg) {448 debug('scaffolding project %s', this.projectRoot)449 const scaffolds = []450 const push = scaffolds.push.bind(scaffolds)451 // TODO: we are currently always scaffolding support452 // even when headlessly - this is due to a major breaking453 // change of 0.18.0454 // we can later force this not to always happen when most455 // of our users go beyond 0.18.0456 //457 // ensure support dir is created458 // and example support file if dir doesnt exist...
project_utils.js
Source:project_utils.js
1"use strict";2Object.defineProperty(exports, "__esModule", { value: true });3exports.getDefaultConfigFilePath = exports.checkSupportFile = exports.getSpecUrl = void 0;4const tslib_1 = require("tslib");5const debug_1 = (0, tslib_1.__importDefault)(require("debug"));6const path_1 = (0, tslib_1.__importDefault)(require("path"));7const settings = (0, tslib_1.__importStar)(require("./util/settings"));8const errors_1 = (0, tslib_1.__importDefault)(require("./errors"));9const fs_1 = require("./util/fs");10const escape_filename_1 = require("./util/escape_filename");11const configFiles_1 = require("./configFiles");12const debug = (0, debug_1.default)('cypress:server:project_utils');13const multipleForwardSlashesRe = /[^:\/\/](\/{2,})/g;14const backSlashesRe = /\\/g;15const normalizeSpecUrl = (browserUrl, specUrl) => {16 const replacer = (match) => match.replace('//', '/');17 return [18 browserUrl,19 '#/tests',20 (0, escape_filename_1.escapeFilenameInUrl)(specUrl),21 ].join('/')22 .replace(multipleForwardSlashesRe, replacer);23};24const getPrefixedPathToSpec = ({ integrationFolder, componentFolder, projectRoot, type, pathToSpec, }) => {25 type !== null && type !== void 0 ? type : (type = 'integration');26 // for now hard code the 'type' as integration27 // but in the future accept something different here28 // strip out the integration folder and prepend with "/"29 // example:30 //31 // /Users/bmann/Dev/cypress-app/.projects/cypress/integration32 // /Users/bmann/Dev/cypress-app/.projects/cypress/integration/foo.js33 //34 // becomes /integration/foo.js35 const folderToUse = type === 'integration' ? integrationFolder : componentFolder;36 // To avoid having invalid urls from containing backslashes,37 // we normalize specUrls to posix by replacing backslash by slash38 // Indeed, path.realtive will return something different on windows39 // than on posix systems which can lead to problems40 const url = `/${path_1.default.join(type, path_1.default.relative(folderToUse, path_1.default.resolve(projectRoot, pathToSpec))).replace(backSlashesRe, '/')}`;41 debug('prefixed path for spec %o', { pathToSpec, type, url });42 return url;43};44const getSpecUrl = ({ absoluteSpecPath, specType, browserUrl, integrationFolder, componentFolder, projectRoot, }) => {45 specType !== null && specType !== void 0 ? specType : (specType = 'integration');46 browserUrl !== null && browserUrl !== void 0 ? browserUrl : (browserUrl = '');47 debug('get spec url: %s for spec type %s', absoluteSpecPath, specType);48 // if we don't have a absoluteSpecPath or its __all49 if (!absoluteSpecPath || (absoluteSpecPath === '__all')) {50 const url = normalizeSpecUrl(browserUrl, '/__all');51 debug('returning url to run all specs: %s', url);52 return url;53 }54 // TODO:55 // to handle both unit + integration tests we need56 // to figure out (based on the config) where this absoluteSpecPath57 // lives. does it live in the integrationFolder or58 // the unit folder?59 // once we determine that we can then prefix it correctly60 // with either integration or unit61 const prefixedPath = getPrefixedPathToSpec({62 integrationFolder,63 componentFolder,64 projectRoot,65 pathToSpec: absoluteSpecPath,66 type: specType,67 });68 const url = normalizeSpecUrl(browserUrl, prefixedPath);69 debug('return path to spec %o', { specType, absoluteSpecPath, prefixedPath, url });70 return url;71};72exports.getSpecUrl = getSpecUrl;73const checkSupportFile = ({ supportFile, configFile, }) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {74 if (supportFile && typeof supportFile === 'string') {75 const found = yield fs_1.fs.pathExists(supportFile);76 if (!found) {77 errors_1.default.throw('SUPPORT_FILE_NOT_FOUND', supportFile, settings.configFile({ configFile }));78 }79 }80 return;81});82exports.checkSupportFile = checkSupportFile;83function getDefaultConfigFilePath(projectRoot, returnDefaultValueIfNotFound = true) {84 return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {85 const filesInProjectDir = yield fs_1.fs.readdir(projectRoot);86 const foundConfigFiles = configFiles_1.CYPRESS_CONFIG_FILES.filter((file) => filesInProjectDir.includes(file));87 // if we only found one default file, it is the one88 if (foundConfigFiles.length === 1) {89 return foundConfigFiles[0];90 }91 // if we found more than one, throw a language conflict92 if (foundConfigFiles.length > 1) {93 throw errors_1.default.throw('CONFIG_FILES_LANGUAGE_CONFLICT', projectRoot, ...foundConfigFiles);94 }95 if (returnDefaultValueIfNotFound) {96 // Default is to create a new `cypress.json` file if one does not exist.97 return configFiles_1.CYPRESS_CONFIG_FILES[0];98 }99 throw errors_1.default.get('NO_DEFAULT_CONFIG_FILE_FOUND', projectRoot);100 });101}...
files.js
Source:files.js
...25 return this.getSpecs(test, config).then((function(_this) {26 return function(specs) {27 specs = specs.map(function(fileName) {28 fileName = fileName.replace(SPEC_URL_PREFIX, "__CYPRESS_SPEC_URL_PREFIX__");29 return escapeFilenameInUrl(fileName).replace("__CYPRESS_SPEC_URL_PREFIX__", SPEC_URL_PREFIX);30 });31 return _this.getJavascripts(config).then(function(js) {32 return res.render(iframePath, {33 title: _this.getTitle(test),34 domain: getRemoteState().domainName,35 javascripts: js,36 specs: specs37 });38 });39 };40 })(this));41 },42 getSpecs: function(spec, config) {43 var convertSpecPath, getSpecs;...
escape_filename.js
Source:escape_filename.js
...4const ampersandRe = /&/g;5const percentRe = /%/g;6const questionRe = /\?/g;7const plusRe = /\+/g;8function escapeFilenameInUrl(url) {9 // escape valid file name characters that cannot be used in URL10 return url11 .replace(percentRe, '%25') // %12 .replace(ampersandRe, '%26') // &13 .replace(questionRe, '%3F') // ? -> it's only valid in Linux14 .replace(plusRe, '%2B'); // + https://github.com/cypress-io/cypress/issues/590915}...
Using AI Code Generation
1import escapeFilenameInUrl from 'cypress-downloadfile/lib/escapeFilenameInUrl';2 headers: {3 },4 onBeforeSave(xhr, url, data) {5 data = escapeFilenameInUrl(data, url);6 return data;7 },8});9cy.get('a[href*="dummy.pdf"]').should('have.attr', 'href').and('include', 'dummy.pdf');10cy.get('a[href*="dummy.pdf"]').should('have.attr', 'href').and('include', 'dummy.pdf');
Using AI Code Generation
1Cypress.Commands.add('escapeFilenameInUrl', (filename) => {2 cy.log(`Escape filename ${filename}`);3 return cy.window().then((win) => {4 return win.escapeFilenameInUrl(filename);5 });6});7Cypress.Commands.add('escapeFilenameInUrl', (filename) => {8 cy.log(`Escape filename ${filename}`);9 return cy.window().then((win) => {10 return win.escapeFilenameInUrl(filename);11 });12});13import './commands'14describe('Test', () => {15 it('Test', () => {16 cy.get('input').type('test');17 cy.get('inp
Using AI Code Generation
1describe("Test escapeFilenameInUrl method of Cypress", function () {2 it("Test escapeFilenameInUrl method", function () {3 cy.get(".action-download")4 .should("have.attr", "href")5 .then((href) => {6 const escapedUrl = Cypress.escapeFilenameInUrl(href);7 cy.log(escapedUrl);8 cy.request(escapedUrl).then((response) => {9 expect(response.status).to.eq(200);10 });11 });12 });13});14Cypress.escapeFilenameInUrl(url)
Using AI Code Generation
1describe('Test Escape Filename In Url', () => {2 it('Escape Filename In Url', () => {3 cy.get('input[name="q"]').type('Cypress');4 cy.get('input[name="btnK"]').click();5 cy.url().should('include', 'Cypress');6 cy.url().then((url) => {7 cy.log(url);8 cy.log(Cypress.platform);9 cy.log(Cypress._.escapeRegExp(url));10 cy.log(Cypress._.escapeRegExp(Cypress.platform));11 cy.log(Cypress._.escapeRegExp(Cypress.platform) + Cypress._.escapeRegExp(url));12 cy.log(Cypress._.escapeRegExp(Cypress.platform) + Cypress._.escapeRegExp(url).replace(/%2F/g, '/'));13 });14 });15});16describe('Test Escape Filename In Url', () => {17 it('Escape Filename In Url', () => {18 cy.get('input[name="q"]').type('Cypress');19 cy.get('input[name="btnK"]').click();20 cy.url().should('include', 'Cypress');21 cy.url().then((url) => {22 cy.log(url);23 cy.log(Cypress.platform);24 cy.log(Cypress._.escapeRegExp(url));25 cy.log(Cypress._.escapeRegExp(Cypress.platform));26 cy.log(Cypress._.escapeRegExp(Cypress.platform) + Cypress._.escapeRegExp(url));27 cy.log(Cypress._.escapeRegExp(Cypress.platform) + Cypress._.escapeRegExp(url).replace(/%2F/g, '/'));28 });29 });30});31Cypress.Commands.add('escapeFilenameInUrl', (url) => {32 cy.log(url);33 cy.log(Cypress.platform);34 cy.log(Cypress._.escapeRegExp(url));35 cy.log(Cypress._.escapeRegExp(Cypress.platform));36 cy.log(Cypress._.escapeRegExp(Cypress.platform) + Cypress._.escapeRegExp(url));37 cy.log(Cypress._.escapeRegExp(Cypress.platform) + Cypress._.escapeRegExp(url).replace(/%2F/g, '/'));38});39describe('Test Escape Filename In Url', () => {40 it('Escape Filename In Url', () => {
Using AI Code Generation
1Cypress.Commands.add('escapeFilenameInUrl', (filename) => {2 return filename.replace(/[^a-z0-9.]/g, (c) => {3 return `_${c.charCodeAt(0)}_`4 })5})6Cypress.Commands.add('unescapeFilenameInUrl', (filename) => {7 return filename.replace(/_([0-9]+)_/g, (c) => {8 return String.fromCharCode(c.slice(1, -1))9 })10})11describe('Test', () => {12 it('Test', () => {13 cy.get('input[type="file"]').attachFile('test.txt')14 cy.get('button').click()15 cy.url().then((url) => {16 const filename = Cypress._.last(url.split('/'))17 cy.log(filename)
Using AI Code Generation
1Cypress.Commands.add('escapeFilenameInUrl', (filename) => {2 return filename.replace(/%/g, '%25')3 .replace(/\?/g, '%3F')4 .replace(/#/g, '%23')5 .replace(/&/g, '%26')6 .replace(/=/g, '%3D')7 .replace(/\+/g, '%2B')8 .replace(/\$/g, '%24')9 .replace(/,/g, '%2C')10 .replace(/@/g, '%40')11 .replace(/:/g, '%3A')12 .replace(/;/g, '%3B')13 .replace(/\[/g, '%5B')14 .replace(/]/g, '%5D')15 .replace(/ /g, '%20')16 .replace(/"/g, '%22')17 .replace(/'/g, '%27')18 .replace(/</g, '%3C')19 .replace(/>/g, '%3E')20 .replace(/\|/g, '%7C')21 .replace(/\\/g, '%5C')22 .replace(/\^/g, '%5E')23 .replace(/`/g, '%60')24 .replace(/{/g, '%7B')25 .replace(/}/g, '%7D')26 .replace(/~/g, '%7E')
Using AI Code Generation
1const escapeFilenameInUrl = Cypress.escapeFilenameInUrl;2const fileName = 'test-file.txt';3const url = escapeFilenameInUrl(fileName);4console.log(url);5Cypress.Commands.add('escapeFilenameInUrl', (filename) => {6 return filename.replace(/%/g, '%25')7 .replace(/#/g, '%23')8 .replace(/\?/g, '%3F')9 .replace(/&/g, '%26')10 .replace(/=/g, '%3D')11 .replace(/:/g, '%3A')12 .replace(/\+/g, '%2B')13 .replace(/@/g, '%40')14 .replace(/\$/g, '%24')15 .replace(/,/g, '%2C')16 .replace(/;/g, '%3B')17 .replace(/</g, '%3C')18 .replace(/>/g, '%3E')19 .replace(/\\/g, '%5C')20 .replace(/\^/g, '%5E')21 .replace(/\|/g, '%7C')22 .replace(/`/g, '%60')23 .replace(/"/g, '%22')24 .replace(/'/g, '%27')25 .replace(/\[/g, '%5B')26 .replace(/\]/g, '%5D')27 .replace(/{/g, '%7B')28 .replace(/}/g, '%7D')29 .replace(/ /g, '%20');30});31import './commands';32module.exports = (on, config) => {33}34{35 "env": {36 }37}38{
Using AI Code Generation
1const path = require('path');2const filePath = path.join(__dirname, 'test.csv');3cy.get('input[type=file]').attachFile(filePath);4const filePath = 'test.csv';5cy.get('input[type=file]').attachFile(filePath);6const filePath = 'test.csv';7cy.get('input[type=file]').attachFile(filePath, { subjectType: 'drag-n-drop' });8const filePath = 'test.csv';9cy.get('input[type=file]').attachFile(filePath, { subjectType: 'input' });10const filePath = 'test.csv';11cy.get('input[type=file]').attachFile(filePath, { subjectType: 'drag-n-drop' });12const filePath = 'test.csv';13cy.get('input[type=file]').attachFile(filePath, { subjectType: 'input' });14const filePath = 'test.csv';
Using AI Code Generation
1 .get('a[href*="file.pdf"]')2 .then(function($a) {3 const href = $a.prop('href')4 .request({5 .escapeFilenameInUrl(href),6 })7 .then((response) => {8 expect(response.status).to.eq(200)9 expect(response.headers['content-type']).to.eq('application/pdf')10 expect(response.headers['content-disposition']).to.eq('attachment; filename="file.pdf"')11 expect(response.body.byteLength).to.eq(123456)12 })13 })14describe('My First Test', function() {15 it('Does not do much!', function() {16 .get('a[href*="file.pdf"]')17 .then(function($a) {18 const href = $a.prop('href')19 .downloadFile(href, 'GET', { encoding: null, followRedirect: false })20 .then((response) => {21 expect(response.status).to.eq(200)22 expect(response.headers['content-type']).to.eq('application/pdf')23 expect(response.headers['content-disposition']).to.eq('attachment; filename="file.pdf"')24 expect(response.body.byteLength).to.eq(123456)25 })26 })27 })28})29describe('My First Test', function() {30 it('Does not do much!', function() {31 .get('a[href*="file.pdf"]')32 .then(function($a) {33 const href = $a.prop('href')34 .downloadFile(href, 'GET', { encoding: null, followRedirect: false })35 .then((response) => {36 expect(response.status).to.eq(200)37 expect(response.headers['content-type']).to.eq('application/pdf')38 expect(response.headers['content-disposition']).to.eq('attachment; filename="file.pdf"')
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!!