Skip to content

Commit 357bcbb

Browse files
committed
test(e2e): change PDF approach to work with headless browsers
1 parent cbadbdc commit 357bcbb

14 files changed

Lines changed: 223 additions & 80 deletions

.github/workflows/e2e.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,26 @@ jobs:
255255
env:
256256
CYPRESS_TEST_FILES: ${{ matrix.files }}
257257

258+
- name: Archive logs
259+
uses: actions/upload-artifact@v3
260+
with:
261+
name: logs
262+
path: logs
263+
if: ${{ failure() }}
264+
258265
- name: Archive test screenshots
259266
uses: actions/upload-artifact@v3
260267
with:
261268
name: screenshots
262269
path: test/cypress/screenshots
263270
if: ${{ failure() }}
264271

272+
- name: Archive download folder
273+
uses: actions/upload-artifact@v3
274+
with:
275+
name: downloads
276+
path: test/cypress/downloads
277+
if: ${{ failure() }}
278+
265279
- name: Report Coverage
266280
run: npm run test:coverage

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ node_modules
55
npm-debug.log.*
66
./report.*.json
77
*.log
8+
logs
89
yarn.lock
910
.DS_Store
1011
build

components/CreateGiftCardsSuccess.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default class CreateGiftCardsSuccess extends React.Component {
6868
};
6969

7070
renderManualSuccess() {
71-
const filename = `${this.props.collectiveSlug}-giftcards-${Date.now()}.pdf`;
71+
const filename = `${this.props.collectiveSlug}-giftcards.pdf`;
7272
const downloadUrl = giftCardsDownloadUrl(filename);
7373

7474
return (

components/transactions/TransactionDetails.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ const TransactionDetails = ({ displayActions, transaction, onMutationSuccess })
271271
{showDownloadInvoiceButton && (
272272
<StyledButton
273273
buttonSize="small"
274+
data-loading={loadingInvoice}
274275
loading={loadingInvoice}
275276
onClick={downloadInvoiceWith({
276277
transactionUuid: uuid,

cypress.config.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// eslint-disable-next-line node/no-unpublished-require
22
const { defineConfig } = require('cypress');
3-
const { readPdf } = require('./test/cypress/scripts/read-pdf.ts');
4-
const { listFiles } = require('./test/cypress/scripts/list-files.ts');
3+
const { getTextFromPdfContent } = require('./test/cypress/scripts/get-text-from-pdf-content.ts');
54

65
module.exports = defineConfig({
76
experimentalMemoryManagement: true,
@@ -41,8 +40,7 @@ module.exports = defineConfig({
4140
console.log(...message); // eslint-disable-line no-console
4241
return null;
4342
},
44-
readPdf,
45-
listFiles,
43+
getTextFromPdfContent,
4644
});
4745

4846
config.baseUrl = process.env.WEBSITE_URL || 'http://localhost:3000';

package-lock.json

Lines changed: 165 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/run_e2e_tests.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
mkdir -p logs
4+
35
echo "> Starting maildev server"
46
npx maildev@2.0.5 &
57
MAILDEV_PID=$!
@@ -35,7 +37,7 @@ if [ -z "$IMAGES_FOLDER" ]; then
3537
else
3638
cd $IMAGES_FOLDER
3739
fi
38-
npm start &
40+
npm start >logs/images-service.txt 2>&1 &
3941
IMAGES_PID=$!
4042
cd -
4143

@@ -45,7 +47,7 @@ if [ -z "$PDF_FOLDER" ]; then
4547
else
4648
cd $PDF_FOLDER
4749
fi
48-
PORT=3002 npm start &
50+
PORT=3002 npm start >logs/pdf-service.txt 2>&1 &
4951
PDF_PID=$!
5052
cd -
5153

@@ -92,6 +94,10 @@ npx cypress@12.7.0 run ${CYPRESS_RECORD} --env OC_ENV=$OC_ENV --spec "test/cypre
9294
RETURN_CODE=$?
9395
if [ $RETURN_CODE -ne 0 ]; then
9496
echo "Error with cypress e2e tests, exiting"
97+
pwd
98+
ls
99+
ls test/cypress
100+
ls test/cypress/downloads
95101
exit 1
96102
fi
97103
echo ""

test/cypress/integration/02-collective.transactions.test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ describe('collective.transactions', () => {
2121
cy.login({ redirect: '/brusselstogether/transactions' });
2222
cy.contains('button[data-cy=transaction-details]', 'View Details').first().click();
2323
cy.getByDataCy('download-transaction-receipt-btn').first().click();
24-
cy.waitForDownload('brusselstogether_2017-12-04_b961becd-cb85-6c70-6ec5-075151203084.pdf').then(file => {
25-
cy.task('readPdf', file)
26-
.should('contain', 'BrusselsTogether ASBL') // Bill from
27-
.should('contain', 'Frederik') // Bill to
28-
.should('contain', 'brusselstogetherasbl_b961becd-cb85-6c70-6ec5-075151203084')
29-
.should('contain', `Contribution #1037`)
30-
.should('contain', '2017-12-04')
31-
.should('contain', 'monthly recurring subscription')
32-
.should('contain', '$10.00');
33-
});
24+
cy.getByDataCy('download-transaction-receipt-btn').first().should('have.attr', 'data-loading', 'true'); // Downloading
25+
cy.getByDataCy('download-transaction-receipt-btn').first().should('have.attr', 'data-loading', 'false'); // Downloaded
26+
const filename = 'brusselstogether_2017-12-04_b961becd-cb85-6c70-6ec5-075151203084.pdf';
27+
cy.getDownloadedPDFContent(filename)
28+
.should('contain', 'BrusselsTogether ASBL') // Bill from
29+
.should('contain', 'Frederik') // Bill to
30+
.should('contain', 'brusselstogetherasbl_b961becd-cb85-6c70-6ec5-075151203084')
31+
.should('contain', `Contribution #1037`)
32+
.should('contain', '2017-12-04')
33+
.should('contain', 'monthly recurring subscription')
34+
.should('contain', '$10.00');
3435
});
3536
});

test/cypress/integration/09-giftcards-admin.test.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ describe('Gift cards admin', () => {
3838
});
3939

4040
// Download the PDF
41+
// Mock date to make sure we have the same filename
4142
cy.getByDataCy('download-gift-cards-btn').click();
42-
const fileRegex = new RegExp(`${collectiveSlug}-giftcards-\\d+\\.pdf$`);
43-
cy.waitForDownload(fileRegex).then(file => {
44-
cy.task('readPdf', file).should('contain', '$542.00 Gift Card from TestOrg');
45-
});
43+
const filename = `${collectiveSlug}-giftcards.pdf`;
44+
cy.getDownloadedPDFContent(filename).should('contain', '$542.00 Gift Card from TestOrg');
4645

4746
// Links should also be added to gift cards list
4847
cy.contains('a[href$="/admin/gift-cards"]', 'Back to Gift Cards list').click();

test/cypress/integration/27-expenses.test.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,12 @@ describe('New expense flow', () => {
561561
it('Downloads PDF', () => {
562562
cy.visit(expenseUrl);
563563
cy.getByDataCy('download-expense-invoice-btn').click();
564-
const fileRegex = new RegExp(`Expense-${expense.legacyId}-.*.pdf`);
565-
cy.waitForDownload(fileRegex).then(file => {
566-
cy.task('readPdf', file)
567-
.should('contain', `Expense #${expense.legacyId}: Expense for E2E tests`)
568-
.should('contain', 'Collective: Test Collective')
569-
.should('contain', '$10.00');
570-
});
564+
const date = new Date(expense.createdAt).toISOString().split('T')[0];
565+
const filename = `Expense-${expense.legacyId}-${collective.slug}-invoice-${date}.pdf`;
566+
cy.getDownloadedPDFContent(filename)
567+
.should('contain', `Expense #${expense.legacyId}: Expense for E2E tests`)
568+
.should('contain', 'Collective: Test Collective')
569+
.should('contain', '$10.00');
571570
});
572571

573572
it('Approve, unapprove, reject and pay actions on expense', () => {

0 commit comments

Comments
 (0)