Consider a page which allows you to select a vehicle like car, truck and bike. You can save the selection by pressing a Save button. The save button will trigger the appropriate api call depending on the type of vehicle you selected i.e. POST
/Inventory/AddCar
, /Inventory/AddTruck
, /Inventory/AddBike
. In cypress, I want to intercept all these calls when the save button is clicked and then wait for the calls to complete. I tried using regex in my intercept as follows, but it did not work. How do I intercept multiple api calls which essentially do the same thing?
// A (web) page object function.
function saveVehicle()
{
const regex = `/[\/Inventory\/AddCar]|[\/Inventory\/AddTruck]|[\/Inventory\/AddBike]/`
cy.intercept("POST", regex).as("saveVehicle");
saveButton.click().wait("@saveVehicle");
}
The square brackets denote a single character out out those inside, rather than the complete string.
You can use round brackets instead (matching group), or no brackets at all.
Also, do not use string delimiters if applying the regex directly, /
delimiters are sufficient.
const regex = /(\/Inventory\/AddCar)|(\/Inventory\/AddTruck)|(\/Inventory\/AddBike)/
cy.intercept('POST', regex).as('saveVehicle')
Practice your regex here.