Skip to content

Commit f3d7070

Browse files
authored
Test runner v3.7.0 recipes (#368)
* add custom Brave browser example * add config-file * update test names * add --browser brave * ughh it is cypress run, not cypress open on CI * test new 3.7.0 linux build ```json { "platform": "linux", "arch": "x64", "env": { "CYPRESS_INSTALL_BINARY": "cdn.cypress.io/beta/binary/3.7.0/linux-x64/circle-develop-b03b25c258990966cbc99e50796c039abbf2f893-194641/cypress.zip" }, "packages": "cdn.cypress.io/beta/npm/3.7.0/circle-develop-b03b25c258990966cbc99e50796c039abbf2f893-194639/cypress.tgz", "branch": "3.7.0", "commit": "b03b25c258990966cbc99e50796c039abbf2f893", "status": { "owner": "cypress-io", "repo": "cypress", "sha": "b03b25c258990966cbc99e50796c039abbf2f893", "platform": "linux", "arch": "x64", "context": "[linux-x64] cypress-test-example-repos" } } ``` * test new 3.7.0 linux build ```json { "platform": "linux", "arch": "x64", "env": { "CYPRESS_INSTALL_BINARY": "cdn.cypress.io/beta/binary/3.7.0/linux-x64/circle-develop-b03b25c258990966cbc99e50796c039abbf2f893-194641/cypress.zip" }, "packages": "cdn.cypress.io/beta/npm/3.7.0/circle-develop-b03b25c258990966cbc99e50796c039abbf2f893-194639/cypress.tgz", "branch": "3.7.0", "commit": "b03b25c258990966cbc99e50796c039abbf2f893", "status": { "owner": "cypress-io", "repo": "cypress", "sha": "b03b25c258990966cbc99e50796c039abbf2f893", "platform": "linux", "arch": "x64", "context": "[linux-x64] cypress-test-example-repos" } } ``` * test with https ```json { "platform": "linux", "arch": "x64", "env": { "CYPRESS_INSTALL_BINARY": "https://cdn.cypress.io/beta/binary/3.7.0/linux-x64/circle-develop-b03b25c258990966cbc99e50796c039abbf2f893-194641/cypress.zip" }, "packages": "https://cdn.cypress.io/beta/npm/3.7.0/circle-develop-b03b25c258990966cbc99e50796c039abbf2f893-194639/cypress.tgz", "branch": "3.7.0", "commit": "b03b25c258990966cbc99e50796c039abbf2f893", "status": { "owner": "cypress-io", "repo": "cypress", "sha": "b03b25c258990966cbc99e50796c039abbf2f893", "platform": "linux", "arch": "x64", "context": "[linux-x64] cypress-test-example-repos" } } ```
1 parent 5896ae6 commit f3d7070

File tree

13 files changed

+179
-1
lines changed

13 files changed

+179
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Recipe | Description
1313
[Adding Custom Commands](./examples/fundamentals__add-custom-command) | Write your own custom commands with correct types
1414
[Adding Chai Assertions](./examples/extending-cypress__chai-assertions) | Add new or custom chai assertions with correct types
1515
[Cypress module API](./examples/fundamentals__module-api) | Run Cypress via its module API
16+
[Custom browsers](./examples/fundamentals_custom-browsers) | Control which browsers the project can use, or even add a custom browser into the list
1617

1718
## Testing the DOM
1819

circle.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ jobs:
8989
# is in variable CIRCLE_NODE_TOTAL and the current container index in CIRCLE_NODE_INDEX
9090
# which starts at 0 and goes to $CIRCLE_NODE_TOTAL - 1
9191
- run: npm run test:ci -- --chunk $CIRCLE_NODE_INDEX --total-chunks $CIRCLE_NODE_TOTAL
92+
9293
# a single utility script to run multiple examples against Chrome
9394
test-examples-chrome:
9495
parallelism: 10
@@ -100,6 +101,18 @@ jobs:
100101
at: ~/
101102
- run: npm run test:ci:chrome -- --chunk $CIRCLE_NODE_INDEX --total-chunks $CIRCLE_NODE_TOTAL
102103

104+
# a single utility script to run multiple examples against Brave browser
105+
test-examples-brave:
106+
# only one recipe shows how to run Cypress using Brave browser
107+
parallelism: 1
108+
working_directory: ~/app
109+
docker:
110+
- image: cypress/browsers:node12.13.0-chrome78-ff70-brave78
111+
steps:
112+
- attach_workspace:
113+
at: ~/
114+
- run: npm run test:ci:brave -- --chunk $CIRCLE_NODE_INDEX --total-chunks $CIRCLE_NODE_TOTAL
115+
103116
# define a new job with defaults for each "examples/*" subfolder
104117
blogs__a11y:
105118
<<: *defaults
@@ -211,6 +224,15 @@ all_jobs: &all_jobs
211224
only:
212225
- master
213226

227+
- test-examples-brave:
228+
requires:
229+
- build
230+
filters:
231+
branches:
232+
only:
233+
- master
234+
- 3.7.0
235+
214236
# when adding new example subfolder with a recipe
215237
# add its name here to "create" CircleCI job
216238

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Custom browsers
2+
3+
Cypress v3.7.0+ allows the project to customize the list of browsers. In this example, [cypress/plugins/index.js](cypress/plugins/index.js) filters the list of all browsers detected on the user's machine and only returns the bundled Electron browser. Or you could do the opposite and only allow the project to use Chrome browsers
4+
5+
```js
6+
// cypress/plugins/index.js
7+
module.exports = (on, config) => {
8+
return {
9+
browsers: config.browsers.filter((browser) => browser.family === 'chrome'),
10+
}
11+
}
12+
```
13+
14+
![Only Chrome browsers displayed](images/only-chrome.png)
15+
16+
## Custom browser
17+
18+
Plugins file can also adjust or overwrite the list of browsers. For example [cypress/plugins/brave.js](cypress/plugins/brave.js) file finds the Brave Browser and returns only that browser.
19+
20+
```shell
21+
npx cypress open --config pluginsFile=cypress/plugins/brave.js
22+
```
23+
24+
![Brave browser running tests](images/brave.png)
25+
26+
If you want to use Brave browser during non-interactive run, pass its name
27+
28+
```shell
29+
npx cypress run --config pluginsFile=cypress/plugins/brave.js --browser brave
30+
```
31+
32+
**Tip:** if you are passing lots of `--config` options, set them in a new configuration file, like `cypress-brave.json` and use
33+
34+
```shell
35+
npx cypress run --config-file cypress-brave.json
36+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"supportFile": false,
3+
"fixturesFolder": false
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// <reference types="cypress" />
2+
it('works', () => {
3+
cy.log(
4+
`Running in browser **${Cypress.browser.displayName} v${
5+
Cypress.browser.majorVersion
6+
}**`
7+
)
8+
cy.task('echo', { browser: Cypress.browser })
9+
})
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* eslint-disable no-console */
2+
const os = require('os')
3+
const execa = require('execa')
4+
const tasks = require('./tasks')
5+
6+
/**
7+
* Parses STDOUT from "brave-browser --version" command.
8+
* @param {string} stdout from Brave, something like "Brave Browser 77.0.69.135"
9+
*/
10+
const parseBraveVersion = (stdout) => {
11+
const [, version] = /Brave Browser (\d+\.\d+\.\d+\.\d+)/.exec(stdout)
12+
13+
console.log('Brave browser version is', version)
14+
const majorVersion = parseFloat(version.split('.')[0])
15+
16+
return { version, majorVersion }
17+
}
18+
19+
const findBraveBrowserInfo = (browserPath) => {
20+
return execa(browserPath, ['--version'])
21+
.then((result) => result.stdout)
22+
.then(parseBraveVersion)
23+
.then(({ version, majorVersion }) => {
24+
return {
25+
name: 'brave',
26+
family: 'chrome',
27+
displayName: 'Brave',
28+
version,
29+
path: browserPath,
30+
majorVersion,
31+
}
32+
})
33+
}
34+
35+
const findBraveBrowser = () => {
36+
// the path is hard-coded for simplicity
37+
switch (os.platform()) {
38+
case 'darwin':
39+
return findBraveBrowserInfo(
40+
'/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
41+
)
42+
case 'linux':
43+
return findBraveBrowserInfo('brave-browser')
44+
default:
45+
throw new Error(`Cannot find Brave browser on platform ${os.platform()}`)
46+
}
47+
}
48+
49+
module.exports = (on, config) => {
50+
on('task', tasks)
51+
52+
// only filter browsers if we are passed a list - this way
53+
// this example works on Cypress v3.7.0+ and below
54+
if (!config.browsers) {
55+
return
56+
}
57+
58+
// Cypress v3.7.0+
59+
return findBraveBrowser().then((browser) => {
60+
return {
61+
browsers: [browser],
62+
}
63+
})
64+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const tasks = require('./tasks')
2+
3+
module.exports = (on, config) => {
4+
on('task', tasks)
5+
6+
// only filter browsers if we are passed a list - this way
7+
// this example works on Cypress v3.7.0+ and below
8+
if (!config.browsers) {
9+
return
10+
}
11+
12+
// Cypress v3.7.0+
13+
return {
14+
browsers: config.browsers.filter((browser) => browser.family === 'electron'),
15+
}
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* eslint-disable no-console */
2+
module.exports = {
3+
echo (what) {
4+
console.log(what)
5+
6+
return null
7+
},
8+
}
Loading
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "cypress-custom-browsers",
3+
"version": "1.0.0",
4+
"description": "Customizes list of browsers",
5+
"scripts": {
6+
"start": "echo nothing to start",
7+
"cypress:run": "../../node_modules/.bin/cypress run",
8+
"cypress:open": "../../node_modules/.bin/cypress open",
9+
"test:ci": "../../node_modules/.bin/cypress run",
10+
"test:ci:brave": "../../node_modules/.bin/cypress run --config pluginsFile=cypress/plugins/brave.js --browser brave"
11+
}
12+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"warn-only": "stop-only --warn --folder examples --skip node_modules",
1515
"test:ci": "node ./test-examples",
1616
"test:ci:windows": "node ./test-examples --windows",
17-
"test:ci:chrome": "node ./test-examples --chrome"
17+
"test:ci:chrome": "node ./test-examples --chrome",
18+
"test:ci:brave": "node ./test-examples --brave"
1819
},
1920
"repository": {
2021
"type": "git",

test-examples.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const is = require('check-more-types')
1818
// run this script with "--chrome" CLI flag
1919
const args = arg({
2020
'--chrome': Boolean,
21+
'--brave': Boolean,
2122
'--windows': Boolean,
2223
'--chunk': Number,
2324
'--total-chunks': Number,
@@ -35,6 +36,10 @@ if (args['--chrome']) {
3536
scriptName = 'test:ci:chrome'
3637
}
3738

39+
if (args['--brave']) {
40+
scriptName = 'test:ci:brave'
41+
}
42+
3843
if (args['--windows']) {
3944
scriptName = 'test:ci:windows'
4045
}

0 commit comments

Comments
 (0)