Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ yarn.lock
/.vs
typings/types.d.ts
typings/promiseBasedTypes.d.ts
reflection/
reflection/
skills/
138 changes: 8 additions & 130 deletions docs/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ So, instead of asking "write me a test" it can ask "write a test for **this** pa
CodeceptJS AI can do the following:

- 🏋️‍♀️ **assist writing tests** in `pause()` or interactive shell mode
- 📃 **generate page objects** in `pause()` or interactive shell mode
- 🚑 **self-heal failing tests** (can be used on CI)
- 💬 send arbitrary prompts to AI provider from any tested page attaching its HTML contents

![](/img/fill_form.gif)

Expand Down Expand Up @@ -60,7 +58,7 @@ import { openai } from '@ai-sdk/openai'
export default {
// ... other config
ai: {
model: openai('gpt-4o-mini'),
model: openai('gpt-5'),
},
}
```
Expand Down Expand Up @@ -94,7 +92,7 @@ import { openai } from '@ai-sdk/openai'

export default {
ai: {
model: openai('gpt-4o-mini'),
model: openai('gpt-5'),
// or use gpt-4o, gpt-3.5-turbo, etc.
},
}
Expand All @@ -121,8 +119,8 @@ import { anthropic } from '@ai-sdk/anthropic'

export default {
ai: {
model: anthropic('claude-3-5-sonnet-20241022'),
// or use claude-3-opus-20240229, claude-3-haiku-20240307, etc.
model: anthropic('claude-sonnet-4-6'),
// or use claude-opus-4-7, claude-haiku-4-5, etc.
},
}
```
Expand Down Expand Up @@ -232,10 +230,9 @@ npx codeceptjs generate:heal
Heal recipes should be included into `codecept.conf.js` or `codecept.conf.ts` config file:

```js
import './heal.js'

require('./heal')

exports.config = {
export const config = {
// ... your codeceptjs config
```

Expand Down Expand Up @@ -385,125 +382,6 @@ Run tests with both AI and analyze enabled:
npx codeceptjs run --ai
```

## Arbitrary Prompts

What if you want to take AI on the journey of test automation and ask it questions while browsing pages?

This is possible with the new `AI` helper. Enable it in your config file in `helpers` section:

```js
// inside codecept.conf
helpers: {
// Playwright, Puppeteer, or WebDrver helper should be enabled too
Playwright: {
},

AI: {}
}
```

AI helper will be automatically attached to Playwright, WebDriver, or another web helper you use. It includes the following methods:

- `askGptOnPage` - sends GPT prompt attaching the HTML of the page. Large pages will be split into chunks, according to `chunkSize` config. You will receive responses for all chunks.
- `askGptOnPageFragment` - sends GPT prompt attaching the HTML of the specific element. This method is recommended over `askGptOnPage` as you can reduce the amount of data to be processed.
- `askGptGeneralPrompt` - sends GPT prompt without HTML.
- `askForPageObject` - creates PageObject for you, explained in next section.

`askGpt` methods won't remove non-interactive elements, so it is recommended to manually control the size of the sent HTML.

Here are some good use cases for this helper:

- get page summaries
- inside pause mode navigate through your application and ask to document pages
- etc...

```js
// use it inside test or inside interactive pause
// pretend you are technical writer asking for documentation
const pageDoc = await I.askGptOnPageFragment('Act as technical writer, describe what is this page for', '#container')
```

As of now, those use cases do not apply to test automation but maybe you can apply them to your testing setup.

## Generate PageObjects

Last but not the least. AI helper can be used to quickly prototype PageObjects on pages browsed within interactive session.

![](/img/ai_page_object.png)

Enable AI helper as explained in previous section and launch shell:

```
npx codeceptjs shell --ai
```

Also this is availble from `pause()` if AI helper is enabled,

Ensure that browser is started in window mode, then browse the web pages on your site.
On a page you want to create PageObject execute `askForPageObject()` command. The only required parameter is the name of a page:

```js
I.askForPageObject('login')
```

This command sends request to AI provider should create valid CodeceptJS PageObject.
Run it few times or switch AI provider if response is not satisfactory to you.

> You can change the style of PageObject and locator preferences by adjusting prompt in a config file

When completed successfully, page object is saved to **output** directory and loaded into the shell as `page` variable so locators and methods can be checked on the fly.

If page object has `signInButton` locator you can quickly check it by typing:

```js
I.click(page.signInButton)
```

If page object has `clickForgotPassword` method you can execute it as:

```js
=> page.clickForgotPassword()
```

Here is an example of a session:

```shell
Page object for login is saved to .../output/loginPage-1718579784751.js
Page object registered for this session as `page` variable
Use `=>page.methodName()` in shell to run methods of page object
Use `click(page.locatorName)` to check locators of page object

I.=>page.clickSignUp()
I.click(page.signUpLink)
I.=> page.enterPassword('asdasd')
I.=> page.clickSignIn()
```

You can improve prompt by passing custom request as a second parameter:

```js
I.askForPageObject('login', 'implement signIn(username, password) method')
```

To generate page object for the part of a page, pass in root locator as third parameter.

```js
I.askForPageObject('login', '', '#auth')
```

In this case, all generated locators, will use `#auth` as their root element.

Don't aim for perfect PageObjects but find a good enough one, which you can use for writing your tests.
All created page objects are considered temporary, that's why saved to `output` directory.

Rename created PageObject to remove timestamp and move it from `output` to `pages` folder and include it into codecept.conf file:

```js
include: {
loginPage: "./pages/loginPage.js",
// ...
```

## Advanced Configuration

AI prompts and HTML compression can be configured inside `ai` section of `codecept.conf` file:
Expand Down Expand Up @@ -612,8 +490,8 @@ It is recommended to try HTML processing on one of your web pages before launchi
To do that open the common page of your application and using DevTools copy the outerHTML of `<html>` element. Don't use `Page Source` for that, as it may not include dynamically added HTML elements. Save this HTML into a file and create a NodeJS script:

```js
const { removeNonInteractiveElements } = require('codeceptjs/lib/html')
const fs = require('fs')
import { removeNonInteractiveElements } from 'codeceptjs/lib/html'
import fs from 'fs'

const htmlOpts = {
interactiveElements: ['a', 'input', 'button', 'select', 'textarea', 'label', 'option'],
Expand Down
8 changes: 4 additions & 4 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,12 @@ Or you can use the browser cookies if you are running browser session.
In this case use `setSharedCookies()` from `@codeceptjs/configure` package:

```js
const { setSharedCookies } = require('@codeceptjs/configure');
import { setSharedCookies } from '@codeceptjs/configure'

// add this before exports.config
setSharedCookies();
// call before exporting config
setSharedCookies()

exports.config = {
export const config = {
// ...
helpers: {
// also works with Playwright or Puppeteer
Expand Down
Loading
Loading