UNPKG

custom-commands-for-cypress

Version:
120 lines (92 loc) 4.05 kB
# Utility Functions with Axios This repository contains utility functions that leverage the Axios library to perform various tasks. These functions can be used in projects that require HTTP requests and other related operations. ## Installation To use the utility functions in your project, follow these steps: 1. Install Axios if you haven't already: ```shell npm install axios --save 2. Import the required functions into your project: import { fetchOTP, newEmail, newPassword, updateAccountList, generateRandomString, APIReqWithCreateBody, APIReqWithParams, APITokenGenerator, } from 'axios-utilities'; Utility Functions fetchOTP This function retrieves the OTP (One-Time Password) from a specific API endpoint. It makes use of the Axios library to perform HTTP requests. // Example usage fetchOTP() .then((otp) => { console.log('OTP:', otp); }) .catch((error) => { console.error('Error fetching OTP:', error); }); newEmail This function generates a new email address with the format QA{randomNumber}@example.testinator.com. // Example usage const email = newEmail(); newPassword This function generates a random password with at least one special character, one uppercase letter, one lowercase letter, and one number. // Example usage const password = newPassword(); updateAccountList This function updates the account list in a specified JSON file with a new email and password. The JSON file should contain an array of existing accounts. // Example usage const email = 'test@example.com'; const password = 'password123'; updateAccountList(email, password); generateRandomString This function generates a random string of a specified length, which can be useful for generating random data or test cases. // Example usage const randomString = generateRandomString(10); APIReqWithCreateBody This function performs an API request with a request body. It allows you to specify the HTTP method, URL, headers, request body, and a flag to indicate whether to fail on status code. // Example usage const httpMethod = 'POST'; const url = 'https://api.example.com/resource'; const headers = { 'Content-Type': 'application/json' }; const requestBody = { key: 'value' }; const failOnStatusCode = true; APIReqWithCreateBody(httpMethod, url, headers, requestBody, failOnStatusCode) .then(() => { console.log('API request with create body successful.'); console.log('API response:', process.env.ApiRes); }) .catch((error) => { console.error('API request with create body failed:', error); }); APIReqWithParams This function performs an API request with query parameters. It allows you to specify the HTTP method, URL, headers, query parameters, and a flag to indicate whether to fail on status code. // Example usage const httpMethod = 'GET'; const url = 'https://api.example.com/resource'; const headers = { 'Content-Type': 'application/json' }; const queryParams = { page: 1, limit: 10 }; const failOnStatusCode = true; APIReqWithParams(httpMethod, url, headers, queryParams, failOnStatusCode) .then(() => { console.log('API request with parameters successful.'); console.log('API response:', process.env.GetApiRes); }) .catch((error) => { console.error('API request with parameters failed:', error); }); APITokenGenerator This function generates an API token by making a POST request to a specific URL with the required parameters. It is commonly used for authentication purposes. // Example usage APITokenGenerator() .then(() => { console.log('API token generated successfully.'); console.log('Token response:', process.env.token_res); }) .catch((error) => { console.error('API token generation failed:', error); }); Contributing Contributions to this repository are welcome! If you have any suggestions, bug fixes, or additional utility functions to share, please feel free to open an issue or submit a pull request.