Best JavaScript code snippet using cypress
BaseContract.js
Source:BaseContract.js
...138 debug(error.stack);139 return [2 /*return*/, reject(error)];140 }141 else {142 contractBinary = BaseContract.loadBinaryFromFile(this.compilerOutputFolder, this.contractName);143 }144 }145 this.contractOwner = signer;146 transactionData = ethers_1.Contract.getDeployTransaction.apply(ethers_1.Contract, [contractBinary, this.abiInterface].concat(contractConstructorParams));147 debug("Deploy contract data: " + transactionData.data);148 // need to async await so the promiEvent can be passed into the processTransaction function149 return [4 /*yield*/, Promise.resolve()];150 case 1:151 // need to async await so the promiEvent can be passed into the processTransaction function152 _a.sent();153 return [4 /*yield*/, this.processTransaction(signer, description, transactionData, sendOptions, promiEvent)];154 case 2:155 transactionReceipt = _a.sent();156 this.contract = new ethers_1.Contract(transactionReceipt.contractAddress, this.abiInterface, signer);...
client-certificates.js
Source:client-certificates.js
...172 if (item.ca) {173 item.ca.forEach((ca) => {174 if (ca) {175 debug(`loading CA cert from '${ca}'`);176 const caRaw = loadBinaryFromFile(ca);177 try {178 pki.certificateFromPem(caRaw);179 }180 catch (error) {181 throw new Error(`Cannot parse CA cert: ${error.message}`);182 }183 urlClientCertificates.clientCertificates.ca.push(caRaw);184 }185 });186 }187 if (!item.certs || item.certs.length === 0) {188 throw new Error('Either PEM or PFX must be supplied');189 }190 item.certs.forEach((cert) => {191 if (!cert || (!cert.cert && !cert.pfx)) {192 throw new Error('Either PEM or PFX must be supplied');193 }194 if (cert.cert) {195 if (!cert.key) {196 throw new Error(`No PEM key defined for cert: ${cert.cert}`);197 }198 debug(`loading PEM cert information from '${JSON.stringify(cert)}'`);199 debug(`loading PEM cert from '${cert.cert}'`);200 const pemRaw = loadBinaryFromFile(cert.cert);201 let pemParsed = undefined;202 try {203 pemParsed = pki.certificateFromPem(pemRaw);204 }205 catch (error) {206 throw new Error(`Cannot parse PEM cert: ${error.message}`);207 }208 urlClientCertificates.clientCertificates.cert.push(pemRaw);209 let passphrase = undefined;210 if (cert.passphrase) {211 debug(`loading PEM passphrase from '${cert.passphrase}'`);212 passphrase = loadTextFromFile(cert.passphrase);213 }214 debug(`loading PEM key from '${cert.key}'`);215 const pemKeyRaw = loadBinaryFromFile(cert.key);216 try {217 if (passphrase) {218 if (!pki.decryptRsaPrivateKey(pemKeyRaw, passphrase)) {219 throw new Error('Cannot decrypt PEM key with supplied passphrase (check the passphrase file content and that it doesn\'t have unexpected whitespace at the end)');220 }221 }222 else {223 if (!pki.privateKeyFromPem(pemKeyRaw)) {224 throw new Error('Cannot load PEM key');225 }226 }227 }228 catch (error) {229 throw new Error(`Cannot parse PEM key: ${error.message}`);230 }231 urlClientCertificates.clientCertificates.key.push(new PemKey(pemKeyRaw, passphrase));232 const subject = extractSubjectFromPem(pemParsed);233 urlClientCertificates.addSubject(subject);234 debug(`loaded client PEM certificate: ${subject} for url: ${urlClientCertificates.url}`);235 }236 if (cert.pfx) {237 debug(`loading PFX cert information from '${JSON.stringify(cert)}'`);238 let passphrase = undefined;239 if (cert.passphrase) {240 debug(`loading PFX passphrase from '${cert.passphrase}'`);241 passphrase = loadTextFromFile(cert.passphrase);242 }243 debug(`loading PFX cert from '${cert.pfx}'`);244 const pfxRaw = loadBinaryFromFile(cert.pfx);245 const pfxParsed = loadPfx(pfxRaw, passphrase);246 urlClientCertificates.clientCertificates.pfx.push(new PfxCertificate(pfxRaw, passphrase));247 const subject = extractSubjectFromPfx(pfxParsed);248 urlClientCertificates.addSubject(subject);249 debug(`loaded client PFX certificate: ${subject} for url: ${urlClientCertificates.url}`);250 }251 });252 agent_1.clientCertificateStore.addClientCertificatesForUrl(urlClientCertificates);253 index++;254 });255 debug(`loaded client certificates for ${agent_1.clientCertificateStore.getCertCount()} URL(s)`);256 }257 }258 catch (e) {259 debug(`Failed to load client certificate for clientCertificates[${index}]: ${e.message} ${e.stack}`);260 throw new Error(`Failed to load client certificates for clientCertificates[${index}]: ${e.message}. For more debug details run Cypress with DEBUG=cypress:server:client-certificates*`);261 }262}263exports.loadClientCertificateConfig = loadClientCertificateConfig;264function loadBinaryFromFile(filepath) {265 debug(`loadCertificateFile: ${filepath}`);266 return fs_extra_1.default.readFileSync(filepath);267}268function loadTextFromFile(filepath) {269 debug(`loadPassphraseFile: ${filepath}`);270 return fs_extra_1.default.readFileSync(filepath, 'utf8').toString();271}272/**273 * Extract subject from supplied pem instance274 */275function extractSubjectFromPem(pem) {276 try {277 return pem.subject.attributes278 .map((attr) => [attr.shortName, attr.value].join('='))...
Using AI Code Generation
1Cypress.Commands.add('loadBinaryFromFile', (file) => {2 .fixture(file, 'binary')3 .then(Cypress.Blob.binaryStringToBlob)4})5cy.loadBinaryFromFile('test.txt').then((fileContent) => {6 cy.get('#file-input').attachFile({ fileContent, fileName: 'test.txt', mimeType: 'text/plain' })7})8cy.get('#file-input').then((fileInput) => {9 expect(fileInput[0].files[0].name).to.eq('test.txt')10})11cy.get('#file-input').then((fileInput) => {12 expect(fileInput[0].files[0].type).to.eq('text/plain')13})14cy.get('#file-input').then((fileInput) => {15 expect(fileInput[0].files[0].size).to.eq(10)16})17cy.get('#file-input').then((fileInput) => {18 expect(fileInput[0].files[0].lastModified).to.be.a('number')19})20cy.get('#file-input').then((fileInput) => {21 expect(fileInput[0].files[0].lastModifiedDate).to.be.a('date')22})23cy.get('#file-input').then((fileInput) => {24 expect(fileInput[0].files[0].webkitRelativePath).to.eq('')25})26cy.get('#file-input').then((fileInput) => {27 expect(fileInput[0].files[0].path).to.eq('')28})29cy.get('#file-input').then((fileInput) => {30 expect(fileInput[0].files[0].webkitGetAsEntry).to.eq(undefined)31})32cy.get('#file-input').then((fileInput) => {33 expect(file
Using AI Code Generation
1describe('loadBinaryFromFile', () => {2 it('loads binary file', () => {3 cy.loadBinaryFromFile('test.png').then((binary) => {4 expect(binary).to.be.instanceof(ArrayBuffer)5 })6 })7})8Cypress.Commands.add('loadBinaryFromFile', (fileName) => {9 return cy.readFile(fileName, 'base64').then(Cypress.Blob.base64StringToBlob).then(Cypress.Blob.blobToArrayBuffer)10})11declare namespace Cypress {12 interface Chainable {13 loadBinaryFromFile(fileName: string): Chainable<ArrayBuffer>14 }15}16{17}18module.exports = (on, config) => {19 require('cypress-plugin-snapshots/plugin')(on, config)20}21import './commands'22import './commands'23import './commands'24import './commands'25import './commands'
Using AI Code Generation
1Cypress.Blob.loadBinaryFromFile('path/to/file').then((binary) => {2})3The loadBinaryFromFile() method is available in the following versions of Cypress:4Cypress v3.0.0 (released 2018-09-13)5Cypress v3.1.0 (released 2018-10-09)6Cypress v3.2.0 (released 2018-11-08)7Cypress v3.3.0 (released 2018-12-05)8Cypress v3.4.0 (released 2019-01-03)9Cypress v3.5.0 (released 2019-02-01)10Cypress v3.6.0 (released 2019-03-01)11Cypress v3.7.0 (released 2019-04-02)12Cypress v4.0.0 (released 2019-05-02)13Cypress v4.1.0 (released 2019-06-04)14Cypress v4.2.0 (released 2019-07-02)15Cypress v4.3.0 (released 2019-08-06)16Cypress v4.4.0 (released 2019-09-03)17Cypress v4.5.0 (released 2019-10-02)18Cypress v4.6.0 (released 2019-11-06)19Cypress v4.7.0 (released 2019-12-04)20Cypress v4.8.0 (released 2020-01-02)21Cypress v4.9.0 (released 2020-02-06)22Cypress v4.10.0 (released 2020-03-05)23Cypress v4.11.0 (released 2020-04-02)24Cypress v4.12.0 (released 2020-05-06)25Cypress v4.13.0 (released 2020-06-04)26Cypress v4.14.0 (released 2020-07-02)27Cypress v4.15.0 (released 202
Using AI Code Generation
1Cypress.Commands.add('uploadFile', (fileName, fileType = ' ', selector) => {2 cy.get(selector).then(subject => {3 cy.fixture(fileName, 'base64').then(content => {4 cy.window().then(win => {5 const el = win.document.createElement('input')6 el.files = new win.File([content], fileName, { type: fileType })7 const blob = new win.Blob([content], { type: fileType })8 const testFile = new win.File([blob], fileName, { type: fileType })9 const dataTransfer = new win.DataTransfer()10 dataTransfer.items.add(testFile)11 cy.wrap(subject).trigger('change', { force: true })12 })13 })14 })15})16it('Upload file', () => {17 cy.uploadFile('test.txt', 'text/plain', 'input[type="file"]')18})19it('Upload file', () => {20 cy.uploadFile('test.txt', 'text/plain', 'input[type="file"]')21})22it('Upload file', () => {23 cy.uploadFile('test.txt', 'text/plain', 'input
Using AI Code Generation
1describe('Load binary file from file system', function() {2 it('loads binary file', function() {3 cy.loadBinaryFromFile('test.png')4 .then(function(data) {5 var b64 = btoa(data);6 console.log(b64);7 });8 });9});10Cypress.Commands.add('loadBinaryFromFile', (filename) => {11 return cy.window().then((win) => {12 return new Cypress.Promise((resolve, reject) => {13 let reader = new win.FileReader();14 reader.onload = (e) => {15 let contents = e.target.result;16 resolve(contents);17 };18 reader.onerror = (e) => {19 reject(e);20 };21 cy.fixture(filename, 'base64')22 .then(Cypress.Blob.base64StringToBlob)23 .then((fileContent) => {24 return reader.readAsBinaryString(fileContent);25 });26 });27 });28});
Using AI Code Generation
1import saveAs from 'file-saver'2describe('Load file from disk and save to disk', function()3{4 it('Load file from disk and save to disk', function()5 {6 cy.loadBinaryFromFile('C:\\Users\\John\\Desktop\\test.txt')7 .then((arrayBuffer) =>8 {9 const blob = new Blob([arrayBuffer])10 saveAs(blob, 'test.txt')11 })12 })13})14Cypress.Commands.add('loadBinaryFromFile', (filePath) =>15{16 return cy.window().then((win) =>17 {18 return new Promise((resolve, reject) =>19 {20 const reader = new win.FileReader()21 reader.onload = (e) =>22 {23 resolve(e.target.result)24 }25 reader.onerror = (e) =>26 {27 reject(e)28 }29 reader.readAsArrayBuffer(win.Cypress.Blob.file(filePath))30 })31 })32})
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!!