From 54bd14e578acaacbd33ca786e504255baf8f4f0b Mon Sep 17 00:00:00 2001 From: DavertMik Date: Mon, 18 May 2026 00:03:12 +0300 Subject: [PATCH 1/3] minor docs update --- docs/ai.md | 54 +------ docs/tutorial.md | 362 +++++++++++++++++++++++++++-------------------- 2 files changed, 210 insertions(+), 206 deletions(-) diff --git a/docs/ai.md b/docs/ai.md index 60a4f8a19..998511a2d 100644 --- a/docs/ai.md +++ b/docs/ai.md @@ -1,15 +1,15 @@ --- permalink: /ai -title: Testing with AI 🪄 +title: Testing with AI --- -# 🪄 Testing with AI +# Testing with AI **CodeceptJS is the first open-source test automation framework with AI** features to improve the testing experience. CodeceptJS uses AI provider like OpenAI or Anthropic to auto-heal failing tests, assist in writing tests, and more... Think of it as your testing co-pilot built into the testing framework -> 🪄 **AI features for testing are experimental**. AI works only for web based testing with Playwright, WebDriver, etc. Those features will be improved based on user's experience. +> This is guide on using AI features inside CodeceptJS. To control CodeceptJS via AI Agents, see [Agentic Testing Guide](/agents/). ## How AI Improves Automated Testing @@ -164,50 +164,6 @@ The AI SDK supports 20+ providers including: See [AI SDK Providers](https://ai-sdk.dev/docs/foundations/providers-and-models) for complete list and configuration details. -## Writing Tests with AI Copilot - -If AI features are enabled when using [interactive pause](/basics/#debug) with `pause()` command inside tests: - -For instance, let's create a test to try ai features via `gt` command: - -``` -npx codeceptjs gt -``` - -Name a test and write the code. We will use `Scenario.only` instead of Scenario to execute only this exact test. - -```js -Feature('ai') - -Scenario.only('test ai features', ({ I }) => { - I.amOnPage('https://getbootstrap.com/docs/5.1/examples/checkout/') - pause() -}) -``` - -Now run the test in debug mode with AI enabled: - -``` -npx codeceptjs run --debug --ai -``` - -When pause mode started you can ask GPT to fill in the fields on this page. Use natural language to describe your request, and provide enough details that AI could operate with it. It is important to include at least a space char in your input, otherwise, CodeceptJS will consider the input to be JavaScript code. - -``` - I.fill checkout form with valid values without submitting it -``` - -![](/img/fill_form_1.png) - -GPT will generate code and data and CodeceptJS will try to execute its code. If it succeeds, the code will be saved to history and you will be able to copy it to your test. - -![](/img/fill_form2.png) - -This AI copilot works best with long static forms. In the case of complex and dynamic single-page applications, it may not perform as well, as the form may not be present on HTML page yet. For instance, interacting with calendars or inputs with real-time validations (like credit cards) can not yet be performed by AI. - -Please keep in mind that GPT can't react to page changes and operates with static text only. This is why it is not ready yet to write the test completely. However, if you are new to CodeceptJS and automated testing AI copilot may help you write tests more efficiently. - -> 👶 Enable AI copilot for junior test automation engineers. It may help them to get started with CodeceptJS and to write good semantic locators. ## Self-Healing Tests @@ -409,9 +365,7 @@ ai: { CodeceptJS uses three main prompts for AI features: -- `writeStep` - generates test code in interactive pause mode - `healStep` - suggests fixes for failing tests -- `generatePageObject` - creates page objects from HTML To customize a prompt, generate it using: @@ -452,9 +406,7 @@ You can also override prompts programmatically in config: ```js ai: { prompts: { - writeStep: (html, input) => [{ role: 'user', content: 'As a test engineer...' }] healStep: (html, { step, error, prevSteps }) => [{ role: 'user', content: 'As a test engineer...' }] - generatePageObject: (html, extraPrompt = '', rootLocator = null) => [{ role: 'user', content: 'As a test engineer...' }] } } ``` diff --git a/docs/tutorial.md b/docs/tutorial.md index 13a44397a..3bfc2611c 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -3,45 +3,38 @@ permalink: /tutorial title: CodeceptJS Complete Tutorial --- -# Tutorial: Writing Tests for Checkout Page +# Tutorial: Testing a Checkout Page -**[CodeceptJS](https://codecept.io) is a popular open-source testing framework** for JavaScript. It is designed to simplify writing and maintain end-to-end tests for web applications, using a readable and intuitive syntax. To run tests in browser it uses **[Playwright](https://playwright.dev)** by default but ca execute tests via WebDriver, Puppeteer or Appium. +**[CodeceptJS](https://codecept.io) is a popular open-source end-to-end testing framework** for JavaScript. It is designed to make web tests readable and easy to maintain by writing them as a linear scenario of user actions. By default it drives the browser with **[Playwright](https://playwright.dev)**, but the same tests can run via WebDriver, Puppeteer, or Appium without changes. -## Let's get CodeceptJS installed! +In this tutorial we write a real, runnable test for the **[Bootstrap checkout example](https://getbootstrap.com/docs/4.0/examples/checkout/)** — a public page with a billing and payment form. By the end you will have a clean test and a reusable page object. -To install CodeceptJS, you will need to have Node.js and npm (the Node.js package manager) installed on your system. You can check if you already have these tools installed by running the following commands in a terminal: +## Install CodeceptJS + +You need Node.js (and npm) installed. Check with: ```bash node --version npm --version ``` -If either of these commands return an error, you will need to install Node.js and npm before you can install CodeceptJS. You can download and install the latest version of Node.js from the official website, which includes npm. - -To install CodeceptJS create a new folder and run command form terminal: +Create a new folder, then install CodeceptJS together with Playwright: +```bash +npm init -y +npm install codeceptjs playwright --save-dev +npx playwright install --with-deps ``` -npx create-codeceptjs . -``` - -If you run the npx create-codeceptjs . command, it will install CodeceptJS with Playwright in the current directory. -> The `npx` command is a tool that comes with npm (the Node.js package manager) and it allows you to run npm packages without having to install them globally on your system. +`npx playwright install` downloads the Chromium, Firefox, and WebKit browsers; `--with-deps` also installs the system libraries they need. -It may take some time as it downloads browsers: Chrome, Firefox and Safari and creates a demo project. +Now scaffold the project: -But we are here to write a checkout test, right? - -Let's initialize a new project for that! - -Run - -``` +```bash npx codeceptjs init ``` -Agree on defaults (press Enter for every question asked). When asked for base site URL, provide a URL of a ecommerce website you are testing. For instance, it could be: `https://myshop.com` if you test already published website or `http://localhost` if you run the website locally. -When asked for a test name and suite name write "Checkout". It will create the following dirctory structure: +`init` runs a short wizard. Accept the defaults — when asked for the **base URL** enter `https://getbootstrap.com`, and name the first test **Checkout**. This creates: ``` . @@ -50,9 +43,31 @@ When asked for a test name and suite name write "Checkout". It will create the f └── Checkout_test.js ``` -The `codecept.conf.js` file in the root of the project directory contains the global configuration settings for CodeceptJS. +`codecept.conf.js` holds the project configuration. Because CodeceptJS 4.x uses **ES modules**, the config and tests use `import`/`export` syntax — `init` sets `"type": "module"` in `package.json` for you. -Now open a test: +Open `codecept.conf.js`. The two settings that matter here are the helper and the base URL: + +```js +import { setHeadlessWhen } from '@codeceptjs/configure' + +// show the browser locally, run headless on CI +setHeadlessWhen(process.env.CI) + +export const config = { + tests: './*_test.js', + output: './output', + helpers: { + Playwright: { + url: 'https://getbootstrap.com', + browser: 'chromium', + }, + }, +} +``` + +## Your First Test + +Open `Checkout_test.js`: ```js Feature('Checkout'); @@ -60,212 +75,249 @@ Feature('Checkout'); Scenario('test something', ({ I }) => { }); ``` -Inside the Scenario block you write a test. -Add `I.amOnPage('/')` into it. It will open the browser on a URL you specified as a base. +A test lives inside a `Scenario` block. Let's open the checkout page: ```js Feature('Checkout'); Scenario('test something', ({ I }) => { - I.amOnPage('/') + I.amOnPage('/docs/4.0/examples/checkout/'); }); ``` -But you may want to ask... - -## What is I? -Glad you asked! +`I.amOnPage()` navigates the browser. Because the path is relative, it is appended to the base URL from the config — keep the base URL in config so you can switch between staging and production without touching tests. -In CodeceptJS, the `I` object is used to represent the user performing actions in a test scenario. It provides a number of methods (also known as actions) that can be used to simulate user interactions with the application under test. +But you may be wondering... -Some of the most popular actions of the I object are: +### What is `I`? -* `I.amOnPage(url)`: This action navigates the user to the specified URL. -* `I.click(locator)`: This action simulates a click on the element identified by the given locator. -* `I.fillField(field, value)`: This action fills the specified field with the given value. -* `I.see(text, context)`: This action checks that the given text is visible on the page (or in the specified context). -* `I.selectOption(select, option)`: This action selects the specified option from the given select dropdown. -* `I.waitForElement(locator, timeout)`: This action waits for the specified element to appear on the page, up to the given timeout. -* `I.waitForText(text, timeout, context)`: This action waits for the given text to appear on the page (or in the specified context), up to the given timeout. +In CodeceptJS the `I` object is the **actor** — it represents the user performing actions. It exposes methods (called *actions*) that simulate interactions with the app: -We will need to use them to navigate into Checkout process. How do we navigate web? Sure by clicking on links! +- `I.amOnPage(url)` — navigate to a URL +- `I.click(locator)` — click an element +- `I.fillField(field, value)` — type into an input +- `I.selectOption(select, option)` — choose an option in a dropdown +- `I.checkOption(locator)` — tick a checkbox or radio +- `I.see(text)` — assert that text is visible +- `I.seeInField(field, value)` — assert an input holds a value -Let's use `I.click()` for that. +CodeceptJS **waits automatically** before clicking, filling, and most other actions, so you rarely need explicit waits. Steps also write themselves into a promise chain, so you usually **don't need `await`** for regular actions — only for `grab*` actions and page object methods that return data. -But how we can access elements on a webpage? +### Locating Elements -CodeceptJS is smart enough to locate clickable elements by their visible text. For instance, if on your ecommerce website you have a product 'Coffee Cup' with that exact name you can use +Most actions accept a locator. CodeceptJS supports several strategies — prefer the readable ones: ```js -I.click('Coffee Cup'); -``` - -But sometimes elements are not as easy to locate, so you can use CSS or XPath locators to locate them. +// by visible text / label +I.click('Continue to checkout'); +I.fillField('First name', 'John'); -For instance, locating Coffee Cup via CSS can take into accont HTML structure of a page and element attributes. For instance, it can be like this: +// by ARIA role and accessible name (resilient to CSS changes) +I.click({ role: 'button', name: 'Continue to checkout' }); -```js -I.click('div.products a.product-name[title="Coffee Cup"]'); +// by CSS or XPath, when nothing semantic is available +I.fillField('#email', 'john@example.com'); ``` -In this example, the `div.products` part of the selector specifies a div element with the `products` class, and the `a.product-name[title="Coffee Cup"]` part specifies an a element with `the product-name` class and the `title` attribute set to Coffee Cup. +> **Best practice:** prefer labels and ARIA locators (`{ role, name }`). They survive styling changes and document intent. Fall back to CSS/XPath only when needed. -You can read more about HTML and CSS locators, and basically that's all what you need to know to start writing a checkout test! +## Writing the Checkout Test -## Get back to Checkout - -Let's see how a regular checkout script may look in CodeceptJS: +The Bootstrap checkout form has billing fields, country/state selects, and a payment section. CodeceptJS finds inputs by their visible `