WireMock Service
wdio-wiremock-service is a 3rd party package, for more information please see GitHub | npm
This service helps you to run WireMock seamlessly when running tests with WebdriverIO. It uses the well known Maven repository to download the WireMock jar for you which is then automatically installed, started and stopped. Stay up to date by joining the community over at Gitter for help and support.
#
InstallationInstructions on how to install WebdriverIO
can be found here.
#
ConfigurationIn order to use the service with the wdio testrunner you need to add it to your service array:
IMPORTANT in version 5 of WebdriverIO you will need to set the port manually!
When using webdriverio standalone you need to add the service and trigger the onPrepare
and onComplete
hooks manually. An example can be found here (the example makes use of Jest):
#
UsageIn the root directory (default ./mock
) you find two subdirectories, __files
and mappings
which are used for your fixtures and mocks.
#
FixturesWireMock allows you to use fixture files alongside your mocks, place these in the __files
directory.
Example of a fixture:
#
MocksIn order for WireMock to find your mocks, place them in the mappings
directory.
Example of a mock:
Example of a mock with a fixture:
#
HTTP APIYou can also create a stub mapping by posting to WireMock’s HTTP API, this is useful when you want to set different states.
#
Basic stubbingThe following code will configure a response with a status of 200 to be returned when the relative URL exactly matches /example/data (including query parameters). The body of the response will be { "dummy": [{ "data": "example" }]}
and a Content-Type header will be sent with a value of application/json.
To create the stub described above via the JSON API, the following document can either be posted to http://<host>:<port>/__admin/mappings
or placed in a file with a .json extension under the mappings directory as a fixture.
#
Saving stubsStub mappings which have been created can be persisted to the mappings directory via a call to WireMock by posting a request with an empty body to http://<host>:<port>/__admin/mappings/save
.
#
File servingWhen running the standalone JAR, files placed under the __files
directory will be served up as if from under the docroot (rootDir), except if stub mapping matching the URL exists. For example if a file exists __files/things/myfile.html
and no stub mapping will match /things/myfile.html
then hitting http://<host>:<port>/things/myfile.html
will serve the file.
#
Removing stubsStub mappings can be deleted via the HTTP API by issuing a DELETE
to http://<host>:<port>/__admin/mappings/{id}
where id is the UUID of the stub mapping, found in its id field.
#
ResetThe WireMock server can be reset at any time, removing all stub mappings and deleting the request log by sending a POST
request with an empty body to http://<host>:<port>/__admin/reset
.
#
Getting all currently registered stub mappingsAll stub mappings can be fetched by sending a GET
to http://<host>:<port>/__admin/mappings
.
Optionally limit and offset parameters can be specified to constrain the set returned e.g. GET
http://localhost:8080/__admin/mappings?limit=10&offset=50
.
#
Getting a single stub mapping by IDA single stub mapping can be retrieved by ID by sending a GET
to http://<host>:<port>/__admin/mappings/{id}
.
#
More informationFor more information about stubbing check WireMock's official documentation.
#
Writing testsWriting your first test is really straight forward:
#
Using the WDIO testrunner in synch mode#
Using the WDIO testrunner in async mode#
Using WebdriverIO Standalone#
OptionsThe following options can be added to the service.
#
portPort where WireMock should run on.
Type: Number
Default: 8080
Example:
#
rootDirPath where WireMock will look for files.
Type: String
Default: ./mock
Example:
#
versionVersion of WireMock to be downloaded and used.
Type: String
Default: 2.26.3
Example:
#
silentSilent mode for logging WireMock's output (including additional logging from the service itself).
Type: Boolean
Default: false
Example:
#
mavenBaseUrlBase download url for Maven.
Type: String
Default: https://repo1.maven.org/maven2
Example:
#
argsList where you can pass all the supported arguments for configuring WireMock
Note: you cannot pass the options (port
, rootDir
, stdio
, mavenBaseUrl
) here as they will be ignored.
Type: Array
Example:
For more information on WebdriverIO see the homepage.