Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: url path based screenshot download #92

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading