Skip to content

Commit 9afa0ee

Browse files
authored
[SDK-3090] Remote screenshotter service support (#1)
* Initial approach for remote screenshotter service support * Committing dist as a way to reference this branch directly * Use document when there's no subject * Update dist * eRwork chaining * Cleeanup for merge * Document my work * Minimilize workflow for use-case and lack of secrets * Minimilize workflow for use-case and lack of secrets * Unrequire removed step
1 parent ad60d4a commit 9afa0ee

File tree

4 files changed

+58
-60
lines changed

4 files changed

+58
-60
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: CI
22
on:
33
pull_request_target:
44
branches:
@@ -17,7 +17,7 @@ jobs:
1717
- uses: actions/checkout@v3
1818
- uses: actions/setup-node@v3
1919
with:
20-
node-version: '16.x'
20+
node-version: "16.x"
2121
- name: Remove git auth
2222
run: git config --unset http.https://github.com/.extraheader
2323
- name: Generate yarn cache-key
@@ -52,7 +52,7 @@ jobs:
5252
- uses: actions/checkout@v3
5353
- uses: actions/setup-node@v3
5454
with:
55-
node-version: '16.x'
55+
node-version: "16.x"
5656
- name: remove git auth
5757
run: git config --unset http.https://github.com/.extraheader
5858
- name: Configure Yarn cache
@@ -79,7 +79,7 @@ jobs:
7979
- uses: actions/checkout@v3
8080
- uses: actions/setup-node@v3
8181
with:
82-
node-version: '16.x'
82+
node-version: "16.x"
8383
- name: remove git auth
8484
run: git config --unset http.https://github.com/.extraheader
8585
- name: Configure Yarn cache
@@ -102,41 +102,10 @@ jobs:
102102
- name: Test component-testing
103103
run: yarn test:ct:ci
104104

105-
test-integration-coverage:
106-
name: test-integration-coverage
107-
runs-on: ubuntu-latest
108-
needs: cache
109-
steps:
110-
- uses: actions/checkout@v3
111-
- uses: actions/setup-node@v3
112-
with:
113-
node-version: '16.x'
114-
- name: remove git auth
115-
run: git config --unset http.https://github.com/.extraheader
116-
- name: Configure Yarn cache
117-
uses: actions/cache@v3
118-
with:
119-
key: ${{ needs.cache.outputs.yarn-cache-key }}
120-
path: |
121-
~/.cache/Cypress
122-
.yarn
123-
node_modules
124-
example/node_modules
125-
- name: Install dependencies
126-
run: yarn --immutable
127-
- name: Test integration (with coverage)
128-
uses: paambaati/codeclimate-action@93c6213edc13fcb74a684a39a32956ebf417dd1c
129-
env:
130-
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
131-
with:
132-
coverageCommand: yarn test:integration:ci
133-
coverageLocations: |
134-
${{github.workspace}}/coverage/lcov.info:lcov
135-
136105
build-and-release:
137106
name: build and release
138107
runs-on: ubuntu-latest
139-
needs: [cache, lint, test, test-integration-coverage]
108+
needs: [cache, lint, test]
140109
if: ${{ github.actor != 'dependabot[bot]' && github.ref == 'refs/heads/main' && github.event_name == 'push' }}
141110
steps:
142111
- uses: actions/checkout@v3
@@ -149,7 +118,7 @@ jobs:
149118
git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)"
150119
- uses: actions/setup-node@v3
151120
with:
152-
node-version: '16.x'
121+
node-version: "16.x"
153122
- name: remove git auth
154123
run: git config --unset http.https://github.com/.extraheader
155124
- name: Configure Yarn cache
@@ -163,15 +132,7 @@ jobs:
163132
example/node_modules
164133
- name: Install dependencies
165134
run: yarn --immutable
166-
- name: Authenticate with Registry
167-
run: |
168-
yarn npm logout
169-
echo "registry=http://registry.npmjs.org/" >> .npmrc
170-
echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> .npmrc
171-
npm whoami
172-
env:
173-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
174-
- name: release package
135+
- name: release to branch
175136
if: ${{ success() }}
176137
run: yarn release:ci
177138
env:

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ cy.matchImage({
184184
// maximum threshold above which the test should fail
185185
// default: 0.01
186186
maxDiffThreshold: 0.1,
187-
// forces scale factor to be set as value "1"
187+
// forces scale factor to be set as value "1"
188188
// helps with screenshots being scaled 2x on high-density screens like Mac Retina
189189
// default: true
190190
forceDeviceScaleFactor: false,
@@ -195,6 +195,10 @@ cy.matchImage({
195195
// instead of checking against the image from previous run
196196
// default: undefined
197197
matchAgainstPath: '/path/to/reference-image.png'
198+
// pass the URL of an http service that accepts POST with a body like {"html"=>"<html>hello world</html>"} and
199+
// responds with screenshotted image. This can be useful for eliminating rendering differences across operating systems.
200+
// default: undefined
201+
remoteScreenshotServiceUrl: 'https://example.com/screenshotter/render'
198202
})
199203
```
200204

@@ -215,7 +219,7 @@ export default defineConfig({
215219
}
216220
})
217221
{
218-
222+
219223
}
220224
```
221225

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"prettify:ci": "prettier --check src",
4444
"prepack": "yarn build",
4545
"release": "semantic-release",
46-
"release:ci": "yarn release --yes",
46+
"release:ci": "yarn release --yes --skip-npm",
4747
"release:test": "yarn release --no-git-tag-version --no-push --skip-npm",
4848
"test": "cd example && yarn test:ct:ci && yarn test:e2e:ci && yarn test:integration:ci",
4949
"test:integration": "vitest run",

src/commands.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ declare global {
88
namespace Cypress {
99
type MatchImageOptions = {
1010
screenshotConfig?: Partial<Cypress.ScreenshotDefaultsOptions>;
11+
remoteScreenshotServiceUrl?: string;
1112
diffConfig?: Parameters<typeof pixelmatch>[5];
1213
updateImages?: boolean;
1314
/**
@@ -90,6 +91,12 @@ export const getConfig = (options: Cypress.MatchImageOptions) => {
9091
| Partial<Cypress.ScreenshotDefaultsOptions>
9192
| undefined) ||
9293
{},
94+
remoteScreenshotServiceUrl:
95+
options.remoteScreenshotServiceUrl ||
96+
(Cypress.env("pluginVisualRegressionRemoteScreenshotServiceUrl") as
97+
| string
98+
| undefined) ||
99+
{},
93100
};
94101
};
95102

@@ -107,6 +114,7 @@ Cypress.Commands.add(
107114
maxDiffThreshold,
108115
diffConfig,
109116
screenshotConfig,
117+
remoteScreenshotServiceUrl,
110118
} = getConfig(options);
111119

112120
return cy
@@ -124,17 +132,42 @@ Cypress.Commands.add(
124132
)
125133
.then(({ screenshotPath, title: titleFromTask }) => {
126134
title = titleFromTask;
127-
let imgPath: string;
128-
return ($el ? cy.wrap($el) : cy)
129-
.screenshot(screenshotPath, {
130-
...screenshotConfig,
131-
onAfterScreenshot(el, props) {
132-
imgPath = props.path;
133-
screenshotConfig.onAfterScreenshot?.(el, props);
134-
},
135-
log: false,
136-
})
137-
.then(() => imgPath);
135+
136+
if (remoteScreenshotServiceUrl) {
137+
return cy
138+
.document()
139+
.then((doc) => {
140+
return cy
141+
.request({
142+
url: remoteScreenshotServiceUrl,
143+
method: "POST",
144+
encoding: "binary",
145+
body: {
146+
html: ($el?.html() || doc.body.parentElement?.innerHTML),
147+
},
148+
} as Cypress.RequestOptions)
149+
.then((response) => {
150+
return cy.writeFile(
151+
screenshotPath as string,
152+
response.body,
153+
"binary"
154+
);
155+
});
156+
})
157+
.then(() => screenshotPath as string);
158+
} else {
159+
let imgPath: string;
160+
return ($el ? cy.wrap($el) : cy)
161+
.screenshot(screenshotPath, {
162+
...screenshotConfig,
163+
onAfterScreenshot(el, props) {
164+
imgPath = props.path;
165+
screenshotConfig.onAfterScreenshot?.(el, props);
166+
},
167+
log: false,
168+
})
169+
.then(() => imgPath);
170+
}
138171
})
139172
.then((imgPath) =>
140173
cy

0 commit comments

Comments
 (0)