|
23 | 23 |
|
24 | 24 | <!-- Consumer Overview --> |
25 | 25 |
|
26 | | -This is an example of a React "Product" API consumer that uses playwright, [pact-playwright-adapter](https://www.npmjs.com/package/@pactflow/pact-playwright-adapter), [Pactflow](https://pactflow.io) and GitHub Actions to generate and publish Pact consumer contracts. |
| 26 | +This is an example of a React "Product" API consumer that uses playwright & [Pactflow](https://pactflow.io) and GitHub Actions to generate and publish Pact consumer contracts. |
27 | 27 |
|
28 | 28 | It performs pre-deployment cross-compatibility checks to ensure that it is compatible with specified providers using the Bi-Directional contract capability of Pactflow. |
29 | 29 |
|
|
37 | 37 |
|
38 | 38 | - It a React app implementing a "Product" website created with Create React App |
39 | 39 | - It utilises playwright to functionally test the website |
40 | | -- It utilises [pact-playwright-adapter](https://www.npmjs.com/package/@pactflow/pact-playwright-adapter) to transform playwright mocks into Pact consumer contracts. |
| 40 | +- It utilises a small helper file to cover playwright routes into pact interactions |
41 | 41 |
|
42 | 42 | ## Overview of Part of Bi-Directional Contract Testing Flow |
43 | 43 |
|
@@ -136,10 +136,31 @@ Using alternate pact CLI tools. |
136 | 136 | \_How to use Playwright |
137 | 137 |
|
138 | 138 | - Spin up the ui project by running `npm run start` |
139 | | -- Define your pact provider and consumer name at `playwright.json` as playwright environment variables |
140 | | -- You can stub your network request and response with `cy.intercept`, and record network call to a consumer driven contract with `cy.usePactWait`. Each request you want to add to the contract must call this method. |
141 | | -- `npm run playwright:headless:chrome` - this will run playwright e2e test in headless mode, and write stubbed network calls a pact file |
142 | | -- `npm run playwright:run` - this will run playwright e2e test with browser ui |
| 139 | +- Run your playwright tests with `npm run test` |
| 140 | + |
| 141 | +Look at one of the tests `test/productByQuery.spec.js` |
| 142 | + |
| 143 | +1. Import `transformPlaywrightMatchToPact` from `test/playwrightSerialiser.js` |
| 144 | +2. add `transformPlaywrightMatchToPact` function call into your playwright [route](https://playwright.dev/docs/api/class-page#page-route) |
| 145 | + |
| 146 | +```js |
| 147 | +const { transformPlaywrightMatchToPact } = require('./playwrightSerialiser') |
| 148 | + |
| 149 | +await page.route(productApiPath + '/products?id=2', async (route) => { |
| 150 | + const response = { |
| 151 | + status: 200, |
| 152 | + body: JSON.stringify(testData), |
| 153 | + headers: { |
| 154 | + 'Content-Type': 'application/json' |
| 155 | + } |
| 156 | + } |
| 157 | + route.fulfill(response) |
| 158 | + const pacticipant = 'pactflow-example-bi-directional-consumer-playwright' |
| 159 | + const provider = process.env.PACT_PROVIDER || 'pactflow-example-bi-directional-provider-dredd' |
| 160 | + await transformPlaywrightMatchToPact(route, { pacticipant, provider }) |
| 161 | + return |
| 162 | +}) |
| 163 | +``` |
143 | 164 |
|
144 | 165 | ## OS/Platform specific considerations |
145 | 166 |
|
|
0 commit comments