This is a comprehensive testing framework built with Cypress to automate testing for Hyperswitch. The framework supports API testing with features like multiple credential management, configuration management, global state handling, and extensive utility functions. The framework provides extensive support for API testing with advanced features including:
- Multiple credential management
- Dynamic configuration management
- Global state handling
- Extensive utility functions
- Parallel test execution
- Connector-specific implementations
- Overview
- Table of Contents
- Quick Start
- Getting Started
- Test reports
- Folder structure
- Adding tests
- Debugging
- Linting
- Best Practices
- Additional Resources
- Contributing
- Appendix
For experienced users who want to get started quickly:
git clone https://github.com/juspay/hyperswitch.git
cd hyperswitch/cypress-tests
npm ci
# connector_id must be replaced with the connector name that is being tested (e.g. stripe, paypal, etc.)
CYPRESS_CONNECTOR="connector_id" npm run cypress:ci
- Node.js (18.x or above)
- npm or yarn
- Hyperswitch development environment
Note
To learn about the hardware requirements and software dependencies for running Cypress, refer to the official documentation.
-
Clone the repository and switch to the project directory:
git clone https://github.com/juspay/hyperswitch.git cd hyperswitch/cypress-tests
-
Install Cypress and its dependencies to
cypress-tests
directory by running the following command:npm ci
Once installed, verify the installation by running:
npx cypress --version
To learn about the supported commands, execute:
npm run
-
Set up the cards database:
psql --host=localhost --port=5432 --username=db_user --dbname=hyperswitch_db --command "\copy cards_info FROM '.github/data/cards_info.csv' DELIMITER ',' CSV HEADER;"
-
Set environment variables for cypress
export CYPRESS_CONNECTOR="connector_id" export CYPRESS_BASEURL="base_url" export DEBUG=cypress:cli export CYPRESS_ADMINAPIKEY="admin_api_key" export CYPRESS_CONNECTOR_AUTH_FILE_PATH="path/to/creds.json"
Tip
It is recommended to install direnv and use a .envrc
file to store these environment variables with cypress-tests
directory. This will make it easier to manage environment variables while working with Cypress tests.
Note
To learn about how creds
file should be structured, refer to the example.creds.json section below.
Execution of Cypress tests can be done in two modes: Development mode (Interactive) and CI mode (Headless). The tests can be executed against a single connector or multiple connectors in parallel. Time taken to execute the tests will vary based on the number of connectors and the number of tests. For a single connector, the tests will take approximately 07-12 minutes to execute (this also depends on the hardware configurations).
For Development mode, the tests will run in the Cypress UI where execution of tests can be seen in real-time and provides a larger area for debugging based on the need. In CI mode (Headless), tests run in the terminal without UI interaction and generate reports automatically.
npm run cypress
# All tests
npm run cypress:ci
# Specific test suites
npm run cypress:misc # Miscellaneous tests (e.g. health check, memory cache etc.)
npm run cypress:payment-method-list # Payment method list tests
npm run cypress:payments # Payment tests
npm run cypress:payouts # Payout tests
npm run cypress:routing # Routing tests
-
Set additional environment variables:
export PAYMENTS_CONNECTORS="payment_connector_1 payment_connector_2 payment_connector_3 payment_connector_4" export PAYOUTS_CONNECTORS="payout_connector_1 payout_connector_2 payout_connector_3" export PAYMENT_METHOD_LIST="" export ROUTING=""
-
In terminal, execute:
source .env ../scripts/execute_cypress.sh
Optionally,
--parallel <jobs (integer)>
can be passed to run cypress tests in parallel. By default, whenparallel
command is passed, it will be run in batches of5
.
The test reports are generated in the cypress/reports
directory. The reports are generated in the mochawesome
format and can be viewed in the browser.
These reports does include:
- screenshots of the failed tests
- HTML and JSON reports
The folder structure of this directory is as follows:
.
├── .prettierrc # prettier configs
├── README.md # this file
├── cypress
│ ├── e2e
│ │ ├── configs # Directory for utility functions related to connectors.
│ │ │ ├── PaymentMethodList
│ │ │ │ ├── connector_<1>.js
│ │ │ │ ├── ...
│ │ │ │ └── connector_<n>.js
│ │ │ ├── Payment
│ │ │ ├── Payout
│ │ │ └── Routing
│ │ └── spec # Directory for test scenarios related to connectors.
│ │ ├── Misc
│ │ │ ├── 00000-test_<0>.cy.js
│ │ │ ├── ...
│ │ │ └── 0000n-test_<n>.cy.js
│ │ ├── Payment
│ │ ├── PaymentMethodList
│ │ ├── Payout
│ │ └── Routing
│ ├── fixtures # Directory for storing test data API request.
│ │ ├── fixture_<1>.json
│ │ ├── ...
│ │ └── fixture_<n>.json
│ ├── support # Directory for Cypress support files.
│ │ ├── commands.js # File containing custom Cypress commands and utilities.
│ │ ├── e2e.js
│ │ └── redirectionHandler.js # Functions for handling redirections in tests
│ └── utils
│ ├── RequestBodyUtils.js # Utility Functions for handling request bodies
│ ├── State.js
│ └── featureFlags.js # Functions for validating and controlling feature flags
├── screenshots
│ └── <connector-name> # Connector directory for storing screenshots of test failures
│ └── <test-name>.png
├── cypress.config.js # Cypress configuration file.
├── eslint.config.js # linter configuration file.
└── package.json # Node.js package file.
-
Include the connector details in the
creds.json
file -
Add the new connector details to the ConnectorUtils folder (including CardNo and connector-specific information).
To add a new Payment connector, refer to
Stripe.js
file for reference. To add a new Payout connector, refer toAdyen.js
file for reference.File Naming: Create a new file named <connector_name>.js for your specific connector.
Include Relevant Information: Populate the file with all the necessary details specific to that connector.
Handling Unsupported Features:
- If a connector does not support a specific payment method or a feature:
- The relevant configurations in the
<connector_name>.js
file can be omitted - The handling of unsupported or unimplemented features will be managed by the
Commons.js
file, which will throw the appropriateunsupported
ornot implemented
error
-
In
Utils.js
, import the new connector details -
If the connector has a specific redirection requirement, add relevant redirection logic in
support/redirectionHandler.js
To add a new test, create a new test file in the e2e
directory under respective service
. The test file should follow the naming convention 000<number>-<Service>Test.cy.js
and should contain the test cases related to the service.
// cypress/e2e/<Service>Test/NewFeature.cy.js
import * as fixtures from "../../../fixtures/imports";
import State from "../../../utils/State";
describe("New Feature", () => {
let globalState;
before(() => {
cy.task("getGlobalState").then((state) => {
globalState = new State(state);
});
});
after("flush global state", () => {
cy.task("setGlobalState", globalState.data);
});
it("tests new functionality", () => {
// Test implementation
});
});
// cypress/support/commands.js
Cypress.Commands.add("newCommand", (params, globalState) => {
const baseUrl = globalState.get("baseUrl");
const apiKey = globalState.get("apiKey");
const url = `${baseUrl}/endpoint`;
cy.request({
method: "POST",
url: url,
headers: {
"api-key": apiKey,
},
body: params,
}).then((response) => {
// Assertions
});
});
The global state is used to share data between tests. The global state is stored in the State
class and is accessible across all tests. Can only be accessed in the before
and after
hooks.
- Use
npm run cypress
for real-time test execution - View request/response details in Cypress UI
- Use DevTools for deeper debugging
cy.task("cli_log", "Debug message");
cy.log("Test state:", globalState.data);
- Automatically captured on test failure
- Custom screenshot capture:
cy.screenshot("debug-state");
- Add state logging in hooks:
beforeEach(() => {
cy.log("Current state:", JSON.stringify(globalState.data));
});
- If the
globalState
object does not contain latest data, it must be due to the hooks not being executed in the correct order - Add
cy.log(globalState)
to the test case to verify the data in theglobalState
object
Note
Refer to the Cypress's official documentation for more information on hooks and their execution order here.
- Use
cy.task
to interact with the Node.js environment - Task can only be used in
support
files andspec
files. Using them in files outside these directories will result in unexpected behavior or errors like abrupt termination of the test suite
To run the formatting and lint checks, execute the following command:
# Format the code
npm run format
# Check the formatting
npm run format:check
# Lint the code. This wont fix the logic issues, unused imports or variables
npm run lint -- --fix
- Use the global state for sharing data between tests
- Implement proper error handling
- Use appropriate wait strategies
- Maintain test independence
- Follow the existing folder structure
- Document connector-specific behaviors
- Use descriptive test and variable names
- Use custom commands for repetitive tasks
- Use
cy.log
for debugging and do not useconsole.log
- Fork the repository
- Create a feature branch
- Add tests following the guidelines
- Submit a pull request
{
// Connector with single credential support and metadata support
"adyen": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
},
"metadata": {
"key": "value"
}
},
"bankofamerica": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
}
},
"bluesnap": {
"connector_account_details": {
"auth_type": "BodyKey",
"api_key": "api_key",
"key1": "key1"
}
},
// Connector with multiple credential support
"cybersource": {
"connector_1": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
}
},
"connector_2": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
}
}
},
"nmi": {
"connector_account_details": {
"auth_type": "BodyKey",
"api_key": "api_key",
"key1": "key1"
}
},
"paypal": {
"connector_account_details": {
"auth_type": "BodyKey",
"api_key": "api_key",
"key1": "key1"
}
},
"stripe": {
"connector_account_details": {
"auth_type": "HeaderKey",
"api_key": "api_key"
}
},
"trustpay": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
}
}
}
- There are some use cases where a connector supports a feature that requires a different set of API keys (example: Network transaction ID for Stripe expects a different API Key to be passed). This forces the need for having multiple credentials that serves different use cases
- This basically means that a connector can have multiple credentials
- At present the maximum number of credentials that can be supported is
2
- The
creds.json
file should be structured to support multiple credentials for such connectors. Thecreds.json
file should be structured as follows:
{
"connector_name": {
"connector_1": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
}
},
"connector_2": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
}
}
}
}
Configs
is the newobject
that is introduced to manage the dynamic configurations that are required for the tests- This is supposed to be passed in an exchange (configuration for a specific can be passed to a test based on the need and this will impact everywhere in the test execution for that connector)
- At present, only 3 configs are supported:
DELAY
: This is used to introduce a delay in the test execution. This is useful when a connector requires a delay in order to perform a specific operationCONNECTOR_CREDENTIAL
: This is used to control the connector credentials that are used in the test execution. This is useful only when a connector supports multiple credentials and the test needs to be executed with a specific credentialTRIGGER_SKIP
: This is used to skip a test execution (preferably redirection flows). This is useful when a test is does not support a specific redirection flow and needs to be skipped
- Example: In order to refund a payment in Trustpay, a
DELAY
of at least5
seconds is required. By passingDELAY
to theConfigs
object for Trustpay, the delay will be applied to all the tests that are executed for Trustpay
{
"Configs": {
"DELAY": {
"STATUS": true,
"TIMEOUT": 15000
}
}
}