Cypress
Example of running Cypress tests and saving their results on the Apify platform. JSON results are saved to Dataset, videos to Key-value store.
src/main.js
cypress/e2e/first-spec.cy.js
cypress/e2e/second-spec.cy.js
cypress/support/e2e.js
1// Apify SDK - toolkit for building Apify Actors (Read more at https://docs.apify.com/sdk/js/)
2import { Actor } from 'apify';
3import fs from 'fs';
4
5// Component and E2E testing library (read more at https://docs.cypress.io/guides/overview/why-cypress)
6import cypress from 'cypress';
7// File system traversing library (read more at https://www.jsdocs.io/package/globby)
8import { globby } from 'globby';
9// Apify logging utility library
10import log from '@apify/log';
11// Library for console logging tables
12import 'console.table';
13
14// this is ESM project, and as such, it requires you to specify extensions in your relative imports
15// read more about this here: https://nodejs.org/docs/latest-v18.x/api/esm.html#mandatory-file-extensions
16// import { router } from './routes.js';
17
18// Helper function to run tests from specific test file with given configuration from INPUT.json
19const runOneSpec = (spec) => cypress.run({ config: input, spec });
20
21// The init() call configures the Actor for its environment. It's recommended to start every Actor with an init()
22await Actor.init();
23
24// Define the configuration to start the cypress test with - get it from the input of the Actor or use a default config.
25const input = await Actor.getInput() || { baseUrl: "https://apify.com", video: true };
26log.info(`Running tests with following input: ${JSON.stringify(input)}`);
27
28// Get cypress test files from ./cypress/e2e that end with spec.cy.js
29const tests = await globby('./cypress/e2e/*-spec.cy.js');
30log.info(`Getting tests: ${tests}`);
31
32const testsResultsSummary = [];
33
34// Opens an instance of Key-value store (read more https://docs.apify.com/sdk/js/reference/class/Actor#openKeyValueStore)
35const kvs = await Actor.openKeyValueStore();
36// Opens an instance of Dataset (read more https://docs.apify.com/sdk/js/reference/class/Actor#openDataset)
37const dataset = await Actor.openDataset();
38
39// Run tests one by one
40for (const test of tests) {
41 const result = await runOneSpec(test);
42 let keyValueStoreLink;
43 // Save video recording of the test to Key-value store
44 if (result?.config?.video) {
45 const baseName = test.split('/').pop();
46 const file = `./cypress/videos/${baseName}.mp4`;
47 const kvsKeyName = baseName.replaceAll('.', '-');
48 // Saves video to Key-value store under kvsKeyName (read more at https://docs.apify.com/sdk/js/reference/class/KeyValueStore#setValue)
49 await kvs.setValue(kvsKeyName, fs.readFileSync(file), { contentType: 'video/mp4' });
50 keyValueStoreLink = kvs.getPublicUrl(kvsKeyName);
51 }
52
53 // Transform the test results
54 const transformedResult = {
55 testSuiteTitle: result.runs[0].tests ? result.runs[0].tests[0].title[0] : result.runs[0].spec.baseName,
56 totalPassed: result.totalPassed,
57 totalPending: result.totalPending,
58 totalFailed: result.totalFailed,
59 totalSkipped: result.totalSkipped,
60 totalDuration: result.totalDuration,
61 videoLink: keyValueStoreLink,
62 rawData: result,
63 };
64
65 // Save transformed test results to Dataset (read more at https://docs.apify.com/sdk/js/reference/class/Dataset#pushData)
66 await dataset.pushData(transformedResult);
67 testsResultsSummary.push(transformedResult);
68}
69
70// Create a loggable test summary
71const summary = testsResultsSummary
72 .map((test) => {
73 return {
74 spec: test?.testSuiteTitle,
75 passes: test?.totalPassed,
76 failures: test?.totalFailed,
77 pending: test?.totalPending,
78 skipped: test?.totalSkipped,
79 duration: test?.totalDuration,
80 };
81 });
82
83// Log the summary as a table
84console.table('Summary', summary);
85
86// Gracefully exit the Actor process. It's recommended to quit all Actors with an exit()
87await Actor.exit();
Cypress test template
Run your Cypress tests on the Apify Platform effectively and easily. The template provides the necessary setup for running the Cypress tests. You can change the Cypress configuration in the input, which is defined by the input schema. The template uses globby library to fetch the Cypress test files and run them within the Actor. The video recordings are stored in Key-value store and the comprehensive test results are stored in the Dataset.
Included features
- Apify SDK - toolkit for building Actors
- Input schema - define and easily validate a schema for your Actor's input
- Dataset - store structured data where each object stored has the same attributes
- Cypress - JavaScript Component and E2E testing library
How it works
You can easily run your tests on the Apify Platform, just copy-paste your test files into cypress/e2e
folder. The tests' names need to end with -spec.cy.js
.
You can also customize the test run by specifying other options in the input, e.g. the screen size, video recording, or the default command timeout.
After running the tests, the Apify platform stores the results in a comprehensive way - datasets for JSON results, and key-value store for videos. You can view the results directly on the platform or download them to your local machine using a REST API.
Resources on Cypress test
- Cypress test runner Actor
- How to deploy tests to the Apify platform
- Video guide on getting data using Apify API
- Integration with Zapier, Make, Google Drive and others
- Video guide on getting data using Apify API
- A short guide on how to create Actors using code templates:
Scrape single page with provided URL with Axios and extract data from page's HTML with Cheerio.
A scraper example that uses Cheerio to parse HTML. It's fast, but it can't run the website's JavaScript or pass JS anti-scraping challenges.
Example of a Puppeteer and headless Chrome web scraper. Headless browsers render JavaScript and are harder to block, but they're slower than plain HTTP.
Web scraper example with Crawlee, Playwright and headless Chrome. Playwright is more modern, user-friendly and harder to block than Puppeteer.
Skeleton project that helps you quickly bootstrap `CheerioCrawler` in JavaScript. It's best for developers who already know Apify SDK and Crawlee.
Empty template with basic structure for the Actor with Apify SDK that allows you to easily add your own functionality.