Top Related Projects
Fast, easy and reliable testing for anything that runs in a browser.
JavaScript API for Chrome and Firefox
Next-gen browser and mobile automation test framework for Node.js
A browser automation framework and ecosystem.
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
Quick Overview
CodeceptJS is a modern end-to-end testing framework for web applications. It provides a high-level, scenario-driven approach to writing tests, allowing developers to describe user actions in a natural language-like syntax. CodeceptJS supports multiple testing backends, including Playwright, Puppeteer, and Selenium WebDriver.
Pros
- Easy-to-read and maintain test scenarios using a behavior-driven development (BDD) approach
- Supports multiple testing backends, providing flexibility in choosing the right tool for the job
- Built-in reporting and debugging tools for improved test analysis
- Active community and regular updates
Cons
- Steeper learning curve for developers accustomed to traditional testing frameworks
- Limited support for certain advanced scenarios compared to native backend APIs
- Performance overhead due to the abstraction layer, especially for large test suites
- Some features may require additional configuration or plugins
Code Examples
- Basic test scenario:
Feature('My First Test');
Scenario('test something', ({ I }) => {
I.amOnPage('https://github.com');
I.see('GitHub');
I.fillField('input[name=q]', 'CodeceptJS');
I.click('Search');
I.see('codeceptjs/CodeceptJS', 'a');
});
- Using custom helper methods:
const { I } = inject();
Given('I am on the homepage', () => {
I.amOnPage('/');
});
When('I search for {string}', (searchTerm) => {
I.fillField('input[name=q]', searchTerm);
I.click('Search');
});
Then('I should see {string} in the results', (expectedResult) => {
I.see(expectedResult, '.search-results');
});
- API testing example:
Feature('API Testing');
Scenario('test API response', ({ I }) => {
const response = await I.sendGetRequest('/api/users');
I.seeResponseCodeIs(200);
I.seeResponseContainsJson({ status: 'success' });
});
Getting Started
- Install CodeceptJS:
npm install codeceptjs playwright --save-dev
- Initialize CodeceptJS project:
npx codeceptjs init
- Create a test file (e.g.,
first_test.js
):
Feature('My First Test');
Scenario('test something', ({ I }) => {
I.amOnPage('https://github.com');
I.see('GitHub');
});
- Run the test:
npx codeceptjs run
Competitor Comparisons
Fast, easy and reliable testing for anything that runs in a browser.
Pros of Cypress
- Built-in automatic waiting and retry mechanisms for more stable tests
- Extensive, user-friendly documentation with interactive examples
- Real-time test execution with time-travel debugging
Cons of Cypress
- Limited to testing web applications within Chromium-based browsers
- Lacks built-in support for multi-tab testing
- More challenging to integrate with existing Selenium-based test suites
Code Comparison
Cypress example:
describe('Login', () => {
it('should log in successfully', () => {
cy.visit('/login')
cy.get('#username').type('user@example.com')
cy.get('#password').type('password123')
cy.get('button[type="submit"]').click()
cy.url().should('include', '/dashboard')
})
})
CodeceptJS example:
Feature('Login')
Scenario('should log in successfully', ({ I }) => {
I.amOnPage('/login')
I.fillField('#username', 'user@example.com')
I.fillField('#password', 'password123')
I.click('button[type="submit"]')
I.seeInCurrentUrl('/dashboard')
})
Both frameworks offer intuitive syntax for writing tests, but CodeceptJS provides a more readable, scenario-driven approach. Cypress excels in its built-in waiting mechanisms and browser interactions, while CodeceptJS offers greater flexibility in terms of supported browsers and testing types.
JavaScript API for Chrome and Firefox
Pros of Puppeteer
- Lower-level API providing more fine-grained control over browser automation
- Faster execution due to direct browser control without additional abstraction layers
- Broader use cases beyond testing, including web scraping and performance analysis
Cons of Puppeteer
- Steeper learning curve, especially for those new to browser automation
- Requires more boilerplate code for common testing scenarios
- Less intuitive for writing and maintaining end-to-end tests
Code Comparison
Puppeteer:
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.type('#username', 'user');
await page.click('#submit');
CodeceptJS:
I.amOnPage('https://example.com');
I.fillField('#username', 'user');
I.click('#submit');
CodeceptJS provides a higher-level, more readable syntax for common testing scenarios, while Puppeteer offers more granular control at the cost of verbosity. CodeceptJS abstracts away many of the setup steps required in Puppeteer, making it easier for testers to focus on writing test cases. However, Puppeteer's lower-level API allows for more complex interactions and use cases beyond testing, such as web scraping and performance analysis.
Next-gen browser and mobile automation test framework for Node.js
Pros of WebdriverIO
- More flexible and customizable, allowing for greater control over test scripts
- Supports a wider range of browsers and devices, including mobile testing
- Larger community and ecosystem, with more plugins and integrations available
Cons of WebdriverIO
- Steeper learning curve, especially for those new to JavaScript or automation
- Requires more boilerplate code and setup compared to CodeceptJS
- Can be more verbose, leading to potentially longer and more complex test scripts
Code Comparison
WebdriverIO:
describe('My Login application', () => {
it('should login with valid credentials', async () => {
await browser.url('https://example.com/login');
await $('#username').setValue('myuser');
await $('#password').setValue('mypassword');
await $('button[type="submit"]').click();
await expect($('#logged-in-message')).toBeExisting();
});
});
CodeceptJS:
Feature('Login');
Scenario('login with valid credentials', ({ I }) => {
I.amOnPage('/login');
I.fillField('Username', 'myuser');
I.fillField('Password', 'mypassword');
I.click('Submit');
I.see('Welcome, User!');
});
The code comparison demonstrates WebdriverIO's more explicit, chainable API style, while CodeceptJS offers a more readable, scenario-driven approach with built-in step definitions.
A browser automation framework and ecosystem.
Pros of Selenium
- Widely adopted and supported across multiple programming languages
- Extensive documentation and large community for support
- Direct integration with browser drivers for precise control
Cons of Selenium
- Steeper learning curve, especially for non-programmers
- Requires more boilerplate code for setup and test execution
- Can be slower in test execution compared to CodeceptJS
Code Comparison
Selenium (Java):
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
WebElement element = driver.findElement(By.id("username"));
element.sendKeys("user123");
element.submit();
CodeceptJS:
I.amOnPage('https://example.com');
I.fillField('username', 'user123');
I.click('Submit');
CodeceptJS offers a more readable and concise syntax, making it easier for non-programmers to write and understand tests. Selenium provides more granular control but requires more verbose code. While Selenium is the industry standard with broader language support, CodeceptJS focuses on simplicity and readability, making it an attractive option for teams looking for a more user-friendly testing framework.
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Pros of Playwright
- Cross-browser support for Chromium, Firefox, and WebKit
- Built-in auto-waiting and retry-ability for more stable tests
- Powerful API for advanced scenarios like network interception and mocking
Cons of Playwright
- Steeper learning curve for beginners
- Less extensive plugin ecosystem compared to CodeceptJS
- Requires more boilerplate code for simple scenarios
Code Comparison
Playwright:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'screenshot.png' });
await browser.close();
})();
CodeceptJS:
Feature('Example');
Scenario('take screenshot', ({ I }) => {
I.amOnPage('https://example.com');
I.saveScreenshot('screenshot.png');
});
Summary
Playwright offers robust cross-browser support and advanced features, making it suitable for complex testing scenarios. However, it may require more setup and coding for simple tests. CodeceptJS provides a more user-friendly syntax and extensive plugin support, but may lack some of the advanced capabilities of Playwright. The choice between the two depends on the specific project requirements and team expertise.
Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
Pros of Nightwatch
- Built-in test runner and assertion library, reducing setup complexity
- Supports multiple browsers and mobile testing out of the box
- Extensive API documentation and active community support
Cons of Nightwatch
- Steeper learning curve for beginners due to its comprehensive feature set
- Less flexibility in choosing test runners or assertion libraries
- Syntax can be more verbose compared to CodeceptJS
Code Comparison
CodeceptJS:
Feature('Login');
Scenario('successful login', ({ I }) => {
I.amOnPage('/login');
I.fillField('Username', 'john');
I.fillField('Password', 'secret');
I.click('Submit');
I.see('Welcome, John!');
});
Nightwatch:
module.exports = {
'Login Test': function(browser) {
browser
.url('/login')
.setValue('input[name="username"]', 'john')
.setValue('input[name="password"]', 'secret')
.click('button[type="submit"]')
.assert.containsText('body', 'Welcome, John!')
.end();
}
};
Both CodeceptJS and Nightwatch are powerful end-to-end testing frameworks, but they differ in syntax and approach. CodeceptJS offers a more readable, behavior-driven syntax that's easier for non-technical team members to understand. Nightwatch provides a more traditional, JavaScript-based approach with built-in assertions and browser controls. The choice between them often depends on team preferences and project requirements.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Build Status:
CodeceptJS
Reference: Helpers API
Supercharged E2E Testing
CodeceptJS is a new testing framework for end-to-end testing with WebDriver (or others). It abstracts browser interaction to simple steps that are written from a user's perspective. A simple test that verifies the "Welcome" text is present on a main page of a site will look like:
Feature('CodeceptJS demo');
Scenario('check Welcome page on site', ({ I }) => {
I.amOnPage('/');
I.see('Welcome');
});
CodeceptJS tests are:
- Synchronous. You don't need to care about callbacks or promises or test scenarios which are linear. But, your tests should be linear.
- Written from user's perspective. Every action is a method of
I
. That makes test easy to read, write and maintain even for non-tech persons. - Backend API agnostic. We don't know which WebDriver implementation is running this test.
CodeceptJS uses Helper modules to provide actions to I
object. Currently, CodeceptJS has these helpers:
- Playwright - is a Node library to automate the Chromium, WebKit and Firefox browsers with a single API.
- Puppeteer - uses Google Chrome's Puppeteer for fast headless testing.
- WebDriver - uses webdriverio to run tests via WebDriver or Devtools protocol.
- TestCafe - cheap and fast cross-browser test automation.
- Appium - for mobile testing with Appium
- Detox - This is a wrapper on top of Detox library, aimed to unify testing experience for CodeceptJS framework. Detox provides a grey box testing for mobile applications, playing especially well for React Native apps.
And more to come...
Why CodeceptJS?
CodeceptJS is a successor of Codeception, a popular full-stack testing framework for PHP. With CodeceptJS your scenario-driven functional and acceptance tests will be as simple and clean as they can be. You don't need to worry about asynchronous nature of NodeJS or about various APIs of Playwright, Selenium, Puppeteer, TestCafe, etc. as CodeceptJS unifies them and makes them work as they are synchronous.
Features
- ðª AI-powered with GPT features to assist and heal failing tests.
- â Based on Mocha testing framework.
- ð¼ Designed for scenario driven acceptance testing in BDD-style.
- ð» Uses ES6 natively without transpiler.
- Also plays nice with TypeScript.
- </> Smart locators: use names, labels, matching text, CSS or XPath to locate elements.
- ð Interactive debugging shell: pause test at any point and try different commands in a browser.
- Easily create tests, pageobjects, stepobjects with CLI generators.
Installation
npm i codeceptjs --save
Move to directory where you'd like to have your tests (and CodeceptJS config) stored, and execute:
npx codeceptjs init
to create and configure test environment. It is recommended to select WebDriver from the list of helpers, if you need to write Selenium WebDriver tests.
After that create your first test by executing:
npx codeceptjs generate:test
Now test is created and can be executed with
npx codeceptjs run
If you want to write your tests using TypeScript just generate standard Type Definitions by executing:
npx codeceptjs def .
Later you can even automagically update Type Definitions to include your own custom helpers methods.
Note:
- CodeceptJS requires Node.js version
12+
or later.
Usage
Learn CodeceptJS by examples. Let's assume we have CodeceptJS installed and WebDriver helper enabled.
Basics
Let's see how we can handle basic form testing:
Feature('CodeceptJS Demonstration');
Scenario('test some forms', ({ I }) => {
I.amOnPage('http://simple-form-bootstrap.plataformatec.com.br/documentation');
I.fillField('Email', 'hello@world.com');
I.fillField('Password', secret('123456'));
I.checkOption('Active');
I.checkOption('Male');
I.click('Create User');
I.see('User is valid');
I.dontSeeInCurrentUrl('/documentation');
});
All actions are performed by I
object; assertions functions start with see
function.
In these examples all methods of I
are taken from WebDriver helper, see reference to learn how to use them.
Let's execute this test with run
command. Additional option --steps
will show us the running process. We recommend use --steps
or --debug
during development.
npx codeceptjs run --steps
This will produce an output:
CodeceptJS Demonstration --
test some forms
⢠I am on page "http://simple-form-bootstrap.plataformatec.com.br/documentation"
⢠I fill field "Email", "hello@world.com"
⢠I fill field "Password", "****"
⢠I check option "Active"
⢠I check option "Male"
⢠I click "Create User"
⢠I see "User is valid"
⢠I dont see in current url "/documentation"
â OK in 17752ms
CodeceptJS has an ultimate feature to help you develop and debug your test.
You can pause execution of test in any place and use interactive shell to try different actions and locators.
Just add pause()
call at any place in a test and run it.
Interactive shell can be started outside test context by running:
npx codeceptjs shell
Actions
We filled form with fillField
methods, which located form elements by their label.
The same way you can locate element by name, CSS
or XPath
locators in tests:
// by name
I.fillField('user_basic[email]', 'hello@world.com');
// by CSS
I.fillField('#user_basic_email', 'hello@world.com');
// don't make us guess locator type, specify it
I.fillField({css: '#user_basic_email'}, 'hello@world.com');
Other methods like checkOption
, and click
work in a similar manner. They can take labels or CSS or XPath locators to find elements to interact.
Assertions
Assertions start with see
or dontSee
prefix. In our case we are asserting that string 'User is valid' is somewhere in a webpage.
However, we can narrow the search to particular element by providing a second parameter:
I.see('User is valid');
// better to specify context:
I.see('User is valid', '.alert-success');
In this case 'User is valid' string will be searched only inside elements located by CSS .alert-success
.
Grabbers
In case you need to return a value from a webpage and use it directly in test, you should use methods with grab
prefix.
They are expected to be used inside async/await
functions, and their results will be available in test:
Feature('CodeceptJS Demonstration');
Scenario('test page title', async ({ I }) => {
I.amOnPage('http://simple-form-bootstrap.plataformatec.com.br/documentation');
const title = await I.grabTitle();
I.expectEqual(title, 'Example application with SimpleForm and Twitter Bootstrap'); // Avaiable with Expect helper. -> https://codecept.io/helpers/Expect/
});
The same way you can grab text, attributes, or form values and use them in next test steps.
Before/After
Common preparation steps like opening a web page, logging in a user, can be placed in Before
or Background
:
const { I } = inject();
Feature('CodeceptJS Demonstration');
Before(() => { // or Background
I.amOnPage('http://simple-form-bootstrap.plataformatec.com.br/documentation');
});
Scenario('test some forms', () => {
I.click('Create User');
I.see('User is valid');
I.dontSeeInCurrentUrl('/documentation');
});
Scenario('test title', () => {
I.seeInTitle('Example application');
});
PageObjects
CodeceptJS provides the most simple way to create and use page objects in your test. You can create one by running
npx codeceptjs generate pageobject
It will create a page object file for you and add it to the config.
Let's assume we created one named docsPage
:
const { I } = inject();
module.exports = {
fields: {
email: '#user_basic_email',
password: '#user_basic_password'
},
submitButton: {css: '#new_user_basic input[type=submit]'},
sendForm(email, password) {
I.fillField(this.fields.email, email);
I.fillField(this.fields.password, password);
I.click(this.submitButton);
}
}
You can easily inject it to test by providing its name in test arguments:
Feature('CodeceptJS Demonstration');
Before(({ I }) => { // or Background
I.amOnPage('http://simple-form-bootstrap.plataformatec.com.br/documentation');
});
Scenario('test some forms', ({ I, docsPage }) => {
docsPage.sendForm('hello@world.com','123456');
I.see('User is valid');
I.dontSeeInCurrentUrl('/documentation');
});
When using Typescript, replace module.exports
with export
for autocompletion.
Contributing
Contributors
Thanks all to those who are and will have contributing to this awesome project!
License
MIT © CodeceptJS Team
Top Related Projects
Fast, easy and reliable testing for anything that runs in a browser.
JavaScript API for Chrome and Firefox
Next-gen browser and mobile automation test framework for Node.js
A browser automation framework and ecosystem.
Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot