@qavajs/steps-playwright
Version:
qavajs steps to interact with playwright
82 lines • 3.09 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@qavajs/core");
/**
* Create simple mock instance
* @param {string} urlTemplate - minimatch url template to mock
* @param {string} memoryKey - memory key to store mock instance
* @example When I create mock for '/yourservice/**' as 'mock1'
* @example When I create mock for '$mockUrlTemplate' as 'mock1'
*/
(0, core_1.When)('I create mock for {value} as {value}', async function (urlTemplate, memoryKey) {
const url = await urlTemplate.value();
memoryKey.set(url);
});
async function respondWith(mockKey, statusCode, body) {
const mockUrl = await mockKey.value();
const responseStatusCode = parseInt(await statusCode.value());
const responseBody = await this.getValue(body);
await this.playwright.page.route(mockUrl, async (route) => {
await route.fulfill({
body: responseBody,
status: responseStatusCode
});
});
}
/**
* Add mocking rule to respond with desired status code and payload
* @param {string} mockKey - memory key to get mock instance
* @param {string} statusCode - status code
* @param {string} body - response body
* @example
* When I create mock for '/yourservice/**' as 'myServiceMock'
* And I set '$myServiceMock' mock to respond '200' with:
* """
* {
* "status": "success"
* }
* """
*/
(0, core_1.When)('I set {value} mock to respond {value} with:', respondWith);
/**
* Add mocking rule to respond with desired status code and payload
* @param {string} mockKey - memory key to get mock instance
* @param {string} statusCode - status code
* @param {string} body - response body
* @example
* When I create mock for '/yourservice/**' as 'myServiceMock'
* And I set '$myServiceMock' mock to respond '200' with '$response'
*/
(0, core_1.When)('I set {value} mock to respond {value} with {string}', respondWith);
/**
* Add mocking rule to abort request with certain reason
* @param {string} mockKey - memory key to get mock instance
* @param {string} reason - reason string see https://playwright.dev/docs/api/class-route#route-abort
* @example
* When I create mock for '/yourservice/**' as 'myServiceMock'
* And I set '$myServiceMock' mock to abort with 'Failed' reason
*/
(0, core_1.When)('I set {value} mock to abort with {value} reason', async function (mockKey, reason) {
const mockUrl = await mockKey.value();
const errorCode = await reason.value();
await this.playwright.page.route(mockUrl, async (route) => {
await route.abort(errorCode);
});
});
/**
* Restore mock
* @param {string} mockKey - memory key to get mock instance
* @example When I restore '$myServiceMock'
*/
(0, core_1.When)('I restore {value} mock', async function (mockKey) {
const mockUrl = await mockKey.value();
await this.playwright.page.unroute(mockUrl);
});
/**
* Restore all mocks
* @example When I restore all mocks
*/
(0, core_1.When)('I restore all mocks', async function () {
this.playwright.page._routes = [];
});
//# sourceMappingURL=mock.js.map
;