From fa1cf91834b6239120d3a5a0524f0bd34e65ac39 Mon Sep 17 00:00:00 2001 From: Arthur Warnier Date: Sun, 15 May 2022 23:05:49 +0200 Subject: [PATCH] feat(authentication): add the ability to use http authentication http basic authentication can be used by creating a js file that contains the necessary credentials (as seen in testcafe documentation) => featureFileName.credentials.js re #76 --- ...http-authentication-example.credentials.js | 2 + examples/http-authentication-example.feature | 9 ++ examples/http-authentication-example.ts | 29 ++++++ package.json | 3 +- src/compiler.js | 21 ++++- yarn.lock | 92 +++++++++++-------- 6 files changed, 114 insertions(+), 42 deletions(-) create mode 100644 examples/http-authentication-example.credentials.js create mode 100644 examples/http-authentication-example.feature create mode 100644 examples/http-authentication-example.ts diff --git a/examples/http-authentication-example.credentials.js b/examples/http-authentication-example.credentials.js new file mode 100644 index 0000000..99ba811 --- /dev/null +++ b/examples/http-authentication-example.credentials.js @@ -0,0 +1,2 @@ +module.exports.username = 'user'; +module.exports.password = 'pass'; diff --git a/examples/http-authentication-example.feature b/examples/http-authentication-example.feature new file mode 100644 index 0000000..3a71677 --- /dev/null +++ b/examples/http-authentication-example.feature @@ -0,0 +1,9 @@ +Feature: HTTP Basic Authentication + + I want to be able to import HTTP credentials in my tests by using TestCafe's httpAuth functionality + + Scenario: Authenticating on Jigsaw HTTP test site + Given I created an HTTP authentication file for my feature + And I opened the HTTP test site + When I open the protected page + Then I should reach the protected page \ No newline at end of file diff --git a/examples/http-authentication-example.ts b/examples/http-authentication-example.ts new file mode 100644 index 0000000..4474a96 --- /dev/null +++ b/examples/http-authentication-example.ts @@ -0,0 +1,29 @@ +import { existsSync } from 'fs'; +import { Given, When, Then } from '@cucumber/cucumber'; +import { Selector } from 'testcafe'; + +const linkSelector = Selector( + 'div.row:nth-child(3) > div:nth-child(1) > div:nth-child(2) > div:nth-child(2) > a:nth-child(3)' +); + +Given('I created an HTTP authentication file for my feature', async (t) => { + // only works with js files at the moment + const allowedExtentions = ['.js']; + const foundCredentialFiles = allowedExtentions + .map((extention) => __filename.slice(0, -3).concat('.credentials', extention)) + .filter((filename) => existsSync(filename)); + await t.expect(foundCredentialFiles.length).gt(0); +}); + +Given('I opened the HTTP test site', async (t) => { + await t.navigateTo('https://authenticationtest.com/').expect(linkSelector.exists).ok(); +}); + +When('I open the protected page', async (t) => { + await t.click(linkSelector); +}); + +Then('I should reach the protected page', async (t) => { + const titleSelector = Selector('div.container h1'); + await t.expect(titleSelector.innerText).eql('Login Success'); +}); diff --git a/package.json b/package.json index a5ce0fc..dc7077f 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "docstring-example": "node main.js chrome examples/doc-strings.*", "datatable-example": "node main.js chrome ./examples/datatable.js ./examples/datatable.feature", "custom-param-type-example": "node main.js chrome examples/google.ts ./examples/custom-param-type.js ./examples/custom-param-type.feature --param-type-registry-file ./examples/custom-param-type-registry.js", + "http-auth-example": "node main.js chrome ./examples/http-authentication-example.ts ./examples/http-authentication-example.feature", "gitcommit": "git-cz", "gitrecommit": "git-cz --retry", "gitamend": "git-cz --amend", @@ -60,7 +61,7 @@ "cz-conventional-changelog": "3.3.0", "prettier": "^2.5.1", "standard-version": "^9.3.2", - "testcafe": "^1.18.3" + "testcafe": "^1.18.6" }, "config": { "commitizen": { diff --git a/src/compiler.js b/src/compiler.js index c3113e1..90fdbcc 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -10,7 +10,7 @@ const cucumberExpressions = require('@cucumber/cucumber-expressions'); const TestcafeESNextCompiler = require('testcafe/lib/compiler/test-file/formats/es-next/compiler'); const TestcafeTypescriptCompiler = require('testcafe/lib/compiler/test-file/formats/typescript/compiler'); const CustomizableCompilers = require('testcafe/lib/configuration/customizable-compilers'); -const { readFileSync } = require('fs'); +const { readFileSync, existsSync } = require('fs'); const { IdGenerator } = require('@cucumber/messages'); const chalk = require('chalk'); @@ -75,7 +75,6 @@ module.exports = class GherkinTestcafeCompiler { const testFile = { filename: specFile, collectedTests: [] }; const fixture = new Fixture(testFile); - const { gherkinDocument } = gherkinResult[1]; if (!gherkinDocument) { @@ -162,6 +161,17 @@ module.exports = class GherkinTestcafeCompiler { }`, }; + // TODO: handle TS + const allowedExtentions = ['.js']; + const foundCredentialFiles = allowedExtentions + .map((extention) => specFile.slice(0, -8).concat('.credentials', extention)) + .filter((filename) => existsSync(filename)); + if (foundCredentialFiles.length > 1) { + console.warn( + `Looks like you have several credential files for ${specFile}. ${foundCredentialFiles[0]} will be used` + ); + } + fixture(`Feature: ${gherkinDocument.feature.name}`) .before((ctx) => this._runFeatureHooks(ctx, meta, this.beforeAllHooks)) .after((ctx) => this._runFeatureHooks(ctx, meta, this.afterAllHooks)) @@ -172,8 +182,7 @@ module.exports = class GherkinTestcafeCompiler { return; } - const test = new Test(testFile); - test(`Scenario: ${scenario.name}`, async (t) => { + const test = new Test(testFile)(`Scenario: ${scenario.name}`, async (t) => { let error; try { @@ -195,6 +204,10 @@ module.exports = class GherkinTestcafeCompiler { 'tags', scenario.tags.length > 0 ? scenario.tags.map((tag) => tag.name).reduce((acc, cur) => `${acc},${cur}`) : '' ); + + if (foundCredentialFiles[0]) { + test.httpAuth(require(foundCredentialFiles[0])); + } }); return testFile.collectedTests; diff --git a/yarn.lock b/yarn.lock index efdb752..b6b81be 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1423,10 +1423,10 @@ acorn-hammerhead@0.4.0: dependencies: "@types/estree" "0.0.46" -acorn-hammerhead@0.5.0: - version "0.5.0" - resolved "https://registry.yarnpkg.com/acorn-hammerhead/-/acorn-hammerhead-0.5.0.tgz#94ca6ecef5c6e65a0f813580a9b130fd2775300c" - integrity sha512-TI9TFfJBfduhcM2GggayNhdYvdJ3UgS/Bu3sB7FB2AUmNCmCJ+TSOT6GXu+bodG5/xL74D5zE4XRaqyjgjsYVQ== +acorn-hammerhead@0.6.1: + version "0.6.1" + resolved "https://registry.yarnpkg.com/acorn-hammerhead/-/acorn-hammerhead-0.6.1.tgz#f8f27c58ceaf90fbdb77a92f4331a678271194f2" + integrity sha512-ZWG/nXPvFiveXhJq/PxuS+4LI1BqtEOviGXWjlTvI+64kwzaddYNaE0UzLorTX7kyxrFtxjJ4w1LmKN5yEzOCg== dependencies: "@types/estree" "0.0.46" @@ -1581,10 +1581,10 @@ async-exit-hook@^1.1.2: resolved "https://registry.yarnpkg.com/async-exit-hook/-/async-exit-hook-1.1.2.tgz#8095d75e488c29acee0551fe87252169d789cfba" integrity sha1-gJXXXkiMKazuBVH+hyUhadeJz7o= -async@0.2.6: - version "0.2.6" - resolved "https://registry.yarnpkg.com/async/-/async-0.2.6.tgz#ad3f373d9249ae324881565582bc90e152abbd68" - integrity sha1-rT83PZJJrjJIgVZVgryQ4VKrvWg= +async@3.2.3: + version "3.2.3" + resolved "https://registry.yarnpkg.com/async/-/async-3.2.3.tgz#ac53dafd3f4720ee9e8a160628f18ea91df196c9" + integrity sha512-spZRyzKL5l5BZQrr/6m/SqFdBN0q3OCI0f9rjfBzCMBIP4p75P620rR3gTmaksNOhmzgdxcaxdNfMy6anrbM0g== atob@^2.1.2: version "2.1.2" @@ -3578,10 +3578,10 @@ lodash.once@^4.0.0: resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c" integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg== -log-update-async-hook@^2.0.2: - version "2.0.2" - resolved "https://registry.yarnpkg.com/log-update-async-hook/-/log-update-async-hook-2.0.2.tgz#6eba89dbe67fa12d0b20ac47df7942947af1fcd1" - integrity sha512-HQwkKFTZeUOrDi1Duf2CSUa/pSpcaCHKLdx3D/Z16DsipzByOBffcg5y0JZA1q0n80dYgLXe2hFM9JGNgBsTDw== +log-update-async-hook@^2.0.6: + version "2.0.6" + resolved "https://registry.yarnpkg.com/log-update-async-hook/-/log-update-async-hook-2.0.6.tgz#33fa4414e73799920edaa64f2f931d2b6f627f68" + integrity sha512-UIFPlCpCxrSVL38TXzk34JhhDnvvhsjzuyqooCYy9TtTaVdBLNsuJiTWX9unO/wzBF7RwY1WTCmEmBSI3iPDCA== dependencies: ansi-escapes "^2.0.0" async-exit-hook "^1.1.2" @@ -4218,6 +4218,11 @@ promisify-event@^1.0.0: dependencies: pinkie-promise "^2.0.0" +psl@^1.1.33: + version "1.8.0" + resolved "https://registry.yarnpkg.com/psl/-/psl-1.8.0.tgz#9326f8bcfb013adcc005fdff056acce020e51c24" + integrity sha512-RIdOzyoavK+hA18OGGWDqUTsCLhtA7IcZ/6NCs4fFJaHBDab+pDDmDIByWFRQJq2Cd7r1OoQxBGKOaztq+hjIQ== + pump@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/pump/-/pump-3.0.0.tgz#b4a2116815bde2f4e1ea602354e8c75565107a64" @@ -4231,6 +4236,11 @@ punycode@^1.4.1: resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e" integrity sha1-wNWmOycYgArY4esPpSachN1BhF4= +punycode@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.1.tgz#b58b010ac40c22c5657616c8d2c2c02c7bf479ec" + integrity sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A== + q@^1.5.1: version "1.5.1" resolved "https://registry.yarnpkg.com/q/-/q-1.5.1.tgz#7e32f75b41381291d04611f1bf14109ac00651d7" @@ -4924,10 +4934,10 @@ supports-preserve-symlinks-flag@^1.0.0: resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09" integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w== -testcafe-browser-tools@2.0.22: - version "2.0.22" - resolved "https://registry.yarnpkg.com/testcafe-browser-tools/-/testcafe-browser-tools-2.0.22.tgz#86a05155e50076c6ae2c317368cef055cd84dc37" - integrity sha512-ABzKV3h+yrbxC0WfqqCjWP+/XFBH66VY8Nuz3IqDu4/9mbrn2sJpcEdcoxLVRVkIxcLUgCejF38Rorumh9iHvw== +testcafe-browser-tools@2.0.23: + version "2.0.23" + resolved "https://registry.yarnpkg.com/testcafe-browser-tools/-/testcafe-browser-tools-2.0.23.tgz#9c1957d373b14d7917bb98084f79c9087ed08cdc" + integrity sha512-Ewk2I0DIiF9j/8DqDPhRbWuEIa4nxWhJ45DzS/fiftpLuljZshV/omc6M9O3MjrBp6d4uTI45AbhMVE2APvs+Q== dependencies: array-find "^1.0.0" debug "^4.3.1" @@ -4947,15 +4957,14 @@ testcafe-browser-tools@2.0.22: read-file-relative "^1.2.0" which-promise "^1.0.0" -testcafe-hammerhead@24.5.13: - version "24.5.13" - resolved "https://registry.yarnpkg.com/testcafe-hammerhead/-/testcafe-hammerhead-24.5.13.tgz#7eeca7f8f68b2d20ad93214ee0e93b49fb140e0b" - integrity sha512-81P9to2pXBCOy+jnyEaPcjrfKk3wOv7JmZSX3KQp0MxF12X9u6Tg0JEeTMYvnEfCeNhLRYDipAZvI+t9nfx0KA== +testcafe-hammerhead@24.5.18: + version "24.5.18" + resolved "https://registry.yarnpkg.com/testcafe-hammerhead/-/testcafe-hammerhead-24.5.18.tgz#5096fa59a955651a3b594f3df5bfefff72fe1277" + integrity sha512-ae7ikqW4SzKY81BDaCc5eVyTmiiqbq8qGpr484GyVobRb4stPUKCDVyYm05t7BiO60Lhhh9Fm0w5o3oNHqQxQg== dependencies: - acorn-hammerhead "0.5.0" + acorn-hammerhead "0.6.1" asar "^2.0.1" bowser "1.6.0" - brotli "^1.3.1" crypto-md5 "^1.0.0" css "2.2.3" debug "4.3.1" @@ -4974,7 +4983,7 @@ testcafe-hammerhead@24.5.13: pinkie "2.0.4" read-file-relative "^1.2.0" semver "5.5.0" - tough-cookie "2.3.3" + tough-cookie "4.0.0" tunnel-agent "0.6.0" webauth "^1.1.0" @@ -5009,12 +5018,12 @@ testcafe-hammerhead@>=19.4.0: tunnel-agent "0.6.0" webauth "^1.1.0" -testcafe-legacy-api@5.1.2: - version "5.1.2" - resolved "https://registry.yarnpkg.com/testcafe-legacy-api/-/testcafe-legacy-api-5.1.2.tgz#a030719a43684f03fea723b4bea2e0b5fead2dde" - integrity sha512-vc9A4rFUdijlBFnNOVMk0hFfxnrAmtA7FMz1P/LtvNyui5JfkLmbyIQcJbxR2rjTINp0owZ2c+xQvYms/us7Fw== +testcafe-legacy-api@5.1.4: + version "5.1.4" + resolved "https://registry.yarnpkg.com/testcafe-legacy-api/-/testcafe-legacy-api-5.1.4.tgz#de913a79869abf9c5ff117eeb9adbef78519f4ff" + integrity sha512-CWjwGlRZdSuoWDIRBHKetpmDffR+/LKS6+69n8VM4mkLKgUwsP8p3MERHdx0obBn8wZ0LSyrYj8SCtv5f7oWZg== dependencies: - async "0.2.6" + async "3.2.3" dedent "^0.6.0" highlight-es "^1.0.0" is-jquery-obj "^0.1.0" @@ -5070,10 +5079,10 @@ testcafe-reporter-xunit@^2.2.1: resolved "https://registry.yarnpkg.com/testcafe-reporter-xunit/-/testcafe-reporter-xunit-2.2.1.tgz#674b6551bec88829d4ed08af43e7838793cf714e" integrity sha512-ge1msi8RyNVyK0QrsmC79zedV7jHasKpBPeOUZd/ORpbYLeYDnprjIeOuIukw0knnTieeYsOK29/ZD+UI7/tdw== -testcafe@^1.18.3: - version "1.18.3" - resolved "https://registry.yarnpkg.com/testcafe/-/testcafe-1.18.3.tgz#6a03697d724d01cc31ac29c4ad1f52a30f604cf9" - integrity sha512-cBWPBmY20xI9iWQzS9s2t3oIYhXl5gJzM6CTQjLXt5CMAEvWThsOzLWWnNoA2nk4CLBexZ0S5SkrYOk/SGVSaw== +testcafe@^1.18.6: + version "1.18.6" + resolved "https://registry.yarnpkg.com/testcafe/-/testcafe-1.18.6.tgz#855c692c799390a18b0d0af87e5148ac79444a59" + integrity sha512-5X/Chn5zbHy8TftyB/iXfKOizrYM8vrNLSjjyRQCW2IpYh//7EUJ0MZmBKRcXye9//eLaOoUBs/FDvAW55j4Lw== dependencies: "@babel/core" "^7.12.1" "@babel/plugin-proposal-async-generator-functions" "^7.12.1" @@ -5127,7 +5136,7 @@ testcafe@^1.18.3: is-stream "^2.0.0" json5 "^2.1.0" lodash "^4.17.13" - log-update-async-hook "^2.0.2" + log-update-async-hook "^2.0.6" make-dir "^3.0.0" mime-db "^1.41.0" moment "^2.10.3" @@ -5150,9 +5159,9 @@ testcafe@^1.18.3: semver "^5.6.0" source-map-support "^0.5.16" strip-bom "^2.0.0" - testcafe-browser-tools "2.0.22" - testcafe-hammerhead "24.5.13" - testcafe-legacy-api "5.1.2" + testcafe-browser-tools "2.0.23" + testcafe-hammerhead "24.5.18" + testcafe-legacy-api "5.1.4" testcafe-reporter-dashboard "0.2.5" testcafe-reporter-json "^2.1.0" testcafe-reporter-list "^2.1.0" @@ -5264,6 +5273,15 @@ tough-cookie@2.3.3: dependencies: punycode "^1.4.1" +tough-cookie@4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-4.0.0.tgz#d822234eeca882f991f0f908824ad2622ddbece4" + integrity sha512-tHdtEpQCMrc1YLrMaqXXcj6AxhYi/xgit6mZu1+EDWUn+qhUf8wMQoFIy9NXuq23zAwtcB0t/MjACGR18pcRbg== + dependencies: + psl "^1.1.33" + punycode "^2.1.1" + universalify "^0.1.2" + tr46@~0.0.3: version "0.0.3" resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a" @@ -5381,7 +5399,7 @@ unicode-property-aliases-ecmascript@^1.0.4: resolved "https://registry.yarnpkg.com/unicode-property-aliases-ecmascript/-/unicode-property-aliases-ecmascript-1.1.0.tgz#dd57a99f6207bedff4628abefb94c50db941c8f4" integrity sha512-PqSoPh/pWetQ2phoj5RLiaqIk4kCNwoV3CI+LfGmWLKI3rE3kl1h59XpX2BjgDrmbxD9ARtQobPGU1SguCYuQg== -universalify@^0.1.0: +universalify@^0.1.0, universalify@^0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66" integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==