From a44f2bb6ebe6979efc3c4711147b2420aa290e15 Mon Sep 17 00:00:00 2001 From: Dan Miller Date: Thu, 28 Mar 2019 09:53:38 -0230 Subject: [PATCH] Use webRequest to cancel all requests to infura in e2e tests. --- gulpfile.js | 36 +++++++++++++++++++ package.json | 1 + test/e2e/beta/from-import-beta-ui.spec.js | 13 ++++++- .../beta/metamask-beta-responsive-ui.spec.js | 13 ++++++- test/e2e/beta/metamask-beta-ui.spec.js | 21 ++++++++--- 5 files changed, 78 insertions(+), 6 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index c1012d5aa..f4c9fc9dd 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -195,6 +195,21 @@ gulp.task('manifest:production', function () { .pipe(gulp.dest('./dist/', { overwrite: true })) }) +gulp.task('manifest:testing', function () { + return gulp.src([ + './dist/firefox/manifest.json', + './dist/chrome/manifest.json', + ], {base: './dist/'}) + + // Exclude chromereload script in production: + .pipe(jsoneditor(function (json) { + json.permissions = [...json.permissions, 'webRequestBlocking'] + return json + })) + + .pipe(gulp.dest('./dist/', { overwrite: true })) +}) + gulp.task('copy', gulp.series( gulp.parallel(...copyTaskNames), @@ -212,6 +227,15 @@ gulp.task('dev:copy', ) ) +gulp.task('test:copy', + gulp.series( + gulp.parallel(...copyDevTaskNames), + 'manifest:chrome', + 'manifest:opera', + 'manifest:testing' + ) +) + // scss compilation and autoprefixing tasks gulp.task('build:scss', createScssBuildTask({ @@ -383,6 +407,18 @@ gulp.task('dev', ) ) +gulp.task('dev:test', + gulp.series( + 'clean', + 'dev:scss', + gulp.parallel( + 'dev:extension:js', + 'test:copy', + 'dev:reload' + ) + ) +) + gulp.task('dev:extension', gulp.series( 'clean', diff --git a/package.json b/package.json index 873d53222..79451612c 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dist": "gulp dist", "doc": "jsdoc -c development/tools/.jsdoc.json", "publish-docs": "gh-pages -d docs/jsdocs", + "start:test": "gulp dev:test", "test": "npm run test:unit && npm run test:integration && npm run lint", "watch:test:unit": "nodemon --exec \"npm run test:unit\" ./test ./app ./ui", "test:unit": "cross-env METAMASK_ENV=test mocha --exit --require test/setup.js --recursive \"test/unit/**/*.js\" \"ui/app/**/*.test.js\"", diff --git a/test/e2e/beta/from-import-beta-ui.spec.js b/test/e2e/beta/from-import-beta-ui.spec.js index 8aa0fb628..962ca690a 100644 --- a/test/e2e/beta/from-import-beta-ui.spec.js +++ b/test/e2e/beta/from-import-beta-ui.spec.js @@ -76,7 +76,18 @@ describe('Using MetaMask with an existing account', function () { 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); }' + 'return window.origFetch(...args); };' + + 'function cancelInfuraRequest(requestDetails) {' + + 'console.log("Canceling: " + requestDetails.url);' + + 'return {' + + 'cancel: true' + + '};' + + ' }' + + 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + + 'cancelInfuraRequest,' + + '{urls: ["https://*.infura.io/*"]},' + + '["blocking"]' + + ');' ) }) diff --git a/test/e2e/beta/metamask-beta-responsive-ui.spec.js b/test/e2e/beta/metamask-beta-responsive-ui.spec.js index 6df1da051..b1ed8536e 100644 --- a/test/e2e/beta/metamask-beta-responsive-ui.spec.js +++ b/test/e2e/beta/metamask-beta-responsive-ui.spec.js @@ -75,7 +75,18 @@ describe('MetaMask', function () { 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); }' + 'return window.origFetch(...args); };' + + 'function cancelInfuraRequest(requestDetails) {' + + 'console.log("Canceling: " + requestDetails.url);' + + 'return {' + + 'cancel: true' + + '};' + + ' }' + + 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + + 'cancelInfuraRequest,' + + '{urls: ["https://*.infura.io/*"]},' + + '["blocking"]' + + ');' ) }) diff --git a/test/e2e/beta/metamask-beta-ui.spec.js b/test/e2e/beta/metamask-beta-ui.spec.js index 2700d1656..202a0052d 100644 --- a/test/e2e/beta/metamask-beta-ui.spec.js +++ b/test/e2e/beta/metamask-beta-ui.spec.js @@ -80,7 +80,18 @@ describe('MetaMask', function () { 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.metametrics + '\')) }); } else if ' + '(args[0] === "https://dev.blockscale.net/api/gasexpress.json") { return ' + 'Promise.resolve({ json: () => Promise.resolve(JSON.parse(\'' + fetchMockResponses.gasExpress + '\')) }); } ' + - 'return window.origFetch(...args); }' + 'return window.origFetch(...args); };' + + 'function cancelInfuraRequest(requestDetails) {' + + 'console.log("Canceling: " + requestDetails.url);' + + 'return {' + + 'cancel: true' + + '};' + + ' }' + + 'window.chrome && window.chrome.webRequest && window.chrome.webRequest.onBeforeRequest.addListener(' + + 'cancelInfuraRequest,' + + '{urls: ["https://*.infura.io/*"]},' + + '["blocking"]' + + ');' ) }) @@ -1015,11 +1026,13 @@ describe('MetaMask', function () { const functionType = await findElement(driver, By.css('.confirm-page-container-content__function-type')) const functionTypeText = await functionType.getText() - assert.equal(functionTypeText, 'Transfer') + assert.equal(functionTypeText, 'Not Found') const confirmDataDiv = await findElement(driver, By.css('.confirm-page-container-content__data-box')) const confirmDataText = await confirmDataDiv.getText() - assert.equal(confirmDataText.match(/0xa9059cbb0000000000000000000000002f318c334780961fb129d2a6c30d0763d9a5c97/)) + + await delay(regularDelayMs) + assert(confirmDataText.match(/0xa9059cbb0000000000000000000000002f318c334780961fb129d2a6c30d0763d9a5c97/)) const detailsTab = await findElement(driver, By.xpath(`//li[contains(text(), 'Details')]`)) detailsTab.click() @@ -1191,7 +1204,7 @@ describe('MetaMask', function () { const functionType = await findElement(driver, By.css('.confirm-page-container-content__function-type')) const functionTypeText = await functionType.getText() - assert.equal(functionTypeText, 'Approve') + assert.equal(functionTypeText, 'Not Found') const confirmDataDiv = await findElement(driver, By.css('.confirm-page-container-content__data-box')) const confirmDataText = await confirmDataDiv.getText()