Skip to content

Commit 201f4dd

Browse files
authored
Show how to increase the browser window size (#657)
1 parent 5236d97 commit 201f4dd

File tree

10 files changed

+158
-19
lines changed

10 files changed

+158
-19
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Recipe | Description
2222
[Out-of-the-box TypeScript](./examples/fundamentals__typescript) | Write tests in TypeScript without setting up preprocessors
2323
[Per-test timeout](./examples/fundamentals__timeout) | Fail a test if it runs longer than the specified time limit
2424
[Cypress events](./examples/fundamentals__cy-events) | Using `Cypress.on` and `cy.on` to listen to [Cypress events](https://on.cypress.io/catalog-of-events) like `before:window:load`
25+
[Video resolution](./examples/fundamentals__window-size) | Increase the browser window size to record high quality videos and capture detailed screenshots
2526

2627
## Testing the DOM
2728

circle.yml

+26-19
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ jobs:
211211
<<: *defaults
212212
file-upload-react:
213213
<<: *defaults
214+
fundamentals__window-size:
215+
<<: *defaults
214216
fundamentals__node-modules:
215217
<<: *defaults
216218
fundamentals__fixtures:
@@ -427,6 +429,10 @@ all_jobs: &all_jobs
427429
- file-upload-react:
428430
requires:
429431
- build
432+
- fundamentals__window-size:
433+
requires:
434+
- build
435+
test-command: npm run test:ci:record -- --group $CIRCLE_JOB
430436
- fundamentals__node-modules:
431437
requires:
432438
- build
@@ -442,6 +448,25 @@ all_jobs: &all_jobs
442448
- fundamentals__cy-events:
443449
requires:
444450
- build
451+
- fundamentals__dynamic-tests:
452+
requires:
453+
- build
454+
- fundamentals__fixtures:
455+
requires:
456+
- build
457+
- fundamentals__timeout:
458+
requires:
459+
- build
460+
- fundamentals__module-api:
461+
requires:
462+
- build
463+
- fundamentals__module-api-wrap:
464+
requires:
465+
- build
466+
test-command: npm run test:ci
467+
- fundamentals__typescript:
468+
requires:
469+
- build
445470
- logging-in__basic-auth:
446471
requires:
447472
- build
@@ -577,25 +602,6 @@ all_jobs: &all_jobs
577602
- server-communication__server-timing:
578603
requires:
579604
- build
580-
- fundamentals__dynamic-tests:
581-
requires:
582-
- build
583-
- fundamentals__fixtures:
584-
requires:
585-
- build
586-
- fundamentals__timeout:
587-
requires:
588-
- build
589-
- fundamentals__module-api:
590-
requires:
591-
- build
592-
- fundamentals__module-api-wrap:
593-
requires:
594-
- build
595-
test-command: npm run test:ci
596-
- fundamentals__typescript:
597-
requires:
598-
- build
599605

600606
# to avoid constantly tweaking required jobs on CircleCI
601607
# we can have a single job wait on all other test jobs here.
@@ -622,6 +628,7 @@ all_jobs: &all_jobs
622628
- blogs__form-submit
623629
- extending-cypress__chai-assertions
624630
- file-upload-react
631+
- fundamentals__window-size
625632
- fundamentals__node-modules
626633
- fundamentals__fixtures
627634
- fundamentals__timeout
2.26 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Browser window size vs Application viewport
2+
3+
![Showing the window dimensions](images/page.png)
4+
5+
The browser window, the command log, and the application's iframe sizes are printed.
6+
7+
The [plugin file](./cypress/plugins/index.js) increases the browser window size when running in the headless mode to produce high quality video. See [launch api](https://on.cypress.io/browser-launch-api) for more documentation.
8+
9+
**Note:** increasing the browser window size seems to not work very well in Electron browser
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"fixturesFolder": false,
3+
"supportFile": false,
4+
"viewportWidth": 2000
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference types="cypress" />
2+
/* eslint-env browser */
3+
describe('window size', () => {
4+
it('shows the page clearly', () => {
5+
// let's get the total opened browser dimensions
6+
const windowWidth = window.top.innerWidth
7+
const windowHeight = window.top.innerHeight
8+
9+
cy.log(`browser window is: **${windowWidth} x ${windowHeight}**`)
10+
cy.task('log', { message: 'browser window', o: { windowWidth, windowHeight } }, { log: false })
11+
12+
// part of the browser window is taken up the command log
13+
const commandLog = window.top.document.querySelector('.reporter-wrap')
14+
const commandLogWidth = commandLog.offsetWidth
15+
const commandLogHeight = commandLog.offsetHeight
16+
17+
cy.log(`command log is: **${commandLogWidth} x ${commandLogHeight}**`)
18+
cy.task('log', { message: 'command log', o: { commandLogWidth, commandLogHeight } }, { log: false })
19+
20+
// the app thinks it has the following dimensions
21+
cy.window({ log: false }).then((win) => {
22+
// the application is scaled to fit into its iframe
23+
// and the iframe's dimensions are
24+
const iframe = cy.state('$autIframe')
25+
const iframeWidth = Math.round(iframe.width())
26+
const iframeHeight = Math.round(iframe.height())
27+
28+
cy.log(`app iframe real size is: **${iframeWidth} x ${iframeHeight}**`)
29+
cy.task('log', { message: 'app iframe real size', o: { iframeWidth, iframeHeight } }, { log: false })
30+
31+
// the application thinks it has the window of the follow size
32+
// which is the "viewport" numbers
33+
const viewportWidth = win.innerWidth
34+
const viewportHeight = win.innerHeight
35+
36+
cy.log(`app viewport is: **${viewportWidth} x ${viewportHeight}**`)
37+
cy.task('log', { message: 'app viewport', o: { viewportWidth, viewportHeight } }, { log: false })
38+
})
39+
40+
cy.visit('index.html')
41+
cy.screenshot('page', { capture: 'runner' })
42+
})
43+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-disable no-console */
2+
module.exports = function (on, config) {
3+
// configure plugins here
4+
on('task', {
5+
log ({ message, o }) {
6+
console.log(message)
7+
if (o) {
8+
console.log(JSON.stringify(o, null, 2))
9+
}
10+
11+
return null
12+
},
13+
})
14+
15+
// let's increase the browser window size when running headlessly
16+
// this will produce higher resolution images and videos
17+
// https://on.cypress.io/browser-launch-api
18+
on('before:browser:launch', (browser = {}, launchOptions) => {
19+
console.log('launching browser %s is headless? %s', browser.name, browser.isHeadless)
20+
21+
// the browser width and height we want to get
22+
// our screenshots and videos will be of that resolution
23+
const width = 1920
24+
const height = 1080
25+
26+
console.log('setting the browser window size to %d x %d', width, height)
27+
28+
if (browser.name === 'chrome' && browser.isHeadless) {
29+
launchOptions.args.push(`--window-size=${width},${height}`)
30+
31+
// force screen to be non-retina and just use our given resolution
32+
launchOptions.args.push('--force-device-scale-factor=1')
33+
}
34+
35+
if (browser.name === 'electron' && browser.isHeadless) {
36+
// might not work on CI for some reason
37+
launchOptions.preferences.width = width
38+
launchOptions.preferences.height = height
39+
}
40+
41+
if (browser.name === 'firefox' && browser.isHeadless) {
42+
launchOptions.args.push(`--width=${width}`)
43+
launchOptions.args.push(`--height=${height}`)
44+
}
45+
46+
return launchOptions
47+
})
48+
}
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html lang="en">
2+
<head>
3+
<style>
4+
* {
5+
margin: 0
6+
}
7+
body {
8+
background: url('200px.png');
9+
}
10+
</style>
11+
</head>
12+
<body>
13+
</body>
14+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "window-size",
3+
"version": "1.0.0",
4+
"description": "How to control the browser window size",
5+
"private": true,
6+
"scripts": {
7+
"cypress:open": "../../node_modules/.bin/cypress open",
8+
"cypress:run": "../../node_modules/.bin/cypress run",
9+
"test:ci": "../../node_modules/.bin/cypress run --headless --browser chrome",
10+
"test:ci:record": "../../node_modules/.bin/cypress run --record --headless --browser chrome"
11+
}
12+
}

0 commit comments

Comments
 (0)