Skip to content

Commit

Permalink
Merge pull request #891 from browserstack/axios-master-without-speedb…
Browse files Browse the repository at this point in the history
…oats

Axios master without speedboats
  • Loading branch information
pranavj1001 authored Nov 20, 2024
2 parents 653e76d + c101a57 commit f611498
Show file tree
Hide file tree
Showing 38 changed files with 1,696 additions and 1,555 deletions.
550 changes: 274 additions & 276 deletions bin/accessibility-automation/cypress/index.js

Large diffs are not rendered by default.

68 changes: 41 additions & 27 deletions bin/accessibility-automation/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const { API_URL } = require('./constants');
const utils = require('../helpers/utils');
const fs = require('fs');
const path = require('path');
const request = require('request');
const axios = require('axios');
const os = require('os');
const glob = require('glob');
const helper = require('../helpers/helper');
const { CYPRESS_V10_AND_ABOVE_CONFIG_FILE_EXTENSIONS } = require('../helpers/constants');
const { consoleHolder } = require("../testObservability/helper/constants");
const supportFileContentMap = {}
const HttpsProxyAgent = require('https-proxy-agent');

exports.checkAccessibilityPlatform = (user_config) => {
let accessibility = false;
Expand Down Expand Up @@ -86,8 +88,8 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {

const config = {
auth: {
user: userName,
pass: accessKey
username: userName,
password: accessKey
},
headers: {
'Content-Type': 'application/json'
Expand All @@ -105,19 +107,21 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {
process.env.BROWSERSTACK_TEST_ACCESSIBILITY = 'true';
}
logger.debug(`BrowserStack Accessibility Automation Test Run ID: ${response.data.data.id}`);

this.setAccessibilityCypressCapabilities(user_config, response.data);
helper.setBrowserstackCypressCliDependency(user_config);

} catch (error) {
if (error.response) {
logger.error("Incorrect Cred")
logger.error(
`Exception while creating test run for BrowserStack Accessibility Automation: ${
error.response.status
} ${error.response.statusText} ${JSON.stringify(error.response.data)}`
);
} else {
if(error.message === 'Invalid configuration passed.') {
logger.error("Invalid configuration passed.")
logger.error(
`Exception while creating test run for BrowserStack Accessibility Automation: ${
error.message || error.stack
Expand All @@ -126,7 +130,7 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {
for(const errorkey of error.errors){
logger.error(errorkey.message);
}

} else {
logger.error(
`Exception while creating test run for BrowserStack Accessibility Automation: ${
Expand All @@ -143,33 +147,43 @@ exports.createAccessibilityTestRun = async (user_config, framework) => {

const nodeRequest = (type, url, data, config) => {
return new Promise(async (resolve, reject) => {
const options = {...config,...{
const options = {
...config,
method: type,
url: `${API_URL}/${url}`,
body: data,
json: config.headers['Content-Type'] === 'application/json',
}};
data: data
};

request(options, function callback(error, response, body) {
if(error) {
logger.info("error in nodeRequest", error);
reject(error);
} else if(!(response.statusCode == 201 || response.statusCode == 200)) {
logger.info("response.statusCode in nodeRequest", response.statusCode);
reject(response && response.body ? response.body : `Received response from BrowserStack Server with status : ${response.statusCode}`);
if(process.env.HTTP_PROXY){
options.proxy = false
options.httpsAgent = new HttpsProxyAgent(process.env.HTTP_PROXY);

} else if (process.env.HTTPS_PROXY){
options.proxy = false
options.httpsAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
}

axios(options).then(response => {
if(!(response.status == 201 || response.status == 200)) {
logger.info("response.status in nodeRequest", response.status);
reject(response && response.data ? response.data : `Received response from BrowserStack Server with status : ${response.status}`);
} else {
try {
if(typeof(body) !== 'object') body = JSON.parse(body);
} catch(e) {
if(!url.includes('/stop')) {
reject('Not a JSON response from BrowserStack Server');
try {
if(typeof(response.data) !== 'object') body = JSON.parse(response.data);
} catch(e) {
if(!url.includes('/stop')) {
reject('Not a JSON response from BrowserStack Server');
}
}
}
resolve({
data: body
});
resolve({
data: response.data
});
}
});
}).catch(error => {

logger.info("error in nodeRequest", error);
reject(error);
})
});
}

Expand Down Expand Up @@ -211,7 +225,7 @@ exports.setAccessibilityEventListeners = (bsConfig) => {
try {
if(!file.includes('commands.js') && !file.includes('commands.ts')) {
const defaultFileContent = fs.readFileSync(file, {encoding: 'utf-8'});

let cypressCommandEventListener = getAccessibilityCypressCommandEventListener(path.extname(file));
if(!defaultFileContent.includes(cypressCommandEventListener)) {
let newFileContent = defaultFileContent +
Expand Down
5 changes: 2 additions & 3 deletions bin/accessibility-automation/plugin/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const path = require("node:path");

const browserstackAccessibility = (on, config) => {
Expand All @@ -10,7 +9,7 @@ const browserstackAccessibility = (on, config) => {
on('task', {
browserstack_log(message) {
console.log(message)

return null
},
})
Expand All @@ -36,7 +35,7 @@ const browserstackAccessibility = (on, config) => {
}
}
} catch(err) {}

})
config.env.ACCESSIBILITY_EXTENSION_PATH = process.env.ACCESSIBILITY_EXTENSION_PATH
config.env.OS_VERSION = process.env.OS_VERSION
Expand Down
1 change: 0 additions & 1 deletion bin/commands/generateReport.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module.exports = function generateReport(args, rawArgs) {
let messageType = Constants.messageTypes.INFO;
let errorCode = null;
let buildId = args._[1];

reportGenerator(bsConfig, buildId, args, rawArgs, buildReportData);
utils.sendUsageReport(bsConfig, args, 'generate-report called', messageType, errorCode, buildReportData, rawArgs);
} catch(err) {
Expand Down
77 changes: 39 additions & 38 deletions bin/commands/info.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
const request = require('request');

const axios = require('axios').default;
const config = require("../helpers/config"),
logger = require("../helpers/logger").winstonLogger,
Constants = require("../helpers/constants"),
utils = require("../helpers/utils"),
getInitialDetails = require('../helpers/getInitialDetails').getInitialDetails;

const { setAxiosProxy } = require('../helpers/helper');

module.exports = function info(args, rawArgs) {
let bsConfigPath = utils.getConfigPath(args.cf);

Expand All @@ -19,7 +20,7 @@ module.exports = function info(args, rawArgs) {
// accept the access key from command line if provided
utils.setAccessKey(bsConfig, args);

getInitialDetails(bsConfig, args, rawArgs).then((buildReportData) => {
getInitialDetails(bsConfig, args, rawArgs).then(async (buildReportData) => {

utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);

Expand All @@ -43,65 +44,65 @@ module.exports = function info(args, rawArgs) {
options.url = `${config.turboScaleBuildsUrl}/${buildId}`;
}

request.get(options, function (err, resp, body) {
let message = null;
let messageType = null;
let errorCode = null;

if (err) {
message = Constants.userMessages.BUILD_INFO_FAILED;
messageType = Constants.messageTypes.ERROR;
errorCode = 'api_failed_build_info';

logger.info(message);
logger.error(utils.formatRequest(err, resp, body));
} else {
let build = null;
let message = null;
let messageType = null;
let errorCode = null;

options.config = {
auth: {
username: bsConfig.auth.username,
password: bsConfig.auth.access_key
},
headers: options.headers
};
setAxiosProxy(options.config);

try {
const response = await axios.get(options.url, options.config);
let build = null;
try {
build = JSON.parse(body);
build = response.data;
} catch (error) {
build = null;
}

if (resp.statusCode == 299) {
if (response.status == 299) {
messageType = Constants.messageTypes.INFO;
errorCode = 'api_deprecated';

if (build) {
message = build.message;
logger.info(message);
} else {
message = Constants.userMessages.API_DEPRECATED;
logger.info(message);
}
logger.info(utils.formatRequest(err, resp, body));
} else if (resp.statusCode != 200) {
message = build ? build.message : Constants.userMessages.API_DEPRECATED;
logger.info(utils.formatRequest(response.statusText, response, response.data));
} else if (response.status != 200) {
message = Constants.userMessages.BUILD_INFO_FAILED;
messageType = Constants.messageTypes.ERROR;
errorCode = 'api_failed_build_info';

if (build) {
message = `${
Constants.userMessages.BUILD_INFO_FAILED
} with error: \n${JSON.stringify(build, null, 2)}`;
logger.error(message);
if (build.message === 'Unauthorized') errorCode = 'api_auth_failed';
} else {
message = Constants.userMessages.BUILD_INFO_FAILED;
logger.error(message);
}
logger.error(utils.formatRequest(err, resp, body));
logger.error(message);
logger.error(utils.formatRequest(response.statusText, response, response.data));
} else {
messageType = Constants.messageTypes.SUCCESS;
message = `Build info for build id: \n ${JSON.stringify(
build,
null,
2
)}`;
logger.info(message);
}
}
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode, buildReportData, rawArgs);
});
logger.info(message);
} catch (error) {
message = `${
Constants.userMessages.BUILD_INFO_FAILED
} with error: \n${error.response.data.message}`;
messageType = Constants.messageTypes.ERROR;
errorCode = 'api_failed_build_info';
logger.info(message);
logger.error(utils.formatRequest(error.response.statusText, error.response, error.response.data));
}
utils.sendUsageReport(bsConfig, args, message, messageType, errorCode, buildReportData, rawArgs);
}).catch((err) => {
logger.warn(err);
});
Expand Down
16 changes: 7 additions & 9 deletions bin/commands/runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const {
printBuildLink
} = require('../testObservability/helper/helper');


const {
createAccessibilityTestRun,
setAccessibilityEventListeners,
Expand All @@ -39,6 +38,7 @@ const {
} = require('../accessibility-automation/helper');
const { isTurboScaleSession, getTurboScaleGridDetails, patchCypressConfigFileContent, atsFileCleanup } = require('../helpers/atsHelper');


module.exports = function run(args, rawArgs) {

markBlockStart('preBuild');
Expand All @@ -64,15 +64,14 @@ module.exports = function run(args, rawArgs) {

// set cypress config filename
utils.setCypressConfigFilename(bsConfig, args);

/*
Set testObservability & browserstackAutomation flags
*/

/* Set testObservability & browserstackAutomation flags */
const [isTestObservabilitySession, isBrowserstackInfra] = setTestObservabilityFlags(bsConfig);
const checkAccessibility = checkAccessibilityPlatform(bsConfig);
const isAccessibilitySession = bsConfig.run_settings.accessibility || checkAccessibility;
const turboScaleSession = isTurboScaleSession(bsConfig);
Constants.turboScaleObj.enabled = turboScaleSession;


utils.setUsageReportingFlag(bsConfig, args.disableUsageReporting);

Expand Down Expand Up @@ -102,6 +101,7 @@ module.exports = function run(args, rawArgs) {
// set spec timeout
utils.setSpecTimeout(bsConfig, args);
}


// accept the specs list from command line if provided
utils.setUserSpecs(bsConfig, args);
Expand All @@ -112,9 +112,7 @@ module.exports = function run(args, rawArgs) {
// set build tag caps
utils.setBuildTags(bsConfig, args);

/*
Send build start to Observability
*/
// Send build start to Observability
if(isTestObservabilitySession) {
await launchTestSession(bsConfig, bsConfigPath);
utils.setO11yProcessHooks(null, bsConfig, args, null, buildReportData);
Expand Down Expand Up @@ -379,7 +377,7 @@ module.exports = function run(args, rawArgs) {
});
} else if(!turboScaleSession){
let stacktraceUrl = getStackTraceUrl();
downloadBuildStacktrace(stacktraceUrl).then((message) => {
downloadBuildStacktrace(stacktraceUrl, bsConfig).then((message) => {
utils.sendUsageReport(bsConfig, args, message, Constants.messageTypes.SUCCESS, null, buildReportData, rawArgs);
}).catch((err) => {
let message = `Downloading build stacktrace failed with statuscode: ${err}. Please visit ${data.dashboard_url} for additional details.`;
Expand Down
1 change: 0 additions & 1 deletion bin/commands/stop.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const request = require('request');

const config = require("../helpers/config"),
logger = require("../helpers/logger").winstonLogger,
Expand Down
Loading

0 comments on commit f611498

Please sign in to comment.