Skip to content

Commit

Permalink
SBVT-1847: (Cypress) Add Pre-checks (#161)
Browse files Browse the repository at this point in the history
* add version check

* add import

* add checks

* SBVT-1847: moving version check to plugins

* SBVT-1847: changing the behavior of the pre check commands

* SBVT-1847: small fixes

* SBVT-1847: small fixes

* SBVT-1847: small fixes

* SBVT-1847: removing chrome beta

---------

Co-authored-by: trevornelms <trevornelms@outlook.com>
  • Loading branch information
Jakshas and tnelms1 authored Sep 18, 2023
1 parent fe48ad4 commit ccfe692
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/browser_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ jobs:
strategy:
fail-fast: false
matrix:
browser: [ firefox, chrome, 'chrome:beta', edge ]
# browser: [ firefox, chrome, 'chrome:beta', edge ]
browser: [ firefox, chrome, edge ] #chrome beta causing issues

steps:
- name: Checkout code
Expand Down
31 changes: 31 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"jimp": "^0.22.10",
"pino": "^8.15.0",
"pino-pretty": "^10.2.0",
"semver": "^7.5.4",
"uuid": "^9.0.0"
},
"repository": {
Expand Down
64 changes: 61 additions & 3 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require('dotenv').config();
const Jimp = require("jimp");
const os = require('os');
const pino = require('pino');
const semver = require("semver");

const targetArray = [{target: 'pino-pretty', level: 'warn'}]; //to log below warn uncomment two lines below
let logger = pino(pino.transport({targets: targetArray}));
Expand Down Expand Up @@ -74,7 +75,11 @@ let configFile = (() => {
config.debug = debugFolderPath; //overwrite 'true' to the folder path for passing to commands.js
fs.mkdirSync(debugFolderPath, {recursive: true});

targetArray.push({target: './debug-pino-transport.js', level: 'trace', options: {destination: `${debugFolderPath}/debug.log`}});
targetArray.push({
target: './debug-pino-transport.js',
level: 'trace',
options: {destination: `${debugFolderPath}/debug.log`}
});
logger = pino(pino.transport({targets: targetArray}));
logger.level = 'trace'; //required to overwrite default 'info'
logger.info('"debug: true" found in visualtest.config.js');
Expand Down Expand Up @@ -103,12 +108,56 @@ const apiRequest = async (method, url, body, headers) => {
headers: headers,
data: body
}).catch((err) => {
console.log(`Full error is: %o`, err.response.data);
logger.info(`Full error is: %o`, err.response.data);
return err.response;
});
};

const checkConnectionToBackend = (async (projectToken) => {
let env = projectToken.split('_')[1].toLowerCase();
if (env) {
host = `https://api.${env}.visualtest.io`;
} else {
host = "https://api.visualtest.io";
}
const response = await apiRequest('get', host);
if (response.error) {
logger.trace(response)
throw new Error(`The VisualTest SDK is unable to communicate with our server. This is usually due to one of the following reasons:\n\
1) Firewall is blocking the domain: Solution is to whitelist the domain: "*.visualtest.io"\n\
2) Internet access requires a proxy server: Talk to your network admin\n\
\n\
Error:\n\
${response.error}`);
} else {
logger.info(`Got initial connection response: ${response.body}`);
}
});
const isValidProjectToken = (async (projectToken) => {
const response = await apiRequest('get', `${host}/api/v1/projects/${projectToken.split('/')[0]}`, null, {Authorization: `Bearer ${projectToken}`});
if (response.data.status) {
logger.trace(response)
throw new Error(`Error checking projectToken: ${response.data.message}`);
} else {
logger.info(`ProjectToken is correct.`)
}
return null
})

const checkUsersVersion =(async () => {
const userVersion = package_json.version
const response = await apiRequest('get', 'https://registry.npmjs.org/@smartbear/visualtest-cypress')
const {latest: latestVersion} = response.data["dist-tags"]

if (semver.eq(userVersion, latestVersion)) {
// console.log(chalk.blue('The user has the latest version.'));
} else {
console.log(chalk.yellow('Please upgrade to the latest VisualTest Cypress Plugin version.'));
console.log(chalk.blue('npm install @smartbear/visualtest-cypress@latest'));
}
return null
})();
let getDomCapture = (async () => {
try {
const res = await apiRequest('get', `${configFile.cdnUrl}/dom-capture.min.js`);
Expand Down Expand Up @@ -170,7 +219,11 @@ function makeGlobalRunHooks() {
configFile.debug = debugFolderPath; //overwrite 'true' to the folder path for passing to commands.js
fs.mkdirSync(debugFolderPath, {recursive: true});

targetArray.push({target: './debug-pino-transport.js', level: 'trace', options: {destination: `${debugFolderPath}/debug.log`}});
targetArray.push({
target: './debug-pino-transport.js',
level: 'trace',
options: {destination: `${debugFolderPath}/debug.log`}
});
logger = pino(pino.transport({targets: targetArray}));
logger.level = 'trace'; //required to overwrite default 'info'
}
Expand Down Expand Up @@ -206,6 +259,9 @@ function makeGlobalRunHooks() {
return configFile;
}

await isValidProjectToken(configFile.projectToken);
await checkConnectionToBackend(configFile.projectToken);

logger.trace('config.projectToken: ' + configFile.projectToken);
configFile.projectId = configFile.projectToken.split('/')[0]; //take the first ~half to get the projectId
logger.trace('config.projectId: ' + configFile.projectId);
Expand Down Expand Up @@ -241,7 +297,9 @@ function makeGlobalRunHooks() {
configFile.testRunName = `${osPrettyName} / ${browserPrettyName} ${browserMajorVersion[0]}`;
}
logger.trace('config.testRunName: ' + configFile.testRunName);

if (configFile.testRunName.length > 100) {
throw new Error(`The maximum size of testRunName is 100 characters. Received: ${configFile.testRunName.length} characters.`)
}
configFile.url = host;
configFile.websiteUrl = webUrl;

Expand Down
1 change: 1 addition & 0 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ccfe692

Please sign in to comment.