Best JavaScript code snippet using cypress
detect.js
Source:detect.js
1"use strict";2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {3 if (k2 === undefined) k2 = k;4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });5}) : (function(o, m, k, k2) {6 if (k2 === undefined) k2 = k;7 o[k2] = m[k];8}));9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {10 Object.defineProperty(o, "default", { enumerable: true, value: v });11}) : function(o, v) {12 o["default"] = v;13});14var __importStar = (this && this.__importStar) || function (mod) {15 if (mod && mod.__esModule) return mod;16 var result = {};17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);18 __setModuleDefault(result, mod);19 return result;20};21var __importDefault = (this && this.__importDefault) || function (mod) {22 return (mod && mod.__esModule) ? mod : { "default": mod };23};24Object.defineProperty(exports, "__esModule", { value: true });25exports.detectByPath = exports.detect = exports.setMajorVersion = void 0;26const bluebird_1 = __importDefault(require("bluebird"));27const lodash_1 = __importStar(require("lodash"));28const os_1 = __importDefault(require("os"));29const browsers_1 = require("./browsers");30const darwinHelper = __importStar(require("./darwin"));31const util_1 = require("./darwin/util");32const errors_1 = require("./errors");33const linuxHelper = __importStar(require("./linux"));34const log_1 = require("./log");35const windowsHelper = __importStar(require("./windows"));36const setMajorVersion = (browser) => {37 const majorVersion = parseInt(browser.version.split('.')[0]) || browser.version;38 const unsupportedVersion = browser.minSupportedVersion && majorVersion < browser.minSupportedVersion;39 (0, log_1.log)('browser %s version %s major version %s', browser.name, browser.version, majorVersion, unsupportedVersion);40 const foundBrowser = (0, lodash_1.extend)({}, browser, { majorVersion });41 if (unsupportedVersion) {42 foundBrowser.unsupportedVersion = true;43 foundBrowser.warning = `Cypress does not support running ${browser.displayName} version ${majorVersion}. To use ${browser.displayName} with Cypress, install a version of ${browser.displayName} newer than or equal to ${browser.minSupportedVersion}.`;44 }45 return foundBrowser;46};47exports.setMajorVersion = setMajorVersion;48const helpers = {49 darwin: darwinHelper,50 linux: linuxHelper,51 win32: windowsHelper,52};53function getHelper(platform) {54 return helpers[platform || os_1.default.platform()];55}56function lookup(platform, browser) {57 (0, log_1.log)('looking up %s on %s platform', browser.name, platform);58 const helper = getHelper(platform);59 if (!helper) {60 throw new Error(`Cannot lookup browser ${browser.name} on ${platform}`);61 }62 return helper.detect(browser);63}64/**65 * Try to detect a single browser definition, which may dispatch multiple `checkOneBrowser` calls,66 * one for each binary. If Windows is detected, only one `checkOneBrowser` will be called, because67 * we don't use the `binary` field on Windows.68 */69function checkBrowser(browser) {70 if (Array.isArray(browser.binary) && os_1.default.platform() !== 'win32') {71 return bluebird_1.default.map(browser.binary, (binary) => {72 return checkOneBrowser((0, lodash_1.extend)({}, browser, { binary }));73 });74 }75 return bluebird_1.default.map([browser], checkOneBrowser);76}77function checkOneBrowser(browser) {78 const platform = os_1.default.platform();79 const pickBrowserProps = [80 'name',81 'family',82 'channel',83 'displayName',84 'type',85 'version',86 'path',87 'profilePath',88 'custom',89 'warning',90 'info',91 'minSupportedVersion',92 'unsupportedVersion',93 ];94 const logBrowser = (props) => {95 (0, log_1.log)('setting major version for %j', props);96 };97 const failed = (err) => {98 if (err.notInstalled) {99 (0, log_1.log)('browser %s not installed', browser.name);100 return false;101 }102 throw err;103 };104 (0, log_1.log)('checking one browser %s', browser.name);105 return lookup(platform, browser)106 .then((val) => (Object.assign(Object.assign({}, browser), val)))107 .then((val) => lodash_1.default.pick(val, pickBrowserProps))108 .then((val) => {109 logBrowser(val);110 return val;111 })112 .then((browser) => (0, exports.setMajorVersion)(browser))113 .catch(failed);114}115/** returns list of detected browsers */116const detect = (goalBrowsers, useDarwinWorkaround = true) => {117 // we can detect same browser under different aliases118 // tell them apart by the name and the version property119 if (!goalBrowsers) {120 goalBrowsers = browsers_1.browsers;121 }122 // BigSur (darwin 20.x) and Electron 12+ cause huge performance issues when123 // spawning child processes, which is the way we find browsers via execa.124 // The performance cost is multiplied by the number of binary variants of125 // each browser plus any fallback lookups we do.126 // The workaround gets around this by breaking out of the bundled Electron127 // Node.js and using the user's Node.js if possible. It only pays the cost128 // of spawning a single child process instead of multiple. If this fails,129 // we fall back to to the slower, default method130 // https://github.com/cypress-io/cypress/issues/17773131 if (useDarwinWorkaround && (0, util_1.needsDarwinWorkaround)()) {132 (0, log_1.log)('using darwin detection workaround');133 if (log_1.log.enabled) {134 // eslint-disable-next-line no-console135 console.time('time taken detecting browsers (darwin workaround)');136 }137 return bluebird_1.default.resolve((0, util_1.darwinDetectionWorkaround)())138 .catch((err) => {139 (0, log_1.log)('darwin workaround failed, falling back to normal detection');140 (0, log_1.log)(err.stack);141 return (0, exports.detect)(goalBrowsers, false);142 })143 .finally(() => {144 if (log_1.log.enabled) {145 // eslint-disable-next-line no-console146 console.timeEnd('time taken detecting browsers (darwin workaround)');147 }148 });149 }150 const removeDuplicates = (val) => {151 return lodash_1.default.uniqBy(val, (browser) => {152 return `${browser.name}-${browser.version}`;153 });154 };155 const compactFalse = (browsers) => {156 return (0, lodash_1.compact)(browsers);157 };158 (0, log_1.log)('detecting if the following browsers are present %o', goalBrowsers);159 return bluebird_1.default.mapSeries(goalBrowsers, checkBrowser)160 .then((val) => lodash_1.default.flatten(val))161 .then(compactFalse)162 .then(removeDuplicates);163};164exports.detect = detect;165const detectByPath = (path, goalBrowsers) => {166 if (!goalBrowsers) {167 goalBrowsers = browsers_1.browsers;168 }169 const helper = getHelper();170 const detectBrowserByVersionString = (stdout) => {171 return (0, lodash_1.find)(goalBrowsers, (goalBrowser) => {172 return goalBrowser.versionRegex.test(stdout);173 });174 };175 const detectBrowserFromKey = (browserKey) => {176 return (0, lodash_1.find)(goalBrowsers, (goalBrowser) => {177 return (goalBrowser.name === browserKey ||178 goalBrowser.displayName === browserKey ||179 goalBrowser.binary.indexOf(browserKey) > -1);180 });181 };182 const setCustomBrowserData = (browser, path, versionStr) => {183 const version = helper.getVersionNumber(versionStr, browser);184 let parsedBrowser = (0, lodash_1.extend)({}, browser, {185 name: browser.name,186 displayName: `Custom ${browser.displayName}`,187 info: `Loaded from ${path}`,188 custom: true,189 path,190 version,191 });192 return (0, exports.setMajorVersion)(parsedBrowser);193 };194 const pathData = helper.getPathData(path);195 return helper.getVersionString(pathData.path)196 .then((version) => {197 let browser;198 if (pathData.browserKey) {199 browser = detectBrowserFromKey(pathData.browserKey);200 }201 if (!browser) {202 browser = detectBrowserByVersionString(version);203 }204 if (!browser) {205 throw (0, errors_1.notDetectedAtPathErr)(`Unable to find browser with path ${path}`);206 }207 return setCustomBrowserData(browser, pathData.path, version);208 })209 .catch((err) => {210 if (err.notDetectedAtPath) {211 throw err;212 }213 throw (0, errors_1.notDetectedAtPathErr)(err.message);214 });215};...
util.js
Source:util.js
...104 };105 return tryMdFind().catch(tryFullApplicationFind);106}107exports.findApp = findApp;108function needsDarwinWorkaround() {109 return os.platform() === 'darwin' && semver.gte(os.release(), '20.0.0');110}111exports.needsDarwinWorkaround = needsDarwinWorkaround;112function darwinDetectionWorkaround() {113 return __awaiter(this, void 0, void 0, function* () {114 const nodePath = yield findSystemNode.findNodeInFullPath();115 let args = ['./detection-workaround.js'];116 if (process.env.CYPRESS_INTERNAL_ENV === 'development') {117 args = ['-r', '@packages/ts/register.js', './detection-workaround.ts'];118 }119 const { stdout } = yield utils_1.utils.execa(nodePath, args, { cwd: __dirname });120 return JSON.parse(stdout);121 });122}...
Using AI Code Generation
1const { needsDarwinWorkaround } = Cypress.Browser;2const { isBrowser } = Cypress.Browser;3const { isBrowser } = Cypress.Browser;4const { isBrowser } = Cypress.Browser;5const { isBrowser } = Cypress.Browser;6const { isBrowser } = Cypress.Browser;7const { isBrowser } = Cypress.Browser;8const { isBrowser } = Cypress.Browser;9const { isBrowser } = Cypress.Browser;10const { isBrowser } = Cypress.Browser;11const { isBrowser } = Cypress.Browser;12const { isBrowser } = Cypress.Browser;13const { isBrowser } = Cypress.Browser;14const { isBrowser } = Cypress.Browser;15const { isBrowser } = Cypress.Browser;16const { isBrowser } = Cypress.Browser;
Using AI Code Generation
1const cypress = require('cypress')2const { execSync } = require('child_process')3const runTests = () => {4 execSync('cypress run', { stdio: 'inherit' })5}6if (needsDarwinWorkaround()) {
Using AI Code Generation
1const browser = Cypress.Browser.needsDarwinWorkaround()2console.log('browser', browser)3const browser = Cypress.Browser.isElectron()4console.log('browser', browser)5const browser = Cypress.Browser.isChrome()6console.log('browser', browser)7const browser = Cypress.Browser.isFirefox()8console.log('browser', browser)9const browser = Cypress.Browser.isEdge()10console.log('browser', browser)11const browser = Cypress.Browser.isIE()12console.log('browser', browser)13const browser = Cypress.Browser.isElectron()14console.log('browser', browser)15const browser = Cypress.Browser.isChrome()16console.log('browser', browser)17const browser = Cypress.Browser.isFirefox()18console.log('browser', browser)19const browser = Cypress.Browser.isEdge()20console.log('browser', browser)21const browser = Cypress.Browser.isIE()22console.log('browser', browser)
Using AI Code Generation
1Cypress.Browser.needsDarwinWorkaround('firefox')2### `Cypress.Browser.needsWin32Workaround(browserName)`3Cypress.Browser.needsWin32Workaround('chrome')4### `Cypress.Browser.needsXvfb(browserName)`5Cypress.Browser.needsXvfb('electron')6### `Cypress.Browser.isHeaded(browserName)`7Cypress.Browser.isHeaded('chrome')8### `Cypress.Browser.isHeadless(browserName)`9Cypress.Browser.isHeadless('electron')10### `Cypress.Browser.isBrowser(browserName)`11Cypress.Browser.isBrowser('chrome')12### `Cypress.Browser.isChromium(browserName)`13Cypress.Browser.isChromium('chrome')14### `Cypress.Browser.isFirefox(browserName)`15Cypress.Browser.isFirefox('firefox')
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!!