codeceptjs 3.6.7 → 3.6.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/lib/codecept.js +3 -0
- package/lib/command/dryRun.js +30 -35
- package/lib/command/gherkin/snippets.js +1 -1
- package/lib/command/workers/runTests.js +1 -2
- package/lib/heal.js +1 -1
- package/lib/helper/ApiDataFactory.js +1 -1
- package/lib/helper/Appium.js +8 -11
- package/lib/helper/GraphQLDataFactory.js +1 -1
- package/lib/helper/Nightmare.js +2 -5
- package/lib/helper/Playwright.js +10 -10
- package/lib/helper/Protractor.js +2 -10
- package/lib/helper/Puppeteer.js +2 -5
- package/lib/helper/REST.js +24 -0
- package/lib/helper/TestCafe.js +1 -3
- package/lib/helper/WebDriver.js +7 -199
- package/lib/helper/extras/PlaywrightPropEngine.js +2 -2
- package/lib/helper/network/utils.js +1 -1
- package/lib/helper/testcafe/testcafe-utils.js +1 -1
- package/lib/locator.js +1 -1
- package/lib/output.js +15 -7
- package/lib/pause.js +5 -5
- package/lib/plugin/coverage.js +0 -3
- package/lib/secret.js +0 -1
- package/lib/step.js +1 -1
- package/lib/workers.js +0 -1
- package/package.json +41 -40
- package/translations/de-DE.js +1 -0
- package/translations/fr-FR.js +1 -1
- package/typings/index.d.ts +10 -2
- package/typings/promiseBasedTypes.d.ts +41 -172
- package/typings/types.d.ts +43 -178
- package/lib/helper/MockServer.js +0 -221
package/typings/types.d.ts
CHANGED
|
@@ -113,7 +113,7 @@ declare namespace CodeceptJS {
|
|
|
113
113
|
*
|
|
114
114
|
* module.exports = new Factory()
|
|
115
115
|
* // no need to set id, it will be set by REST API
|
|
116
|
-
* .attr('author', () => faker.
|
|
116
|
+
* .attr('author', () => faker.person.findName())
|
|
117
117
|
* .attr('title', () => faker.lorem.sentence())
|
|
118
118
|
* .attr('body', () => faker.lorem.paragraph());
|
|
119
119
|
* ```
|
|
@@ -354,6 +354,17 @@ declare namespace CodeceptJS {
|
|
|
354
354
|
* ```
|
|
355
355
|
*/
|
|
356
356
|
runOnAndroid(caps: any, fn: any): void;
|
|
357
|
+
/**
|
|
358
|
+
* Execute code only in Web mode.
|
|
359
|
+
*
|
|
360
|
+
* ```js
|
|
361
|
+
* I.runInWeb(() => {
|
|
362
|
+
* I.waitForElement('#data');
|
|
363
|
+
* I.seeInCurrentUrl('/data');
|
|
364
|
+
* });
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
runInWeb(): void;
|
|
357
368
|
/**
|
|
358
369
|
* Returns app installation status.
|
|
359
370
|
*
|
|
@@ -651,6 +662,19 @@ declare namespace CodeceptJS {
|
|
|
651
662
|
* Shortcut for `makeTouchAction`
|
|
652
663
|
*/
|
|
653
664
|
tap(locator: any): Promise<void>;
|
|
665
|
+
/**
|
|
666
|
+
* Perform a swipe on the screen or an element.
|
|
667
|
+
*
|
|
668
|
+
* ```js
|
|
669
|
+
* let locator = "#io.selendroid.testapp:id/LinearLayout1";
|
|
670
|
+
* I.swipe(locator, 800, 1200, 1000);
|
|
671
|
+
* ```
|
|
672
|
+
*
|
|
673
|
+
* [See complete reference](http://webdriver.io/api/mobile/swipe.html)
|
|
674
|
+
* @param [speed = 1000] - (optional), 1000 by default
|
|
675
|
+
* @returns Appium: support Android and iOS
|
|
676
|
+
*/
|
|
677
|
+
swipe(locator: CodeceptJS.LocatorOrString, xoffset: number, yoffset: number, speed?: number): Promise<void>;
|
|
654
678
|
/**
|
|
655
679
|
* Perform a swipe on the screen.
|
|
656
680
|
*
|
|
@@ -1204,10 +1228,6 @@ declare namespace CodeceptJS {
|
|
|
1204
1228
|
*
|
|
1205
1229
|
* ## Methods
|
|
1206
1230
|
*/
|
|
1207
|
-
// @ts-ignore
|
|
1208
|
-
// @ts-ignore
|
|
1209
|
-
// @ts-ignore
|
|
1210
|
-
// @ts-ignore
|
|
1211
1231
|
class ExpectHelper {
|
|
1212
1232
|
expectEqual(actualValue: any, expectedValue: any, customErrorMsg?: any): void;
|
|
1213
1233
|
expectNotEqual(actualValue: any, expectedValue: any, customErrorMsg?: any): void;
|
|
@@ -1319,10 +1339,6 @@ declare namespace CodeceptJS {
|
|
|
1319
1339
|
*
|
|
1320
1340
|
* ## Methods
|
|
1321
1341
|
*/
|
|
1322
|
-
// @ts-ignore
|
|
1323
|
-
// @ts-ignore
|
|
1324
|
-
// @ts-ignore
|
|
1325
|
-
// @ts-ignore
|
|
1326
1342
|
class ExpectHelper {
|
|
1327
1343
|
expectEqual(actualValue: any, expectedValue: any, customErrorMsg?: any): void;
|
|
1328
1344
|
expectNotEqual(actualValue: any, expectedValue: any, customErrorMsg?: any): void;
|
|
@@ -1664,7 +1680,7 @@ declare namespace CodeceptJS {
|
|
|
1664
1680
|
* input: { ...buildObj },
|
|
1665
1681
|
* }))
|
|
1666
1682
|
* // 'attr'-id can be left out depending on the GraphQl resolvers
|
|
1667
|
-
* .attr('name', () => faker.
|
|
1683
|
+
* .attr('name', () => faker.person.findName())
|
|
1668
1684
|
* .attr('email', () => faker.interact.email())
|
|
1669
1685
|
* ```
|
|
1670
1686
|
* For more options see [rosie documentation](https://github.com/rosiejs/rosie).
|
|
@@ -1997,10 +2013,6 @@ declare namespace CodeceptJS {
|
|
|
1997
2013
|
* @property [host = "0.0.0.0"] - Mock server host
|
|
1998
2014
|
* @property [httpsOpts] - key & cert values are the paths to .key and .crt files
|
|
1999
2015
|
*/
|
|
2000
|
-
// @ts-ignore
|
|
2001
|
-
// @ts-ignore
|
|
2002
|
-
// @ts-ignore
|
|
2003
|
-
// @ts-ignore
|
|
2004
2016
|
type MockServerConfig = {
|
|
2005
2017
|
port?: number;
|
|
2006
2018
|
host?: string;
|
|
@@ -2125,10 +2137,6 @@ declare namespace CodeceptJS {
|
|
|
2125
2137
|
*
|
|
2126
2138
|
* ## Methods
|
|
2127
2139
|
*/
|
|
2128
|
-
// @ts-ignore
|
|
2129
|
-
// @ts-ignore
|
|
2130
|
-
// @ts-ignore
|
|
2131
|
-
// @ts-ignore
|
|
2132
2140
|
class MockServer {
|
|
2133
2141
|
/**
|
|
2134
2142
|
* Start the mock server
|
|
@@ -3268,10 +3276,6 @@ declare namespace CodeceptJS {
|
|
|
3268
3276
|
* @property [recordHar] - record HAR and will be saved to `output/har`. See more of [HAR options](https://playwright.dev/docs/api/class-browser#browser-new-context-option-record-har).
|
|
3269
3277
|
* @property [testIdAttribute = data-testid] - locate elements based on the testIdAttribute. See more of [locate by test id](https://playwright.dev/docs/locators#locate-by-test-id).
|
|
3270
3278
|
*/
|
|
3271
|
-
// @ts-ignore
|
|
3272
|
-
// @ts-ignore
|
|
3273
|
-
// @ts-ignore
|
|
3274
|
-
// @ts-ignore
|
|
3275
3279
|
type PlaywrightConfig = {
|
|
3276
3280
|
url?: string;
|
|
3277
3281
|
browser?: 'chromium' | 'firefox' | 'webkit' | 'electron';
|
|
@@ -6799,10 +6803,6 @@ declare namespace CodeceptJS {
|
|
|
6799
6803
|
* @property [chrome] - pass additional [Puppeteer run options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions).
|
|
6800
6804
|
* @property [highlightElement] - highlight the interacting elements. Default: false. Note: only activate under verbose mode (--verbose).
|
|
6801
6805
|
*/
|
|
6802
|
-
// @ts-ignore
|
|
6803
|
-
// @ts-ignore
|
|
6804
|
-
// @ts-ignore
|
|
6805
|
-
// @ts-ignore
|
|
6806
6806
|
type PuppeteerConfig = {
|
|
6807
6807
|
url: string;
|
|
6808
6808
|
basicAuth?: any;
|
|
@@ -8745,10 +8745,6 @@ declare namespace CodeceptJS {
|
|
|
8745
8745
|
* @property [onResponse] - an async function which can update response object.
|
|
8746
8746
|
* @property [maxUploadFileSize] - set the max content file size in MB when performing api calls.
|
|
8747
8747
|
*/
|
|
8748
|
-
// @ts-ignore
|
|
8749
|
-
// @ts-ignore
|
|
8750
|
-
// @ts-ignore
|
|
8751
|
-
// @ts-ignore
|
|
8752
8748
|
type RESTConfig = {
|
|
8753
8749
|
endpoint?: string;
|
|
8754
8750
|
prettyPrintJson?: boolean;
|
|
@@ -8929,6 +8925,17 @@ declare namespace CodeceptJS {
|
|
|
8929
8925
|
* @returns response
|
|
8930
8926
|
*/
|
|
8931
8927
|
sendDeleteRequest(url: any, headers?: any): Promise<any>;
|
|
8928
|
+
/**
|
|
8929
|
+
* Sends DELETE request to API with payload.
|
|
8930
|
+
*
|
|
8931
|
+
* ```js
|
|
8932
|
+
* I.sendDeleteRequestWithPayload('/api/users/1', { author: 'john' });
|
|
8933
|
+
* ```
|
|
8934
|
+
* @param [payload = {}] - the payload to be sent. By default it is sent as an empty object
|
|
8935
|
+
* @param [headers = {}] - the headers object to be sent. By default, it is sent as an empty object
|
|
8936
|
+
* @returns response
|
|
8937
|
+
*/
|
|
8938
|
+
sendDeleteRequestWithPayload(url: any, payload?: any, headers?: any): Promise<any>;
|
|
8932
8939
|
}
|
|
8933
8940
|
/**
|
|
8934
8941
|
* SoftAssertHelper is a utility class for performing soft assertions.
|
|
@@ -10185,12 +10192,7 @@ declare namespace CodeceptJS {
|
|
|
10185
10192
|
* @property [timeouts] - [WebDriver timeouts](http://webdriver.io/docs/timeouts.html) defined as hash.
|
|
10186
10193
|
* @property [highlightElement] - highlight the interacting elements. Default: false. Note: only activate under verbose mode (--verbose).
|
|
10187
10194
|
* @property [logLevel = silent] - level of logging verbosity. Default: silent. Options: trace | debug | info | warn | error | silent. More info: https://webdriver.io/docs/configuration/#loglevel
|
|
10188
|
-
* @property [devtoolsProtocol = false] - enable devtools protocol. Default: false. More info: https://webdriver.io/docs/automationProtocols/#devtools-protocol.
|
|
10189
10195
|
*/
|
|
10190
|
-
// @ts-ignore
|
|
10191
|
-
// @ts-ignore
|
|
10192
|
-
// @ts-ignore
|
|
10193
|
-
// @ts-ignore
|
|
10194
10196
|
type WebDriverConfig = {
|
|
10195
10197
|
url: string;
|
|
10196
10198
|
browser: string;
|
|
@@ -10213,7 +10215,6 @@ declare namespace CodeceptJS {
|
|
|
10213
10215
|
timeouts?: any;
|
|
10214
10216
|
highlightElement?: boolean;
|
|
10215
10217
|
logLevel?: string;
|
|
10216
|
-
devtoolsProtocol?: boolean;
|
|
10217
10218
|
};
|
|
10218
10219
|
/**
|
|
10219
10220
|
* WebDriver helper which wraps [webdriverio](http://webdriver.io/) library to
|
|
@@ -10319,7 +10320,6 @@ declare namespace CodeceptJS {
|
|
|
10319
10320
|
* WebDriver : {
|
|
10320
10321
|
* url: "http://localhost",
|
|
10321
10322
|
* browser: "chrome",
|
|
10322
|
-
* devtoolsProtocol: true,
|
|
10323
10323
|
* desiredCapabilities: {
|
|
10324
10324
|
* chromeOptions: {
|
|
10325
10325
|
* args: [ "--headless", "--disable-gpu", "--no-sandbox" ]
|
|
@@ -12123,34 +12123,6 @@ declare namespace CodeceptJS {
|
|
|
12123
12123
|
* @returns scroll position
|
|
12124
12124
|
*/
|
|
12125
12125
|
grabPageScrollPosition(): Promise<PageScrollPosition>;
|
|
12126
|
-
/**
|
|
12127
|
-
* This method is **deprecated**.
|
|
12128
|
-
*
|
|
12129
|
-
*
|
|
12130
|
-
* Set the current geo location
|
|
12131
|
-
*
|
|
12132
|
-
*
|
|
12133
|
-
* ```js
|
|
12134
|
-
* I.setGeoLocation(121.21, 11.56);
|
|
12135
|
-
* I.setGeoLocation(121.21, 11.56, 10);
|
|
12136
|
-
* ```
|
|
12137
|
-
* @param latitude - to set.
|
|
12138
|
-
* @param longitude - to set
|
|
12139
|
-
* @param [altitude] - (optional, null by default) to set
|
|
12140
|
-
* @returns automatically synchronized promise through #recorder
|
|
12141
|
-
*/
|
|
12142
|
-
setGeoLocation(latitude: number, longitude: number, altitude?: number): void;
|
|
12143
|
-
/**
|
|
12144
|
-
* This method is **deprecated**.
|
|
12145
|
-
*
|
|
12146
|
-
* Return the current geo location
|
|
12147
|
-
* Resumes test execution, so **should be used inside async function with `await`** operator.
|
|
12148
|
-
*
|
|
12149
|
-
* ```js
|
|
12150
|
-
* let geoLocation = await I.grabGeoLocation();
|
|
12151
|
-
* ```
|
|
12152
|
-
*/
|
|
12153
|
-
grabGeoLocation(): Promise<{ latitude: number; longitude: number; altitude: number; }>;
|
|
12154
12126
|
/**
|
|
12155
12127
|
* Grab the width, height, location of given locator.
|
|
12156
12128
|
* Provide `width` or `height`as second param to get your desired prop.
|
|
@@ -12177,123 +12149,15 @@ declare namespace CodeceptJS {
|
|
|
12177
12149
|
/**
|
|
12178
12150
|
* Placeholder for ~ locator only test case write once run on both Appium and WebDriver.
|
|
12179
12151
|
*/
|
|
12180
|
-
|
|
12152
|
+
runOnIOS(caps: any, fn: any): void;
|
|
12181
12153
|
/**
|
|
12182
12154
|
* Placeholder for ~ locator only test case write once run on both Appium and WebDriver.
|
|
12183
12155
|
*/
|
|
12184
|
-
|
|
12185
|
-
/**
|
|
12186
|
-
* _Note:_ Only works when devtoolsProtocol is enabled.
|
|
12187
|
-
*
|
|
12188
|
-
* Resets all recorded network requests.
|
|
12189
|
-
*
|
|
12190
|
-
* ```js
|
|
12191
|
-
* I.flushNetworkTraffics();
|
|
12192
|
-
* ```
|
|
12193
|
-
*/
|
|
12194
|
-
flushNetworkTraffics(): void;
|
|
12195
|
-
/**
|
|
12196
|
-
* _Note:_ Only works when devtoolsProtocol is enabled.
|
|
12197
|
-
*
|
|
12198
|
-
* Stops recording of network traffic. Recorded traffic is not flashed.
|
|
12199
|
-
*
|
|
12200
|
-
* ```js
|
|
12201
|
-
* I.stopRecordingTraffic();
|
|
12202
|
-
* ```
|
|
12203
|
-
*/
|
|
12204
|
-
stopRecordingTraffic(): void;
|
|
12205
|
-
/**
|
|
12206
|
-
* _Note:_ Only works when devtoolsProtocol is enabled.
|
|
12207
|
-
*
|
|
12208
|
-
* Starts recording the network traffics.
|
|
12209
|
-
* This also resets recorded network requests.
|
|
12210
|
-
*
|
|
12211
|
-
* ```js
|
|
12212
|
-
* I.startRecordingTraffic();
|
|
12213
|
-
* ```
|
|
12214
|
-
* @returns automatically synchronized promise through #recorder
|
|
12215
|
-
*/
|
|
12216
|
-
startRecordingTraffic(): void;
|
|
12217
|
-
/**
|
|
12218
|
-
* _Note:_ Only works when devtoolsProtocol is enabled.
|
|
12219
|
-
*
|
|
12220
|
-
* Grab the recording network traffics
|
|
12221
|
-
*
|
|
12222
|
-
* ```js
|
|
12223
|
-
* const traffics = await I.grabRecordedNetworkTraffics();
|
|
12224
|
-
* expect(traffics[0].url).to.equal('https://reqres.in/api/comments/1');
|
|
12225
|
-
* expect(traffics[0].response.status).to.equal(200);
|
|
12226
|
-
* expect(traffics[0].response.body).to.contain({ name: 'this was mocked' });
|
|
12227
|
-
* ```
|
|
12228
|
-
* @returns recorded network traffics
|
|
12229
|
-
*/
|
|
12230
|
-
grabRecordedNetworkTraffics(): any[];
|
|
12231
|
-
/**
|
|
12232
|
-
* _Note:_ Only works when devtoolsProtocol is enabled.
|
|
12233
|
-
*
|
|
12234
|
-
* Verifies that a certain request is part of network traffic.
|
|
12235
|
-
*
|
|
12236
|
-
* ```js
|
|
12237
|
-
* // checking the request url contains certain query strings
|
|
12238
|
-
* I.amOnPage('https://openai.com/blog/chatgpt');
|
|
12239
|
-
* I.startRecordingTraffic();
|
|
12240
|
-
* await I.seeTraffic({
|
|
12241
|
-
* name: 'sentry event',
|
|
12242
|
-
* url: 'https://images.openai.com/blob/cf717bdb-0c8c-428a-b82b-3c3add87a600',
|
|
12243
|
-
* parameters: {
|
|
12244
|
-
* width: '1919',
|
|
12245
|
-
* height: '1138',
|
|
12246
|
-
* },
|
|
12247
|
-
* });
|
|
12248
|
-
* ```
|
|
12249
|
-
*
|
|
12250
|
-
* ```js
|
|
12251
|
-
* // checking the request url contains certain post data
|
|
12252
|
-
* I.amOnPage('https://openai.com/blog/chatgpt');
|
|
12253
|
-
* I.startRecordingTraffic();
|
|
12254
|
-
* await I.seeTraffic({
|
|
12255
|
-
* name: 'event',
|
|
12256
|
-
* url: 'https://cloudflareinsights.com/cdn-cgi/rum',
|
|
12257
|
-
* requestPostData: {
|
|
12258
|
-
* st: 2,
|
|
12259
|
-
* },
|
|
12260
|
-
* });
|
|
12261
|
-
* ```
|
|
12262
|
-
* @param opts - options when checking the traffic network.
|
|
12263
|
-
* @param opts.name - A name of that request. Can be any value. Only relevant to have a more meaningful error message in case of fail.
|
|
12264
|
-
* @param opts.url - Expected URL of request in network traffic
|
|
12265
|
-
* @param [opts.parameters] - Expected parameters of that request in network traffic
|
|
12266
|
-
* @param [opts.requestPostData] - Expected that request contains post data in network traffic
|
|
12267
|
-
* @param [opts.timeout] - Timeout to wait for request in seconds. Default is 10 seconds.
|
|
12268
|
-
* @returns automatically synchronized promise through #recorder
|
|
12269
|
-
*/
|
|
12270
|
-
seeTraffic(opts: {
|
|
12271
|
-
name: string;
|
|
12272
|
-
url: string;
|
|
12273
|
-
parameters?: any;
|
|
12274
|
-
requestPostData?: any;
|
|
12275
|
-
timeout?: number;
|
|
12276
|
-
}): void;
|
|
12156
|
+
runOnAndroid(caps: any, fn: any): void;
|
|
12277
12157
|
/**
|
|
12278
|
-
*
|
|
12279
|
-
*
|
|
12280
|
-
* Verifies that a certain request is not part of network traffic.
|
|
12281
|
-
*
|
|
12282
|
-
* Examples:
|
|
12283
|
-
*
|
|
12284
|
-
* ```js
|
|
12285
|
-
* I.dontSeeTraffic({ name: 'Unexpected API Call', url: 'https://api.example.com' });
|
|
12286
|
-
* I.dontSeeTraffic({ name: 'Unexpected API Call of "user" endpoint', url: /api.example.com.*user/ });
|
|
12287
|
-
* ```
|
|
12288
|
-
* @param opts - options when checking the traffic network.
|
|
12289
|
-
* @param opts.name - A name of that request. Can be any value. Only relevant to have a more meaningful error message in case of fail.
|
|
12290
|
-
* @param opts.url - Expected URL of request in network traffic. Can be a string or a regular expression.
|
|
12291
|
-
* @returns automatically synchronized promise through #recorder
|
|
12158
|
+
* Placeholder for ~ locator only test case write once run on both Appium and WebDriver.
|
|
12292
12159
|
*/
|
|
12293
|
-
|
|
12294
|
-
name: string;
|
|
12295
|
-
url: string | RegExp;
|
|
12296
|
-
}): void;
|
|
12160
|
+
runInWeb(): void;
|
|
12297
12161
|
}
|
|
12298
12162
|
interface ActorStatic {
|
|
12299
12163
|
/**
|
|
@@ -12686,7 +12550,7 @@ declare namespace CodeceptJS {
|
|
|
12686
12550
|
inside(locator: CodeceptJS.LocatorOrString): Locator;
|
|
12687
12551
|
after(locator: CodeceptJS.LocatorOrString): Locator;
|
|
12688
12552
|
before(locator: CodeceptJS.LocatorOrString): Locator;
|
|
12689
|
-
static build(locator
|
|
12553
|
+
static build(locator?: CodeceptJS.LocatorOrString): Locator;
|
|
12690
12554
|
/**
|
|
12691
12555
|
* Filters to modify locators
|
|
12692
12556
|
*/
|
|
@@ -12741,6 +12605,7 @@ declare namespace CodeceptJS {
|
|
|
12741
12605
|
function skipped(test: Mocha.Test): void;
|
|
12742
12606
|
}
|
|
12743
12607
|
namespace scenario {
|
|
12608
|
+
function started(test: Mocha.Test): void;
|
|
12744
12609
|
function passed(test: Mocha.Test): void;
|
|
12745
12610
|
function failed(test: Mocha.Test): void;
|
|
12746
12611
|
}
|
package/lib/helper/MockServer.js
DELETED
|
@@ -1,221 +0,0 @@
|
|
|
1
|
-
const { mock, settings } = require('pactum')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* ## Configuration
|
|
5
|
-
*
|
|
6
|
-
* This helper should be configured in codecept.conf.(js|ts)
|
|
7
|
-
*
|
|
8
|
-
* @typedef MockServerConfig
|
|
9
|
-
* @type {object}
|
|
10
|
-
* @prop {number} [port=9393] - Mock server port
|
|
11
|
-
* @prop {string} [host="0.0.0.0"] - Mock server host
|
|
12
|
-
* @prop {object} [httpsOpts] - key & cert values are the paths to .key and .crt files
|
|
13
|
-
*/
|
|
14
|
-
let config = {
|
|
15
|
-
port: 9393,
|
|
16
|
-
host: '0.0.0.0',
|
|
17
|
-
httpsOpts: {
|
|
18
|
-
key: '',
|
|
19
|
-
cert: '',
|
|
20
|
-
},
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* MockServer
|
|
25
|
-
*
|
|
26
|
-
* The MockServer Helper in CodeceptJS empowers you to mock any server or service via HTTP or HTTPS, making it an excellent tool for simulating REST endpoints and other HTTP-based APIs.
|
|
27
|
-
*
|
|
28
|
-
* <!-- configuration -->
|
|
29
|
-
*
|
|
30
|
-
* #### Examples
|
|
31
|
-
*
|
|
32
|
-
* You can seamlessly integrate MockServer with other helpers like REST or Playwright. Here's a configuration example inside the `codecept.conf.js` file:
|
|
33
|
-
*
|
|
34
|
-
* ```javascript
|
|
35
|
-
* {
|
|
36
|
-
* helpers: {
|
|
37
|
-
* REST: {...},
|
|
38
|
-
* MockServer: {
|
|
39
|
-
* // default mock server config
|
|
40
|
-
* port: 9393,
|
|
41
|
-
* host: '0.0.0.0',
|
|
42
|
-
* httpsOpts: {
|
|
43
|
-
* key: '',
|
|
44
|
-
* cert: '',
|
|
45
|
-
* },
|
|
46
|
-
* },
|
|
47
|
-
* }
|
|
48
|
-
* }
|
|
49
|
-
* ```
|
|
50
|
-
*
|
|
51
|
-
* #### Adding Interactions
|
|
52
|
-
*
|
|
53
|
-
* Interactions add behavior to the mock server. Use the `I.addInteractionToMockServer()` method to include interactions. It takes an interaction object as an argument, containing request and response details.
|
|
54
|
-
*
|
|
55
|
-
* ```javascript
|
|
56
|
-
* I.addInteractionToMockServer({
|
|
57
|
-
* request: {
|
|
58
|
-
* method: 'GET',
|
|
59
|
-
* path: '/api/hello'
|
|
60
|
-
* },
|
|
61
|
-
* response: {
|
|
62
|
-
* status: 200,
|
|
63
|
-
* body: {
|
|
64
|
-
* 'say': 'hello to mock server'
|
|
65
|
-
* }
|
|
66
|
-
* }
|
|
67
|
-
* });
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* #### Request Matching
|
|
71
|
-
*
|
|
72
|
-
* When a real request is sent to the mock server, it matches the received request with the interactions. If a match is found, it returns the specified response; otherwise, a 404 status code is returned.
|
|
73
|
-
*
|
|
74
|
-
* - Strong match on HTTP Method, Path, Query Params & JSON body.
|
|
75
|
-
* - Loose match on Headers.
|
|
76
|
-
*
|
|
77
|
-
* ##### Strong Match on Query Params
|
|
78
|
-
*
|
|
79
|
-
* You can send different responses based on query parameters:
|
|
80
|
-
*
|
|
81
|
-
* ```javascript
|
|
82
|
-
* I.addInteractionToMockServer({
|
|
83
|
-
* request: {
|
|
84
|
-
* method: 'GET',
|
|
85
|
-
* path: '/api/users',
|
|
86
|
-
* queryParams: {
|
|
87
|
-
* id: 1
|
|
88
|
-
* }
|
|
89
|
-
* },
|
|
90
|
-
* response: {
|
|
91
|
-
* status: 200,
|
|
92
|
-
* body: 'user 1'
|
|
93
|
-
* }
|
|
94
|
-
* });
|
|
95
|
-
*
|
|
96
|
-
* I.addInteractionToMockServer({
|
|
97
|
-
* request: {
|
|
98
|
-
* method: 'GET',
|
|
99
|
-
* path: '/api/users',
|
|
100
|
-
* queryParams: {
|
|
101
|
-
* id: 2
|
|
102
|
-
* }
|
|
103
|
-
* },
|
|
104
|
-
* response: {
|
|
105
|
-
* status: 200,
|
|
106
|
-
* body: 'user 2'
|
|
107
|
-
* }
|
|
108
|
-
* });
|
|
109
|
-
* ```
|
|
110
|
-
*
|
|
111
|
-
* - GET to `/api/users?id=1` will return 'user 1'.
|
|
112
|
-
* - GET to `/api/users?id=2` will return 'user 2'.
|
|
113
|
-
* - For all other requests, it returns a 404 status code.
|
|
114
|
-
*
|
|
115
|
-
* ##### Loose Match on Body
|
|
116
|
-
*
|
|
117
|
-
* When `strict` is set to false, it performs a loose match on query params and response body:
|
|
118
|
-
*
|
|
119
|
-
* ```javascript
|
|
120
|
-
* I.addInteractionToMockServer({
|
|
121
|
-
* strict: false,
|
|
122
|
-
* request: {
|
|
123
|
-
* method: 'POST',
|
|
124
|
-
* path: '/api/users',
|
|
125
|
-
* body: {
|
|
126
|
-
* name: 'john'
|
|
127
|
-
* }
|
|
128
|
-
* },
|
|
129
|
-
* response: {
|
|
130
|
-
* status: 200
|
|
131
|
-
* }
|
|
132
|
-
* });
|
|
133
|
-
* ```
|
|
134
|
-
*
|
|
135
|
-
* - POST to `/api/users` with the body containing `name` as 'john' will return a 200 status code.
|
|
136
|
-
* - POST to `/api/users` without the `name` property in the body will return a 404 status code.
|
|
137
|
-
*
|
|
138
|
-
* Happy testing with MockServer in CodeceptJS! 🚀
|
|
139
|
-
*
|
|
140
|
-
* ## Methods
|
|
141
|
-
*/
|
|
142
|
-
class MockServer {
|
|
143
|
-
constructor(passedConfig) {
|
|
144
|
-
settings.setLogLevel('SILENT')
|
|
145
|
-
config = { ...passedConfig }
|
|
146
|
-
if (global.debugMode) {
|
|
147
|
-
settings.setLogLevel('VERBOSE')
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Start the mock server
|
|
153
|
-
* @param {number} [port] start the mock server with given port
|
|
154
|
-
*
|
|
155
|
-
* @returns void
|
|
156
|
-
*/
|
|
157
|
-
async startMockServer(port) {
|
|
158
|
-
const _config = { ...config }
|
|
159
|
-
if (port) _config.port = port
|
|
160
|
-
await mock.setDefaults(_config)
|
|
161
|
-
await mock.start()
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Stop the mock server
|
|
166
|
-
*
|
|
167
|
-
* @returns void
|
|
168
|
-
*
|
|
169
|
-
*/
|
|
170
|
-
async stopMockServer() {
|
|
171
|
-
await mock.stop()
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/**
|
|
175
|
-
* An interaction adds behavior to the mock server
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* ```js
|
|
179
|
-
* I.addInteractionToMockServer({
|
|
180
|
-
* request: {
|
|
181
|
-
* method: 'GET',
|
|
182
|
-
* path: '/api/hello'
|
|
183
|
-
* },
|
|
184
|
-
* response: {
|
|
185
|
-
* status: 200,
|
|
186
|
-
* body: {
|
|
187
|
-
* 'say': 'hello to mock server'
|
|
188
|
-
* }
|
|
189
|
-
* }
|
|
190
|
-
* });
|
|
191
|
-
* ```
|
|
192
|
-
* ```js
|
|
193
|
-
* // with query params
|
|
194
|
-
* I.addInteractionToMockServer({
|
|
195
|
-
* request: {
|
|
196
|
-
* method: 'GET',
|
|
197
|
-
* path: '/api/hello',
|
|
198
|
-
* queryParams: {
|
|
199
|
-
* id: 2
|
|
200
|
-
* }
|
|
201
|
-
* },
|
|
202
|
-
* response: {
|
|
203
|
-
* status: 200,
|
|
204
|
-
* body: {
|
|
205
|
-
* 'say': 'hello to mock server'
|
|
206
|
-
* }
|
|
207
|
-
* }
|
|
208
|
-
* });
|
|
209
|
-
* ```
|
|
210
|
-
*
|
|
211
|
-
* @param {CodeceptJS.MockInteraction|object} interaction add behavior to the mock server
|
|
212
|
-
*
|
|
213
|
-
* @returns void
|
|
214
|
-
*
|
|
215
|
-
*/
|
|
216
|
-
async addInteractionToMockServer(interaction) {
|
|
217
|
-
await mock.addInteraction(interaction)
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
module.exports = MockServer
|