You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/ai.md
+103-4
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,7 @@ So, instead of asking "write me a test" it can ask "write a test for **this** pa
23
23
CodeceptJS AI can do the following:
24
24
25
25
* 🏋️♀️ **assist writing tests** in `pause()` or interactive shell mode
26
+
* 📃 **generate page objects** in `pause()` or interactive shell mode
26
27
* 🚑 **self-heal failing tests** (can be used on CI)
27
28
* 💬 send arbitrary prompts to AI provider from any tested page attaching its HTML contents
28
29
@@ -260,15 +261,29 @@ By evaluating this information you will be able to check how effective AI can be
260
261
261
262
### Arbitrary GPT Prompts
262
263
263
-
What if you want to take ChatGPT on the journey of test automation and ask it questions while browsing pages?
264
+
What if you want to take AI on the journey of test automation and ask it questions while browsing pages?
264
265
265
-
This is possible with the new `AI` helper. Enable it in your config and it will automatically attach to Playwright, WebDriver, or another web helper you use. It includes the following methods:
266
+
This is possible with the new `AI` helper. Enable it in your config file in `helpers` section:
267
+
268
+
```js
269
+
// inside codecept.conf
270
+
helpers: {
271
+
// Playwright, Puppeteer, or WebDrver helper should be enabled too
272
+
Playwright: {
273
+
},
274
+
275
+
AI: {}
276
+
}
277
+
```
278
+
279
+
AI helper will be automatically attached to Playwright, WebDriver, or another web helper you use. It includes the following methods:
266
280
267
281
* `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.
268
282
* `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.
269
283
* `askGptGeneralPrompt` - sends GPT prompt without HTML.
284
+
* `askForPageObject` - creates PageObject for you, explained in next section.
270
285
271
-
OpenAI helper won't remove non-interactive elements, so it is recommended to manually control the size of the sent HTML.
286
+
`askGpt` methods won't remove non-interactive elements, so it is recommended to manually control the size of the sent HTML.
272
287
273
288
Here are some good use cases for this helper:
274
289
@@ -282,7 +297,84 @@ Here are some good use cases for this helper:
282
297
constpageDoc=awaitI.askGptOnPageFragment('Act as technical writer, describe what is this page for', '#container');
283
298
```
284
299
285
-
As of now, those use cases do not apply to test automation but maybe you can apply them to your testing setup.
300
+
As of now, those use cases do not apply to test automation but maybe you can apply them to your testing setup.
301
+
302
+
## Generate PageObjects
303
+
304
+
Last but not the least. AI helper can be used to quickly prototype PageObjects on pages browsed within interactive session.
305
+
306
+

307
+
308
+
Enable AI helper as explained in previous section and launch shell:
309
+
310
+
```
311
+
npx codeceptjs shell --ai
312
+
```
313
+
314
+
Also this is availble from `pause()` if AI helper is enabled,
315
+
316
+
Ensure that browser is started in window mode, then browse the web pages on your site.
317
+
On a page you want to create PageObject execute `askForPageObject()` command. The only required parameter is the name of a page:
318
+
319
+
```js
320
+
I.askForPageObject('login')
321
+
```
322
+
323
+
This command sends request to AI provider should create valid CodeceptJS PageObject.
324
+
Run it few times or switch AI provider if response is not satisfactory to you.
325
+
326
+
> You can change the style of PageObject and locator preferences by adjusting prompt in a config file
327
+
328
+
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.
329
+
330
+
If page object has `signInButton` locator you can quickly check it by typing:
331
+
332
+
```js
333
+
I.click(page.signInButton)
334
+
```
335
+
336
+
If page object has `clickForgotPassword` method you can execute it as:
337
+
338
+
```js
339
+
=>page.clickForgotPassword()
340
+
```
341
+
342
+
```shell
343
+
Page object for login is saved to .../output/loginPage-1718579784751.js
344
+
Page object registered forthis session as `page` variable
345
+
Use `=>page.methodName()`in shell to run methods of page object
346
+
Use `click(page.locatorName)` to check locators of page object
347
+
348
+
I.=>page.clickSignUp()
349
+
I.click(page.signUpLink)
350
+
I.=>page.enterPassword('asdasd')
351
+
I.=>page.clickSignIn()
352
+
```
353
+
354
+
You can improve prompt by passing custom request as a second parameter:
Copy file name to clipboardexpand all lines: docs/pageobjects.md
+2
Original file line number
Diff line number
Diff line change
@@ -56,6 +56,8 @@ module.exports = function() {
56
56
57
57
## PageObject
58
58
59
+
> ✨ CodeceptJS can [generate PageObjects using AI](/ai#generate-pageobjects). It fetches all interactive elements from a page, generates locators and methods page and writes JS code. Generated page object can be tested on the fly within the same browser session.
60
+
59
61
If an application has different pages (login, admin, etc) you should use a page object.
60
62
CodeceptJS can generate a template for it with the following command:
content: `As a test automation engineer I am creating a Page Object for a web application using CodeceptJS.
38
+
Here is an sample page object:
39
+
40
+
const { I } = inject();
41
+
42
+
module.exports = {
43
+
44
+
// setting locators
45
+
element1: '#selector',
46
+
element2: '.selector',
47
+
element3: locate().withText('text'),
48
+
49
+
// seting methods
50
+
doSomethingOnPage(params) {
51
+
// ...
52
+
},
53
+
}
54
+
55
+
I want to generate a Page Object for the page I provide.
56
+
Write JavaScript code in similar manner to list all locators on the page.
57
+
Use locators in order of preference: by text (use locate().withText()), label, CSS, XPath.
58
+
Avoid TailwindCSS, Bootstrap or React style formatting classes in locators.
59
+
Add methods to to interact with page when needed.
60
+
${extraPrompt}
61
+
${rootLocator ? `All provided elements are inside '${rootLocator}'. Declare it as root variable and for every locator use locate(...).inside(root)` : ''}
* This helper class provides integration with the AI GPT-3.5 or 4 language model for generating responses to questions or prompts within the context of web pages. It allows you to interact with the GPT-3.5 model to obtain intelligent responses based on HTML fragments or general prompts.
11
17
* This helper should be enabled with any web helpers like Playwright or Puppeteer or WebDrvier to ensure the HTML context is available.
12
18
*
19
+
* Use it only in development mode. It is recommended to run it only inside pause() mode.
20
+
*
13
21
* ## Configuration
14
22
*
15
23
* This helper should be configured in codecept.json or codecept.conf.js
@@ -66,9 +74,9 @@ class AI extends Helper {
66
74
67
75
if(htmlChunks.length>1)messages.push({role: 'user',content: 'If action is not possible on this page, do not propose anything, I will send another HTML fragment'});
0 commit comments