GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codecept.io — Data Management Data Management
Managing data for tests is always a tricky issue. How isolate data between tests, how to prepare data for different tests, etc. There are different approaches to solve it:
The most efficient way would be to allow test to control its data, i.e. the 3rd option. However, accessing database directly is not a good idea as database vendor, schema and data are used by application internally and are out of scope of acceptance test. Today all modern web applications have REST or GraphQL API . So it is a good idea to use it to create data for a test and delete it after. API is supposed to be a stable interface and it can be used by acceptance tests. CodeceptJS provides helpers for Data Management via REST and GraphQL API, as well as Data Objects — class-based page objects with automatic cleanup via lifecycle hooks. Data ObjectsSection titled “Data Objects”Data Objects are page object classes designed to manage test data via API. They use the REST helper (through This is a lightweight alternative to ApiDataFactory — ideal when you want full control over data creation and cleanup logic without factory configuration. Defining a Data ObjectSection titled “Defining a Data Object”ConfigurationSection titled “Configuration”Add the REST helper and the Data Object to your config: Usage in TestsSection titled “Usage in Tests”Data Objects can use any helper methods available via Learn more: See Page Objects for general page object patterns. REST helper allows sending raw HTTP requests to application. This is a tool to make shortcuts and create your data pragmatically via API. However, it doesn’t provide tools for testing APIs, so it should be paired with Playwright or WebDriver helper for browser testing. Enable REST helper in the config. It is recommended to set See the sample config: REST helper provides basic methods to send requests to application: As well as a method for setting headers: Here is a usage example: This can also be used to emulate Ajax requests:
GraphQLSection titled “GraphQL”GraphQL helper allows sending GraphQL queries and mutations to application, over Http. This tool allows you to create shortcuts and manage your data pragmatically via a GraphQL endpoint. However, it does not include tools for testing the endpoint, so it should be used in conjunction with WebDriver helpers for browser testing. Enable GraphQL helper in the config. It is recommended to set See the sample config: GraphQL helper provides two basic methods to queries and mutations to application: As well as a method for setting headers: Here is a usage example:
Data Generation with FactoriesSection titled “Data Generation with Factories”This concept is extended by:
These helpers build data according to defined rules and use REST API or GraphQL mutations to store them and automatically clean them up after a test. Just define how many items of any kind you need and the data factory helper will create them for you. To make this work some preparations are required. At first, you need data generation libraries which are Rosie and Faker. Faker can generate random names, emails, texts, and Rosie uses them to generate objects using factories. Install rosie and faker to create a first factory: Then create a module which will export a factory for an entity. And add that module as a part of the configuration for the helper. Please look at the respective Factory sections for examples for factory modules and configuration. API Data FactorySection titled “API Data Factory”This helper uses REST API to store the built data and automatically clean them up after a test, The way for setting data for a test is as simple as writing: After completing the preparations under ‘Data Generation with Factories’, create a factory module which will export a factory. See the example providing a factory for User generation: Next is to configure helper to match factories with API: Then, calling At the end of a test ApiDataFactory will clean up created record for you. This is done by collecting
ids from crated records and running
GraphQL Data FactorySection titled “GraphQL Data Factory”The helper uses GraphQL mutations to store the built data and automatically clean them up after a test. This way for setting data for a test is as simple as writing: After completing the preparations under ‘Data Generation with Factories’, create a factory module which will export a factory. The object built by the factory is sent as the variables object along with the mutation. So make sure it matches the argument type as detailed in the GraphQL schema. You may want to pass a constructor to the factory to achieve that. See the example providing a factory for User generation: Next is to configure helper to match factories with API: Then, calling At the end of a test GraphQLDataFactory will clean up created record for you. This is done by collecting
data from crated records, creating deletion mutation objects by passing the data to the
Requests Using Browser SessionSection titled “Requests Using Browser Session”All the REST, GraphQL, GraphQLDataFactory, and ApiDataFactory helpers allow override requests before sending. This feature can be used to fetch current browser cookies and set them to REST API or GraphQL client. By doing this we can make requests within the current browser session without a need of additional authentication.
CodeceptJS bundles Without In this case we are accessing WebDriver helper. However, you can replace WebDriver with any helper you use. The same can be done with GraphQLDataFactory. The order of helpers is important! ApiDataFactory will clean up created users after a test, so it needs browser to be still opened to obtain its cookies. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |