Co-authored-by: sai-harsha-vardhan <harsha111hero@gmail.com> Co-authored-by: hyperswitch-bot[bot] <148525504+hyperswitch-bot[bot]@users.noreply.github.com> Co-authored-by: Sai Harsha Vardhan <56996463+sai-harsha-vardhan@users.noreply.github.com>
Cypress Tests
Overview
This Tool is a solution designed to automate testing for the Hyperswitch using Cypress, an open-source tool capable of conducting API call tests and UI tests. This README provides guidance on installing Cypress and its dependencies.
Installation
Prerequisites
Before installing Cypress, ensure that Node and npm is installed on your machine. To check if it is installed, run the following command:
node -v
npm -v
If not, download and install Node from the official Node.js website. This will also install npm.
Run Test Cases on your local
To run test cases, follow these steps:
-
Clone the repository and switch to the project directory:
git clone https://github.com/juspay/hyperswitch cd cypress-tests -
Install Cypress and its dependencies to
cypress-testsdirectory by running the following command:npm ci -
Insert data to
cards_infotable inhyperswitch_dbpsql --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" -
Run Cypress test cases
To run the tests in interactive mode run the following command
npm run cypressTo run all the tests in headless mode run the following command
npm run cypress:ciTo run payment tests in headless mode run the following command
npm run cypress:paymentsTo run payout tests in headless mode run the following command
npm run cypress:payoutsTo run routing tests in headless mode run the following command
npm run cypress:routing
In order to run cypress tests against multiple connectors at a time or in parallel:
-
Set up
.envfile that exports necessary info:export DEBUG=cypress:cli export CYPRESS_ADMINAPIKEY='admin_api_key' export CYPRESS_BASEURL='base_url' export CYPRESS_CONNECTOR_AUTH_FILE_PATH="path/to/creds.json" 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.shOptionally,
--parallel <jobs (integer)>can be passed to run cypress tests in parallel. By default, whenparallelcommand is passed, it will be run in batches of5.
Note
To learn about how creds file should be structured, refer to the example.creds.json section below.
Folder Structure
The folder structure of this directory is as follows:
. # The root directory for the Cypress tests.
├── .gitignore
├── cypress # Contains Cypress-related files and folders.
│ ├── e2e # End-to-end test directory.
│ │ ├── ConnectorTest # Directory for test scenarios related to connectors.
│ │ │ ├── your_testcase1_files_here.cy.js
│ │ │ ├── your_testcase2_files_here.cy.js
│ │ │ └── ...
│ │ └── ConnectorUtils # Directory for utility functions related to connectors.
│ │ ├── connector_detail_files_here.js
│ │ └── utils.js
│ ├── fixtures # Directory for storing test data API request.
│ │ └── your_fixture_files_here.json
│ ├── support # Directory for Cypress support files.
│ │ ├── commands.js # File containing custom Cypress commands and utilities.
│ │ └── e2e.js
│ └── utils
│ └── utility_files_go_here.js
├── cypress.config.js # Cypress configuration file.
├── cypress.env.json # File is used to store environment-specific configuration values,such as base URLs, which can be accessed within your Cypress tests.
├── package.json # Node.js package file.
├── readme.md # This file
└── yarn.lock
Writing Tests
Adding Connectors
To add a new connector for testing with Hyperswitch, follow these steps:
-
Include the connector details in the
creds.jsonfile:example:
{ "stripe": { "connector_account_details": { "auth_type": "HeaderKey", "api_key": "SK_134" } } } -
Add the new connector details to the ConnectorUtils folder (including CardNo and connector-specific information).
Refer to Stripe.js file for guidance:
/cypress-tests/cypress/e2e/ConnectorUtils/Stripe.jsFile 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 feature:
- You can omit the relevant configurations in the <connector_name>.js file.
- The handling of unsupported features will be managed by the commons.js file, which will throw an unsupported or not implemented error as appropriate.
-
In
Utils.js, import the new connector details.
Adding Functions
Similarly, add any helper functions or utilities in the commands.js in support folder and import them into your tests as needed.
Example: Adding List Mandate function to support ListMandate scenario
Cypress.Commands.add("listMandateCallTest", (globalState) => {
// declare all the variables and constants
const customerId = globalState.get("customerId");
// construct the URL for the API call
const url: `${globalState.get("baseUrl")}/customers/${customerId}/mandates`
const api_key = globalState.get("apiKey");
cy.request({
method: "GET",
url: url,
headers: {
"Content-Type": "application/json",
"api-key": api_key,
},
// set failOnStatusCode to false to prevent Cypress from failing the test
failOnStatusCode: false,
}).then((response) => {
// mandatorliy log the `x-request-id` to the console
logRequestId(response.headers["x-request-id"]);
expect(response.headers["content-type"]).to.include("application/json");
if (response.status === 200) {
// do the necessary validations like below
for (const key in response.body) {
expect(response.body[key]).to.have.property("mandate_id");
expect(response.body[key]).to.have.property("status");
}
} else {
// handle the error response
expect(response.status).to.equal(400);
}
});
});
Adding Scenarios
To add new test scenarios:
- Navigate to the ConnectorTest directory.
- Create a new test file or modify existing ones to add your scenarios.
- Write your test scenarios using Cypress commands.
For example, to add a scenario for listing mandates in the Mandateflows:
// cypress/ConnectorTest/CreateSingleuseMandate.js
describe("Payment Scenarios", () => {
it("should complete a successful payment", () => {
// Your test logic here
});
});
In this scenario, you can call functions defined in command.js. For instance, to test the listMandateCallTest function:
describe("Payment Scenarios", () => {
it("list-mandate-call-test", () => {
cy.listMandateCallTest(globalState);
});
});
You can create similar scenarios by calling other functions defined in commands.js. These functions interact with utility files like <connector_name>.js and include necessary assertions to support various connector scenarios.
Debugging
It is recommended to run npm run cypress while developing new test cases to debug and verify as it opens the Cypress UI allowing the developer to run individual tests. This also opens up the possibility to to view the test execution in real-time and debug any issues that may arise by viewing the request and response payloads directly.
If, for any reason, the globalState object does not contain latest data, it must be due to the hooks not being executed in the correct order. In such cases, it is recommended to add cy.log(globalState) to the test case to verify the data in the globalState object.
Please refer to the Cypress's official documentation for more information on hooks and their execution order here.
Additional Resources
For more information on using Cypress and writing effective tests, refer to the official Cypress documentation: Cypress Documentation
Example creds.json
{
"adyen": {
"connector_account_details": {
"auth_type": "SignatureKey",
"api_key": "api_key",
"key1": "key1",
"api_secret": "api_secret"
}
},
"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"
}
},
"cybersource": {
"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"
}
}
}