Stateful Behavior - Is it possible to return different responses to the same request depending on a state/scenario? #241
Replies: 1 comment
-
I only just stumbled across this project a few hours ago and have been reading the docs/code. I had a similar thought... At first I wondered if you might be able to utilize the State Helper in combination with the Request Matching like how its shown in the External Helpers page. But I couldn't quite figure out how to make use of the I was assuming that we might set a single Cookie that would look like this:
But skimming the code, I don't see a way to use I suppose we could do it with separate cookies instead, and then it looks pretty easy to run them through We can make use of a Mock File ExamplesPlaywright Example// Utils (probably better as a fixture)
const setTestInfo = async (context: BrowserContext, records: Record<string, any>, prefix = 'x-test') => {
await context.addCookies(Object.entries(records).reduce((acc, [key, val]) => {
acc.push({ name: [prefix, key].join('-'), value: `${val}` });
return acc;
}, [] as [{ name: string, value: string }]));
};
// Usage
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await browser.newPage();
/** [TC-0001/INITIAL] - Initial page load */
await setTestInfo(context, { id, state: 'INITIAL' });
await page.goto(...);
// 1. Started as: `x-test-state: INITIAL`
// 2. [GET] completed...
await expect(...).toBe(...);
/** [TC-0001/ACCOUNT_CREATE_SUCCESS] - After a new account was successfully created */
await setTestInfo(context, { id, state: 'INITIAL' });
await page.goto(...);
// 1. Started as: `x-test-state: INITIAL`
// 2. [GET] completed...
await expect(...).toBe(...);
await page.getByRole('button', { name: 'Create Account' }).click();
// 3. Still as: `x-test-state: INITIAL`
// 4. [POST] completed...
// 5. Changed to: `x-test-state: ACCOUNT_CREATE_SUCCESS`
// 6. Subsequent [GET] uses new 'state'...
await expect(...).toBe(...);
... Again, I've not used camouflage yet, but "if" the above works it would seem like an elegant enough approach. |
Beta Was this translation helpful? Give feedback.
-
There would be some requests that the headers, body and url paths are the same, but the expected responses are different depending on their states in a scenario flow. Wiremock has a solution named as Scenario. Is it possible to solve this branching in Camouflage Mock?
For example, in a banking service;
Beta Was this translation helpful? Give feedback.
All reactions