
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
codeceptjs
Advanced tools
CodeceptJS is an end-to-end testing framework for Node.js that simplifies the process of writing and running tests for web applications. It supports various testing engines like WebDriver, Puppeteer, TestCafe, and more, allowing for flexible and powerful test automation.
End-to-End Testing
CodeceptJS allows you to write end-to-end tests in a simple and readable format. This example demonstrates a basic login test where the user navigates to the login page, fills in the username and password, clicks the login button, and verifies that the welcome message is displayed.
const { I } = inject();
Feature('Login');
Scenario('test login', (I) => {
I.amOnPage('/login');
I.fillField('Username', 'user');
I.fillField('Password', 'password');
I.click('Login');
I.see('Welcome, user');
});
Page Object Model
CodeceptJS supports the Page Object Model (POM) design pattern, which helps in organizing and maintaining test code. This example shows a LoginPage class that encapsulates the elements and actions related to the login page, making the test code more modular and reusable.
class LoginPage {
constructor() {
this.fields = {
username: 'input[name="username"]',
password: 'input[name="password"]'
};
this.buttons = {
login: 'button[type="submit"]'
};
}
login(username, password) {
I.fillField(this.fields.username, username);
I.fillField(this.fields.password, password);
I.click(this.buttons.login);
}
}
module.exports = new LoginPage();
Data-Driven Testing
CodeceptJS supports data-driven testing, allowing you to run the same test with different sets of data. This example demonstrates how to create a data table with multiple user credentials and run the login test for each set of credentials.
const { I } = inject();
Feature('Login');
const users = new DataTable(['username', 'password']);
users.add(['user1', 'password1']);
users.add(['user2', 'password2']);
Data(users).Scenario('test login with multiple users', (I, current) => {
I.amOnPage('/login');
I.fillField('Username', current.username);
I.fillField('Password', current.password);
I.click('Login');
I.see('Welcome, ' + current.username);
});
Cypress is a popular end-to-end testing framework that provides a fast, reliable, and easy-to-use testing experience. It offers features like time travel, real-time reloads, and automatic waiting. Compared to CodeceptJS, Cypress has a more modern and user-friendly interface but is limited to running tests in a browser environment.
Nightwatch.js is an end-to-end testing framework that uses the W3C WebDriver API to perform browser automation. It is known for its simplicity and ease of setup. Compared to CodeceptJS, Nightwatch.js is more focused on WebDriver-based testing and may require more configuration for advanced use cases.
WebdriverIO is a versatile testing framework that supports both WebDriver and DevTools protocols. It offers a wide range of plugins and integrations, making it highly customizable. Compared to CodeceptJS, WebdriverIO provides more flexibility and control over the testing process but may have a steeper learning curve.
Reference: Helpers API | Demo
CodeceptJS is a new testing framework for end-to-end testing with WebDriver (or others). It abstracts browser interaction to simple steps which is written from a user perspective. A simple test that verifies that "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');
});
Codeception tests are:
I
. That makes test easy to read, write and maintain even for non-tech persons.Codeception uses Helper modules to provide actions to I
object. Currently CodeceptJS has these helpers:
And more to come...
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 Selenium, PhantomJS, Protractor, etc, as CodeceptJS unifies them and makes them work as they were synchronous.
npm install -g codeceptjs
Move to directory where you'd like to have your tests (and codeceptjs config) stored, and run
codeceptjs init
to create and configure test environment. It is recommended to select WebDriverIO from the list of helpers, if you need to write Selenium WebDriver tests.
After that create your first test by executing:
codeceptjs generate:test
Now test is created and can be executed with
codeceptjs run
If you want to write your tests using TypeScript just generate standard Type Definitions by executing:
codeceptjs def .
Later you can even automagically update Type Definitions to include your own custom helpers methods.
Note that CodeceptJS requires Node.js version 6.11 or later.
Learn CodeceptJS by examples. Let's assume we have CodeceptJS installed and WebDriverIO helper enabled.
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', '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 this examples all methods of I
are taken from WebDriverIO 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.
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", "123456"
• 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 you 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:
codeceptjs shell
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 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
.
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 a generator functions, and their results will be available in test:
var assert = require('assert');
Feature('CodeceptJS Demonstration');
Scenario('test page title', function*(I) {
I.amOnPage('http://simple-form-bootstrap.plataformatec.com.br/documentation');
var title = yield I.grabTitle();
assert.equal(title, 'Example application with SimpleForm and Twitter Bootstrap');
});
The same way you can grab text, attributes, or form values and use them in next test steps.
Common preparation steps like opening a web page, logging in a user, can be placed in Before
or Background
:
Feature('CodeceptJS Demonstration');
Before((I) => { // or Background
I.amOnPage('http://simple-form-bootstrap.plataformatec.com.br/documentation');
});
Scenario('test some forms', (I) => {
I.click('Create User');
I.see('User is valid');
I.dontSeeInCurrentUrl('/documentation');
});
Scenario('test title', (I) => {
I.seeInTitle('Example application');
});
CodeceptJS provides the most simple way to create and use page objects in your test. You can create one by running
codeceptjs generate pageobject
It will create a page object file for you and add it to config.
Let's assume we created one named docsPage
:
'use strict';
let I;
module.exports = {
_init() {
I = actor();
},
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');
});
MIT © DavertMik
1.0.1
addMochawesomeContext
method to add custom data to mochawesome reportsrefresh
function by @awhanksUnhandled promise rejection (rejection id: 1): Error: Unknown wait type: pageLoad
[Wrapped Error] "codeceptjs is not defined"
; Reinjectiing client scripts to a webpage on changes.Wait*
methodsretries
optionCodeceptJS hits first stable release. CodeceptJS provides a unified API for web testing for Webdriverio, Protractor, and NightmareJS. Since 1.0 you can also test mobile applications in the similar manner with Appium.
Sample test:
I.seeAppIsInstalled("io.super.app");
I.click('~startUserRegistrationCD');
I.fillField('~email of the customer', 'Nothing special'));
I.see('davert@codecept.io', '~email of the customer'));
I.clearField('~email of the customer'));
I.dontSee('Nothing special', '~email of the customer'));
We also introduced two new helpers for data management. Using them you can easily prepare and cleanup data for your tests using public REST API.
Sample test
// create a user using data factories and REST API
I.have('user', { name: 'davert', password: '123456' })
// use it to login
I.amOnPage('/login')
I.fillField('login', 'davert')
I.fillField('password', '123456')
I.click('Login')
I.see('Hello, davert')
// user will be removed after the test
Next notable feature is SmartWait for WebDriverIO, Protractor, SeleniumWebdriver. When smartwait
option is set, script will wait for extra milliseconds to locate an element before failing. This feature uses implicit waits of Selenium but turns them on only in applicable pieces. For instance, implicit waits are enabled for seeElement
but disabled for dontSeeElement
within
command with generators.--debug
mode. #591 by @APshenkindisableScreenshots
option to disable screenshots on fail by @ApshenkinuniqueScreenshotNames
option to generate unique names for screenshots on failure by @Apshenkinclick('text', '#el')
will throw exception if text is not found inside #el
.saveScreenshot
for PhantomJS, fullPageScreenshots
option introduced by @HughZurname #549seeNumberOfVisibleElements
by @BorisOsipov #574_beforeSuite
hooks from helpers -> BeforeSuite
from test -> _before
hooks from helpers -> Before
from test - > Test steps -> _failed
hooks from helpers (if test failed) -> After
from test -> _after
hooks from helpers -> AfterSuite
from test -> _afterSuite
hook from helpers.restart
option is false - close all tabs expect one in _after
._after
, _afterSuite
hooks even After/AfterSuite from test was failedrestart
option is false. We should start browser only one time and close it only after all tests.keepCookies
flag is enabled_locate
Special thanks to Andrey Pshenkin for his work on this release and the major improvements.
FAQs
Supercharged End 2 End Testing Framework for NodeJS
The npm package codeceptjs receives a total of 175,246 weekly downloads. As such, codeceptjs popularity was classified as popular.
We found that codeceptjs demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.