Skip to content

Commit

Permalink
Merge pull request #92 from BrowserStackCE/develop
Browse files Browse the repository at this point in the history
fix: url path based screenshot download
  • Loading branch information
Rushabhshroff committed Dec 20, 2023
2 parents 5cf5399 + 61997e6 commit e6ad6ed
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
60 changes: 57 additions & 3 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@percy/report",
"description": "Package to generate a build report and project summary report for Percy, BrowserStack's visual testing platform",
"version": "0.0.5",
"version": "0.0.6",
"main": "src/index.js",
"license": "MIT",
"author": "BrowserStack Pvt Ltd",
Expand Down Expand Up @@ -44,11 +44,12 @@
"commander": "^9.4.0",
"date-fns": "^2.29.3",
"dotenv": "^16.0.3",
"ejs": "^3.1.8"
"ejs": "^3.1.8",
"p-queue": "^8.0.1"
},
"devDependencies": {
"@percy/selenium-webdriver": "^1.0.2",
"chai": "^4.3.7",
"mocha": "^10.2.0"
}
}
}
29 changes: 21 additions & 8 deletions src/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const defaultConfig = {
downloadPath: './Reports',
diffThreshold: 1
}
let downloadQueue;
module.exports.Generate = async function (config) {
let { buildId, percyToken, apiUrl, downloadImages, downloadPath, diffThreshold } = Object.assign({}, defaultConfig, config)
let axios = new Axios({
Expand All @@ -28,6 +29,11 @@ module.exports.Generate = async function (config) {
throw res.data
}
})
if(!downloadQueue){
await import('p-queue').then(({default:PQueue})=>{
downloadQueue = new PQueue({concurrency:1,intervalCap:5,interval:5000})
})
}
const isApp = buildDetails['data']['attributes']['type'] == 'app'
while (buildDetails.data && buildDetails.data.attributes.state !== 'finished') {
console.log("Waiting for build to complete on Percy...")
Expand Down Expand Up @@ -101,10 +107,12 @@ module.exports.Generate = async function (config) {
}
}
if (downloadImages) {


['base', 'head', 'diff'].forEach((val) => {
if (images[val]) {
images[val].file = downloadImage({
name: String(snapshot?.['attributes'].name).replace('/', '-'),
name: String(snapshot?.['attributes'].name),
compTag,
width: images[val].width,
type: val,
Expand Down Expand Up @@ -167,18 +175,23 @@ function downloadImage(options) {
if (!fs.existsSync(`${baseDir}/${type}`)) {
fs.mkdirSync(`${baseDir}/${type}`, { recursive: true })
}
let path = `${baseDir}/${type}/${name}-${compTag}-${width}.png`
let composedName = `${name}-${compTag}-${width}.png`.replace(/[\\\/]/g,'_')
let path = `${baseDir}/${type}/${composedName}`
try {
new Axios({ responseType: 'arraybuffer', url: url }).get(url).then((file) => {
downloadQueue.add(()=>new Axios({ responseType: 'arraybuffer', url: url }).get(url).then((file) => {
console.log("Download Complete:" + path)
fs.writeFileSync(path, file.data)
}).catch((err) => {
console.error("Failed to Download: " + path)
console.error(err)
})
return `file:./${type}/${name}-${compTag}-${width}.png`;
} catch {

if(err.code == 'ECONNRESET'){
console.log('Retrying '+ path)
}else{
console.error(`Failed ${path}, error: ${err.code}`)
}
}))
return `file:./${type}/${composedName}`;
} catch(err) {

}
}

Expand Down

0 comments on commit e6ad6ed

Please sign in to comment.