Skip to content

Commit 851e31a

Browse files
author
DavertMik
committed
updated docs for AI and effects
1 parent 25448ca commit 851e31a

File tree

4 files changed

+280
-7
lines changed

4 files changed

+280
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ This release introduces major new features and internal refactoring. It is an im
44

55
🛩️ _Features_
66

7-
### 🔥 **[Element functions](./els)**
7+
### 🔥 **Native Element Functions**
88

9-
A new API for direct element interactions has been introduced. This API provides low-level element manipulation functions for more granular control over element interactions and assertions:
9+
A new [Els API](./els) for direct element interactions has been introduced. This API provides low-level element manipulation functions for more granular control over element interactions and assertions:
1010

1111
- `element()` - perform custom operations on first matching element
1212
- `eachElement()` - iterate and perform operations on each matching element
@@ -49,7 +49,7 @@ Scenario('element functions demo', async ({ I }) => {
4949

5050
### 🔮 **Effects introduced**
5151

52-
Effects is a new concept that encompasses all functions that can modify scenario flow. These functions are now part of a single module. Previously, they were used via plugins like `tryTo` and `retryTo`. Now, it is recommended to import them directly:
52+
[Effects](./effects) is a new concept that encompasses all functions that can modify scenario flow. These functions are now part of a single module. Previously, they were used via plugins like `tryTo` and `retryTo`. Now, it is recommended to import them directly:
5353

5454
```js
5555
const { tryTo, retryTo } = require('codeceptjs/effects')

docs/ai.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ai: {
8585
const openai = new OpenAI({ apiKey: process.env['OPENAI_API_KEY'] })
8686

8787
const completion = await openai.chat.completions.create({
88-
model: 'gpt-3.5-turbo-0125',
88+
model: 'gpt-3.5-turbo',
8989
messages,
9090
})
9191

@@ -354,6 +354,131 @@ npx codeceptjs run --ai
354354
When execution finishes, you will receive information on token usage and code suggestions proposed by AI.
355355
By evaluating this information you will be able to check how effective AI can be for your case.
356356
357+
## Analyze Results
358+
359+
When running tests with AI enabled, CodeceptJS can automatically analyze test failures and provide insights. The analyze plugin helps identify patterns in test failures and provides detailed explanations of what went wrong.
360+
361+
Enable the analyze plugin in your config:
362+
363+
```js
364+
plugins: {
365+
analyze: {
366+
enabled: true,
367+
// analyze up to 3 failures in detail
368+
analyze: 3,
369+
// group similar failures when 5 or more tests fail
370+
clusterize: 5,
371+
// enable screenshot analysis (requires modal that can analyze screenshots)
372+
vision: false
373+
}
374+
}
375+
```
376+
377+
When tests are executed with `--ai` flag, the analyze plugin will:
378+
379+
**Analyze Individual Failures**: For each failed test (up to the `analyze` limit), it will:
380+
381+
- Examine the error message and stack trace
382+
- Review the test steps that led to the failure
383+
- Provide a detailed explanation of what likely caused the failure
384+
- Suggest possible fixes and improvements
385+
386+
Sample Analysis report:
387+
388+
When analyzing individual failures (less than `clusterize` threshold), the output looks like this:
389+
390+
```
391+
🪄 AI REPORT:
392+
--------------------------------
393+
→ Cannot submit registration form with invalid email 👀
394+
395+
* SUMMARY: Form submission failed due to invalid email format, system correctly shows validation message
396+
* ERROR: expected element ".success-message" to be visible, but it is not present in DOM
397+
* CATEGORY: Data errors (password incorrect, no options in select, invalid format, etc)
398+
* STEPS: I.fillField('#email', 'invalid-email'); I.click('Submit'); I.see('.success-message')
399+
* URL: /register
400+
401+
```
402+
403+
> The 👀 emoji indicates that screenshot analysis was performed (when `vision: true`).
404+
405+
**Cluster Similar Failures**: When number of failures exceeds the `clusterize` threshold:
406+
407+
- Groups failures with similar error patterns
408+
- Identifies common root causes
409+
- Suggests fixes that could resolve multiple failures
410+
- Helps prioritize which issues to tackle first
411+
412+
**Categorize Failures**: Automatically classifies failures into categories like:
413+
414+
- Browser/connection issues
415+
- Network errors
416+
- Element locator problems
417+
- Navigation errors
418+
- Code errors
419+
- Data validation issues
420+
- etc.
421+
422+
Clusterization output:
423+
424+
```
425+
🪄 AI REPORT:
426+
_______________________________
427+
428+
## Group 1 🔍
429+
430+
* SUMMARY: Element locator failures across login flow
431+
* CATEGORY: HTML / page elements (not found, not visible, etc)
432+
* ERROR: Element "#login-button" is not visible
433+
* STEP: I.click('#login-button')
434+
* SUITE: Authentication
435+
* TAG: @login
436+
* AFFECTED TESTS (4):
437+
x Cannot login with valid credentials
438+
x Should show error on invalid login
439+
x Login button should be disabled when form empty
440+
x Should redirect to dashboard after login
441+
442+
## Group 2 🌐
443+
444+
* SUMMARY: API timeout issues during user data fetch
445+
* CATEGORY: Network errors (server error, timeout, etc)
446+
* URL: /api/v1/users
447+
* ERROR: Request failed with status code 504, Gateway Timeout
448+
* SUITE: User Management
449+
* AFFECTED TESTS (3):
450+
x Should load user profile data
451+
x Should display user settings
452+
x Should fetch user notifications
453+
454+
## Group 3 ⚠️
455+
456+
* SUMMARY: Form validation errors on registration page
457+
* CATEGORY: Data errors (password incorrect, no options in select, invalid format, etc)
458+
* ERROR: Expected field "password" to have error "Must be at least 8 characters"
459+
* STEP: I.see('Must be at least 8 characters', '.error-message')
460+
* SUITE: User Registration
461+
* TAG: @registration
462+
* AFFECTED TESTS (2):
463+
x Should validate password requirements
464+
x Should show all validation errors on submit
465+
```
466+
467+
If `vision: true` is enabled and your tests take screenshots on failure, the plugin will also analyze screenshots to provide additional visual context about the failure.
468+
469+
The analysis helps teams:
470+
471+
- Quickly understand the root cause of failures
472+
- Identify patterns in failing tests
473+
- Prioritize fixes based on impact
474+
- Maintain more stable test suites
475+
476+
Run tests with both AI and analyze enabled:
477+
478+
```bash
479+
npx codeceptjs run --ai
480+
```
481+
357482
## Arbitrary Prompts
358483
359484
What if you want to take AI on the journey of test automation and ask it questions while browsing pages?

docs/effects.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Effects
2+
3+
Effects are functions that can modify scenario flow. They provide ways to handle conditional steps, retries, and test flow control.
4+
5+
## Installation
6+
7+
Effects can be imported directly from CodeceptJS:
8+
9+
```js
10+
const { tryTo, retryTo, within } = require('codeceptjs/effects')
11+
```
12+
13+
> 📝 Note: Prior to v3.7, `tryTo` and `retryTo` were available globally via plugins. This behavior is deprecated and will be removed in v4.0.
14+
15+
## tryTo
16+
17+
The `tryTo` effect allows you to attempt steps that may fail without stopping test execution. It's useful for handling optional steps or conditions that aren't critical for the test flow.
18+
19+
```js
20+
const { tryTo } = require('codeceptjs/effects')
21+
22+
// inside a test
23+
const success = await tryTo(() => {
24+
// These steps may fail but won't stop the test
25+
I.see('Cookie banner')
26+
I.click('Accept cookies')
27+
})
28+
29+
if (!success) {
30+
I.say('Cookie banner was not found')
31+
}
32+
```
33+
34+
If the steps inside `tryTo` fail:
35+
36+
- The test will continue execution
37+
- The failure will be logged in debug output
38+
- `tryTo` returns `false`
39+
- Auto-retries are disabled inside `tryTo` blocks
40+
41+
## retryTo
42+
43+
The `retryTo` effect allows you to retry a set of steps multiple times until they succeed. This is useful for handling flaky elements or conditions that may need multiple attempts.
44+
45+
```js
46+
const { retryTo } = require('codeceptjs/effects')
47+
48+
// Retry up to 5 times with 200ms between attempts
49+
await retryTo(() => {
50+
I.switchTo('#editor-frame')
51+
I.fillField('textarea', 'Hello world')
52+
}, 5)
53+
```
54+
55+
Parameters:
56+
57+
- `callback` - Function containing steps to retry
58+
- `maxTries` - Maximum number of retry attempts
59+
- `pollInterval` - (optional) Delay between retries in milliseconds (default: 200ms)
60+
61+
The callback receives the current retry count as an argument:
62+
63+
```js
64+
const { retryTo } = require('codeceptjs/effects')
65+
66+
// inside a test...
67+
await retryTo(tries => {
68+
I.say(`Attempt ${tries}`)
69+
I.click('Submit')
70+
I.see('Success')
71+
}, 3)
72+
```
73+
74+
## within
75+
76+
The `within` effect allows you to perform multiple steps within a specific context (like an iframe or modal):
77+
78+
```js
79+
const { within } = require('codeceptjs/effects')
80+
81+
// inside a test...
82+
83+
within('.modal', () => {
84+
I.see('Modal title')
85+
I.click('Close')
86+
})
87+
```
88+
89+
## Usage with TypeScript
90+
91+
Effects are fully typed and work well with TypeScript:
92+
93+
```ts
94+
import { tryTo, retryTo, within } from 'codeceptjs/effects'
95+
96+
const success = await tryTo(async () => {
97+
await I.see('Element')
98+
})
99+
```
100+
101+
This documentation covers the main effects functionality while providing practical examples and important notes about deprecation and future changes. Let me know if you'd like me to expand any section or add more examples!

lib/plugin/analyze.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const defaultConfig = {
6060
6161
If there is no groups of tests, say: "No patterns found"
6262
Preserve error messages but cut them if they are too long.
63-
Respond clearly and directly, without introductory words or phrases like Of course,’ ‘Here is the answer, etc.
63+
Respond clearly and directly, without introductory words or phrases like 'Of course,' 'Here is the answer,' etc.
6464
Do not list more than 3 errors in the group.
6565
If you identify that all tests in the group have the same tag, add this tag to the group report, otherwise ignore TAG section.
6666
If you identify that all tests in the group have the same suite, add this suite to the group report, otherwise ignore SUITE section.
@@ -160,9 +160,56 @@ const defaultConfig = {
160160
}
161161

162162
/**
163+
* CodeceptJS Analyze Plugin - Uses AI to analyze test failures and provide insights
163164
*
164-
* @param {*} config
165-
* @returns
165+
* This plugin analyzes failed tests using AI to provide detailed explanations and group similar failures.
166+
* When enabled with --ai flag, it generates reports after test execution.
167+
*
168+
* #### Usage
169+
*
170+
* ```js
171+
* // in codecept.conf.js
172+
* exports.config = {
173+
* plugins: {
174+
* analyze: {
175+
* enabled: true,
176+
* clusterize: 5,
177+
* analyze: 2,
178+
* vision: false
179+
* }
180+
* }
181+
* }
182+
* ```
183+
*
184+
* #### Configuration
185+
*
186+
* * `clusterize` (number) - minimum number of failures to trigger clustering analysis. Default: 5
187+
* * `analyze` (number) - maximum number of individual test failures to analyze in detail. Default: 2
188+
* * `vision` (boolean) - enables visual analysis of test screenshots. Default: false
189+
* * `categories` (array) - list of failure categories for classification. Defaults to:
190+
* - Browser connection error / browser crash
191+
* - Network errors (server error, timeout, etc)
192+
* - HTML / page elements (not found, not visible, etc)
193+
* - Navigation errors (404, etc)
194+
* - Code errors (syntax error, JS errors, etc)
195+
* - Library & framework errors
196+
* - Data errors (password incorrect, invalid format, etc)
197+
* - Assertion failures
198+
* - Other errors
199+
* * `prompts` (object) - customize AI prompts for analysis
200+
* - `clusterize` - prompt for clustering analysis
201+
* - `analyze` - prompt for individual test analysis
202+
*
203+
* #### Features
204+
*
205+
* * Groups similar failures when number of failures >= clusterize value
206+
* * Provides detailed analysis of individual failures
207+
* * Analyzes screenshots if vision=true and screenshots are available
208+
* * Classifies failures into predefined categories
209+
* * Suggests possible causes and solutions
210+
*
211+
* @param {Object} config - Plugin configuration
212+
* @returns {void}
166213
*/
167214
module.exports = function (config = {}) {
168215
config = Object.assign(defaultConfig, config)

0 commit comments

Comments
 (0)