Skip to content

Commit

Permalink
feat(audit): device support using lighthouse (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
samarpanda authored and lirantal committed Oct 28, 2019
1 parent 5514652 commit 82ade9b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
12 changes: 12 additions & 0 deletions __tests__/Audit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,16 @@ describe('Audit', () => {

expect(lighthouse.mock.calls[0][0]).toBe(url)
})

test('a URL scan with device flag should result in calling lighthouse with that url & flag', async () => {
const url = 'https://abc.com'
const opts = {}
opts.emulatedFormFactor = 'mobile'

const audit = new Audit()
await audit.scanUrl(url, opts)

expect(lighthouse.mock.calls[0][0]).toBe(url)
expect(lighthouse.mock.calls[1][1]).toHaveProperty('emulatedFormFactor', 'mobile')
})
})
28 changes: 28 additions & 0 deletions __tests__/Utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,32 @@ describe('Utils', () => {
const url = 'https://user:pass@google.com?version=1&minor=5&source=github#hash'
expect(Utils.trimUtmParams(url)).toEqual(url)
})

test('parseDevice & hasDevice function with no device flag', async () => {
const argv = {}
const defaultDevice = ''
expect(Utils.parseDevice(argv)).toEqual(defaultDevice)
expect(Utils.hasDevice(argv)).toEqual(false)
})

test('parseDevice & hasDevice with desktop flag', async () => {
const argv = { desktop: true }
const device = 'desktop'
expect(Utils.parseDevice(argv)).toEqual(device)
expect(Utils.hasDevice(argv)).toEqual(true)
})

test('parseDevice & hasDevice with mobile flag', async () => {
const argv = { mobile: true }
const device = 'mobile'
expect(Utils.parseDevice(argv)).toEqual(device)
expect(Utils.hasDevice(argv)).toEqual(true)
})

test('parseDevice & hasDevice with none flag', async () => {
const argv = { none: true }
const device = 'none'
expect(Utils.parseDevice(argv)).toEqual(device)
expect(Utils.hasDevice(argv)).toEqual(true)
})
})
8 changes: 7 additions & 1 deletion bin/is-website-vulnerable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ if (!url) {
process.exit(1)
}

const opts = {}

if (Utils.hasDevice(argv)) {
opts.emulatedFormFactor = Utils.parseDevice(argv)
}

const audit = new Audit()
const showProgressBar = !argv.json
audit
.scanUrl(url, showProgressBar)
.scanUrl(url, opts, showProgressBar)
.then(results => {
var renderer
if (argv.json) {
Expand Down
10 changes: 9 additions & 1 deletion src/Audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = class Audit {
}
}

async scanUrl(url, progress = false) {
async scanUrl(url, optflags = {}, progress = false) {
// Start chrome-launcher Spinner

// chrome-launcher Spinner
Expand Down Expand Up @@ -58,6 +58,14 @@ module.exports = class Audit {
const opts = {}
opts.port = chromeInstance.port

if (Object.prototype.hasOwnProperty.call(optflags, 'emulatedFormFactor')) {
// https://github.com/GoogleChrome/lighthouse#cli-options refer --emulated-form-factor
debug(`setting up lighthouse device flag: ${opts.emulatedFormFactor}`)
opts.emulatedFormFactor = optflags.emulatedFormFactor
} else {
debug(`lighthouse default device flag: mobile`)
}

debug(`invoking lighthouse scan for: ${url}`)
const scanResult = await lighthouse(url, opts, {
extends: 'lighthouse:default',
Expand Down
14 changes: 14 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,19 @@ module.exports = {
const query = nonUtmQueryParams.length > 0 ? `?${nonUtmQueryParams.join('&')}` : ''
const hash = parsedUrl.hash ? parsedUrl.hash : ''
return `${parsedUrl.protocol}//${auth}${parsedUrl.host}${pathname}${query}${hash}`
},
parseDevice: function(argv) {
let device = ''
if (argv.mobile) {
device = 'mobile'
} else if (argv.desktop) {
device = 'desktop'
} else if (argv.none) {
device = 'none'
}
return device
},
hasDevice: function(argv) {
return argv.mobile || argv.desktop || argv.none || false
}
}

0 comments on commit 82ade9b

Please sign in to comment.