@checksum-ai/runtime 1.1.15 → 1.1.17

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 CHANGED
@@ -1,20 +1,17 @@
1
- # Checksum.ai Tests
1
+ # Checksum.ai Runtime
2
2
 
3
- ## Quick Start
3
+ ### Quick Start
4
4
 
5
- 1. Run `npm install -D checksumai`.
6
- 2. Navigate to the directory where you want to add Checksum tests and run `npx checksumai init`.
7
- 3. Run `npx playwright install --with-deps` to install Playwright dependencies.
8
- 4. Edit `checksum.config.ts` to include necessary configurations such as:
9
- - `apiKey`
10
- - `baseURL`
11
- - `username`
12
- - `password`
13
- 5. Update `login.ts` with your login function using Playwright. See the Login Function section below for guidance.
14
- 6. Run `npx checksumai test` to execute the example test and verify successful login.
15
- 7. If you haven't already, visit [app.checksum.ai](https://app.checksum.ai) to complete the configuration and generate a test. Then, wait for the pull request (PR) to be created and approve it.
5
+ 1. Install the package using `npm install -D @checksum-ai/runtime` or `yarn add @checksum-ai/runtime -D`.
6
+ 2. Navigate to the directory where you want to initialize the Checksum tests folder and run `npx checksumai init`.
7
+ 3. In the newly created "checksum" folder
8
+ 1. Edit `checksum.config.ts` and add the necessary configurations, including your apiKey, application baseURL, environment info, etc.
9
+ 2. Update `login.ts` with your login function using Playwright. See the Login Function section below for guidance.
10
+ 4. Run `npx playwright install --with-deps` to install Playwright dependencies.
11
+ 5. Run `npx checksumai test` to execute the example test and verify successful login.
12
+ 6. If you haven't already, visit [app.checksum.ai](https://app.checksum.ai) to complete the configuration and generate a test. Then, wait for the pull request (PR) to be created and approve it.
16
13
 
17
- ## Login Function
14
+ ### Login Function
18
15
 
19
16
  1. This function is executed at the start of each test.
20
17
  2. We recommend using a consistent seeded user for every test. For example, before each test, call a webhook that creates a user, seeds it with data, and returns the username and password. This approach ensures test reliability and allows parallel test execution. Configure this webhook [in your project](https://app.checksum.ai/#/settings/wizard) for consistent test generation.
@@ -23,36 +20,235 @@
23
20
 
24
21
  ## Checksum AI Magic
25
22
 
26
- The tests generated by Checksum are based on Playwright. When executed using the Checksum CLI with an API key, Checksum enhances Playwright's functionality, improving test reliability and automatically maintaining tests.
23
+ The tests generated by Checksum are based on Playwright. When executed using the Checksum runtime CLI with an API key, Checksum enhances Playwright's functionality, improving test reliability and automatically maintains tests. Each enhancement is enabled/disabled using the `checksum.config.ts` file.
27
24
 
28
- ### Autonomous Test Agent
25
+ #### Smart Selectors
26
+ When generating tests, Checksum stores extensive metadata for each action (stored in the `checksum/test-data` folder). If a traditional selector fails, this metadata is used to locate the target element. For example, if a test identifies an element by its ID but the ID changes, Checksum utilizes other data points (e.g., element class, text, parents) to locate the element. Use the `checksumSelector("<id>")` method to link an action to its metadata. Do not alter the IDs.
29
27
 
30
- Checksum runs your Playwright tests regularly, but we added a few extra features to make tests more reliable. All of the features can be turned on/off through `checksum.config.ts`
31
28
 
32
- **Smart Selectors**
33
- When generating tests, Checksum stores extensive metadata for each action (see the `test-data` folder). If a traditional selector fails, this metadata is used for correction. For example, if a test identifies an element by its ID but the ID changes, Checksum utilizes other data points (e.g., element class, text, parents) to locate the element. Use the `checksumSelector("<id>")` method to link an action to its metadata. Do not alter the IDs.
29
+ #### Checksum AI
30
+ If Smart Selectors also fail, Checksum's custom-trained model can regenerate the failed section of the test. In such cases, the model might add, remove, or alter actions to achieve the same objectives, without changing the assertions. The assumption is that as long as the assertions pass, the model has successfully fixed the test. Use the `checksumAI` method to wrap each step in order to achieve this behavior.
34
31
 
35
- **Checksum AI**
36
- If Smart Selectors also fail, Checksum's custom-trained model can regenerate the failed section of the test. In such cases, the model might add, remove, or alter actions to achieve the same objectives, without changing the assertions. The assumption is that as long as the assertions pass, the model has successfully fixed the test. Use the `.checksumAI("<natural language description of the test>")` method to guide the model in fixing the test.
32
+ Example using both `checksumSelector` and `checksumAI`:
33
+ ```js
34
+ await checksumAI("Save the form and continue", () =>
35
+ page
36
+ .checksumSelector("gIexv")
37
+ .getByRole("button", { name: "Save" })
38
+ .click()
39
+ );
40
+ ```
37
41
 
38
42
  You can modify the description as needed for our model. Additionally, you can include steps with only ChecksumAI descriptions, prompting our model to generate the Playwright code. For example, `await page.checksumAI("Click on 'New Task' button")` without the actual action will have our model generate the necessary Playwright code. You can even author entire tests in this manner.
39
43
 
40
- ### Run Modes
41
44
 
42
- Checksum offers three run modes:
45
+ ### Mock Data
43
46
 
44
- 1. **Normal** - Tests are executed using the Autonomous Test Agent as defined in the config file.
45
- 2. **Heal** - If the Autonomous Test Agent corrects a test, a new test file with the fix is created. By default, this file is created locally, but you can also configure the Agent to open a PR to your GitHub repository by setting `autoHealPRs` to true.
46
- 3. **Refactor (Work in Progress)** - The Autonomous Test Agent runs the test and, for each action, regenerates a regular Playwright selector, a Smart Selector, and a Checksum AI description.
47
+ When generating tests, Checksum records all network responses, allowing tests to run in the same context. These recordings are stored as HAR files, stored in the `checksum/test-data/har` folder. Using this method is particularly useful for debugging newly generated tests, especially if your testing database/context differs from the one used for test generation. Note that if your network responses' format changes, the mocked data may not work as expected.
47
48
 
48
- ### Mock Data
49
+ ## Checksum Config
50
+ Alongside standard test run configurations found in `playwright.config.ts`, use the `checksum.config.ts` file to configure your test runs.
51
+
52
+ ```js
53
+ {
54
+ /**
55
+ * Checksum Run mode.
56
+ * Normal is currently the only supported mode.
57
+ */
58
+ runMode: RunMode.Normal,
59
+
60
+ /**
61
+ * Insert here your Checksum API key.
62
+ * You can find it in https://app.checksum.ai/#/settings/
63
+ */
64
+ apiKey: "<API key>",
65
+
66
+ /**
67
+ * Define your test run environments and test users within each environment.
68
+ * The environments must be aligned with those set here:
69
+ * https://app.checksum.ai/#/settings/
70
+ */
71
+ environments: [{
72
+ name: 'staging';
73
+ users: [
74
+ {
75
+ role: "host",
76
+ username: "<host username>",
77
+ password: "<host password>",
78
+ },
79
+ {
80
+ role: "guest",
81
+ username: "<guest username>",
82
+ password: "<guest password>",
83
+ },
84
+ ],
85
+ baseURL: 'staging.mydomain.com';
86
+ loginURL: 'staging.mydomain.com/login';
87
+ default: true;
88
+ }]
89
+
90
+ options: {
91
+ /**
92
+ * Whether to use Checksum Smart Selector in order to
93
+ * recover from failing to locate an element for an action
94
+ */
95
+ useChecksumSelectors: true,
96
+ /**
97
+ * Whether to use Checksum AI in order to
98
+ * recover from a failed action or assertion
99
+ */
100
+ useChecksumAI: { actions: true, assertions: false },
101
+ /**
102
+ * Whether to use recorded network responses when running your tests
103
+ */
104
+ useMockData: false,
105
+ /**
106
+ * Whether to upload the test reports to Checksum cloud
107
+ * allowing you to view them at https://app.checksum.ai/#/test-runs.
108
+ * Note: Only relevant if Playwright reporter config is set to HTML
109
+ * Note: Reports will be saved locally either way (according to Playwright configs)
110
+ * and can be viewed using the CLI command show-reports.
111
+ */
112
+ hostReports: !!process.env.CI,
113
+ }
114
+ }
115
+ ```
116
+ ## Playwright Configuration
117
+
118
+ A Playwright configuration file `playwright.config.ts` should be available to the Playwright tests runner in order to provide project configuration.
119
+ For your convenience we extended the configuration to allow the addition of the `playwright-extra-plugins` packages.
120
+ Available plugins can be found at https://github.com/berstend/puppeteer-extra/tree/master/packages/playwright-extra
121
+
122
+ To add a plugin:
123
+
124
+ - Install it using your package manager (yarn, npm, pnpm)
125
+ - Import the plugin in your `playwright.config.ts` file
126
+ - In the `projects` definition under `use`, add it to the playwrightExtra array
127
+ - Make sure to initialize the plugin before or during the addition to the array
128
+
129
+ Example `playwright.config.ts` with the `puppeteer-extra-plugin-stealth` plugin:
130
+
131
+ ```js
132
+ import { PuppeteerExtraPlugin } from "puppeteer-extra-plugin";
133
+ import StealthPlugin from "puppeteer-extra-plugin-stealth"; // <--- Added import line
134
+
135
+ export default defineConfig<{ playwrightExtra?: PuppeteerExtraPlugin[] }>({
136
+ // ....
137
+ projects: [
138
+ {
139
+ name: "chromium",
140
+ testMatch: /checksum.spec/,
141
+ use: {
142
+ ...devices["Desktop Chrome"],
143
+ playwrightExtra: [StealthPlugin()], // <--- Initialized and added to the playwrightExtra array
144
+ },
145
+ },
146
+ ],
147
+ });
148
+ ```
149
+
150
+ **See more detailed instructions inside the `checksum-root/playwright.config.ts` file**
151
+
152
+ ## Checksum Helpers API
153
+ Helpers are deconstructed from the result of the initial call to the imported @checksum-ai/runtime `init` method, as following:
154
+ ```js
155
+ import { test as base } from "@playwright/test";
156
+ import { init, IChecksumPage } from "@checksum-ai/runtime";
157
+ const { test, expect, defineChecksumTest, login, checksumAI } = init(base);
158
+ ```
159
+
160
+ ```js
161
+ function init(base: ChecksumTestType<PlaywrightTestArgs>): {
162
+ /**
163
+ * The Playwright test instance enhanced by Checksum
164
+ */
165
+ test: ChecksumTestType<ChecksumPlaywrightTestArgs>;
166
+ /**
167
+ * The login method which calls the user defined login function
168
+ */
169
+ login: ReturnType<typeof getLogin>;
170
+ /**
171
+ * The test title definition along with the test id
172
+ * used by Checksum to identify the test
173
+ */
174
+ defineChecksumTest: (title: string, testId: string) => string;
175
+ /**
176
+ * The Playwright expect instance enhanced by Checksum
177
+ */
178
+ expect: ChecksumExpect;
179
+ /**
180
+ * Wrapping each test action with this method allows
181
+ * Checksum to recover from failure using our test recover AI agent
182
+ */
183
+ checksumAI: (description: string, testFunction: Function) => Promise<any>;
184
+ /**
185
+ * Return environment data as defined in the checksum configuration
186
+ * given the environment name and userRole, along with a login function
187
+ * for this environment and user combination
188
+ */
189
+ getEnvironment: ({
190
+ name,
191
+ userRole,
192
+ }: {
193
+ name?: string;
194
+ userRole?: string;
195
+ }) => {
196
+ environment: ChecksumConfigEnvironment;
197
+ user: EnvironmentUser;
198
+ login: ReturnType<typeof getLogin>;
199
+ };
200
+ };
201
+ ```
202
+ ## Checksum Page API
203
+
204
+ ```js
205
+ interface ChecksumPage extends Page {
206
+ /**
207
+ * Affiliates the test step with selection data calculated during test generation and used as part of the recovery mechanism in case Playwright fails to locate the element
208
+ */
209
+ checksumSelector: (id: string) => ChecksumPage;
210
+
211
+ /**
212
+ * For tests requiring file uploads, this method resolves to the configured assets folder storing all relevant files
213
+ */
214
+ resolveAssetsFolder: (assets: string[]) => string[];
215
+
216
+ /**
217
+ * For multi-tab test flows, this method gets the ChecksumPage in order of appearance, when 0 is the initially opened tab.
218
+ */
219
+ getPage(index: number): Promise<ChecksumPage>;
220
+
221
+ /**
222
+ * When required to login mid-test, use reauthenticate with the user's role. Note: It is not possible to change environments during a single test run.
223
+ */
224
+ reauthenticate: (role: string) => Promise<void>;
225
+ }
226
+
227
+ ```
228
+
229
+ ## ChecksumLocator API
230
+
231
+ ChecksumLocator extends the existing Playwright Locator, adding the following functionality:
232
+
233
+ ```js
234
+ interface ChecksumLocator extends Locator {
235
+ /*
236
+ * Click on certain text within the canvas element the locator chain points to.
237
+ * When more than one element with the same text exists, uses the rectSizeIndex
238
+ * to resolve the target element by its bounding box size, 0 being the largest.
239
+ * Example:
240
+ * await page.locator('canvas').canvasClick('graph-point-1', 1); // clicking on the 2nd largest
241
+ */
242
+ canvasClick: (canvasText: string, rectSizeIndex?: number) => Promise<void>;
243
+ }
244
+ ```
49
245
 
50
- When generating tests, Checksum records all backend responses, allowing tests to run in the same backend context. This is particularly useful for debugging or initial test runs, especially if your testing database/context differs from that used for test generation. Note that if your backend response format changes, the mocked data may not work as expected.
51
246
 
52
- ### CLI Commands
247
+ ## CLI Commands
53
248
 
54
249
  1. `init` - Initialize the Checksum directory and configurations.
55
250
  2. `test` - Run Checksum tests. Accepts all [Playwright command line flags](https://playwright.dev/docs/test-cli). To override `checksum.config.ts`, pass full or partial JSON as a string, e.g., `--checksum-config='{"baseURL": "https://example.com"}'`.
251
+ 3. `show-report` - Locally shows the latest test run HTML report. Only applicable following completion of a test run configured to output an HTML report.
56
252
 
57
253
  ## Running with GitHub Actions
58
254