Skip to content

Commit 8955e3c

Browse files
committed
Add devices support
1 parent a7f7bda commit 8955e3c

File tree

3 files changed

+37
-16
lines changed

3 files changed

+37
-16
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict'
22

3+
const devices = require('puppeteer/DeviceDescriptors')
34
const createTempFile = require('create-temp-file2')
45
const puppeteer = require('puppeteer')
56

@@ -13,12 +14,18 @@ async function getHTML (url, opts = {}) {
1314
}
1415

1516
async function takeScreenshot (url, opts = {}) {
16-
const { type = 'png' } = opts
17+
const { type = 'png', device: deviceDescriptor } = opts
1718
const tempFile = createTempFile({ ext: `.${type}` })
1819
const { path } = tempFile
1920

2021
const browser = await puppeteer.launch()
2122
const page = await browser.newPage()
23+
24+
if (deviceDescriptor) {
25+
const device = devices[deviceDescriptor]
26+
if (device) await page.emulate(device)
27+
}
28+
2229
await page.goto(url)
2330
await page.screenshot(Object.assign({ path, type }, opts))
2431
browser.close()

test/example-iphone.png

46.6 KB
Loading

test/index.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,38 @@ describe('frowser', () => {
1818
})
1919

2020
describe('.takeScreenshot', () => {
21-
it('png', () =>
22-
frowser.takeScreenshot('http://example.com').then(tmpStream => {
23-
should(fs.readFileSync(tmpStream.path)).be.eql(
24-
fs.readFileSync('test/example.png')
25-
)
26-
should(path.extname(tmpStream.path)).be.equal('.png')
27-
tmpStream.cleanupSync()
28-
}))
29-
30-
it('jpeg', () =>
31-
frowser
32-
.takeScreenshot('http://example.com', { type: 'jpeg' })
33-
.then(tmpStream => {
21+
describe('format', () => {
22+
it('png', () =>
23+
frowser.takeScreenshot('http://example.com').then(tmpStream => {
3424
should(fs.readFileSync(tmpStream.path)).be.eql(
35-
fs.readFileSync('test/example.jpeg')
25+
fs.readFileSync('test/example.png')
3626
)
37-
should(path.extname(tmpStream.path)).be.equal('.jpeg')
27+
should(path.extname(tmpStream.path)).be.equal('.png')
3828
tmpStream.cleanupSync()
3929
}))
30+
31+
it('jpeg', () =>
32+
frowser
33+
.takeScreenshot('http://example.com', { type: 'jpeg' })
34+
.then(tmpStream => {
35+
should(fs.readFileSync(tmpStream.path)).be.eql(
36+
fs.readFileSync('test/example.jpeg')
37+
)
38+
should(path.extname(tmpStream.path)).be.equal('.jpeg')
39+
tmpStream.cleanupSync()
40+
}))
41+
})
42+
43+
describe('devices', () => {
44+
it('iPhone 6', () =>
45+
frowser
46+
.takeScreenshot('http://example.com', { device: 'iPhone 6' })
47+
.then(tmpStream => {
48+
should(fs.readFileSync(tmpStream.path)).be.eql(
49+
fs.readFileSync('test/example-iphone.png')
50+
)
51+
tmpStream.cleanupSync()
52+
}))
53+
})
4054
})
4155
})

0 commit comments

Comments
 (0)