Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

📜 Let your CodeceptJS tests talk to databases

License

NotificationsYou must be signed in to change notification settings

thiagodp/codeceptjs-dbhelper

Repository files navigation

npm versionDownloads

Let your CodeceptJS tests talk to databases

This is aHelper forCodeceptJS that allows you to execute database queries and commands usingdatabase-js.

👉 It works withCodeceptJS 1, 2, and 3.

Install

You have to install the library and the desired database drivers

Step 1 of 2:Install the helper

npm i -D codeceptjs-dbhelper

Step 2 of 2:Install a database driver

Driver (wrapper)NoteInstallation command
ActiveX Data ObjectsWindows onlynpm i -D database-js-adodb
CSV filesnpm i -D database-js-csv
Excel filesnpm i -D database-js-xlsx
Firebasenpm i -D database-js-firebase
INI filesnpm i -D database-js-ini
JSON filesnpm i -D database-js-json
MySQLnpm i -D database-js-mysql
MS SQL Servernpm i -D database-js-mssql
PostgreSQLnpm i -D database-js-postgres
SQLitenpm i -D database-js-sqlite

Seedatabase-js for the full list of available drivers.

Configure

In your CodeceptJS configuration file (e.g.,codecept.conf.js,codecept.json), includeDbHelper in the propertyhelpers :

  ..."helpers":{    ..."DbHelper":{"require":"./node_modules/codeceptjs-dbhelper"}},  ...

Usage

Syntax differences between CodeceptJS 2 and CodeceptJS 3

In CodeceptJS 2, every callback receivesI as an argument:

Scenario('test something',async(I)=>{// CodeceptJS 2 notation/* ... */});

In CodeceptJS 3, every callback receives anobject that contains the propertyI - that is,{ I }:

Scenario('test something',async({ I})=>{// CodeceptJS 3 notation/* ... */});

See theCodeceptJS docs for more information on how to upgrade your codebase.

Usage

The following examples are written withCodeceptJS 3.

Example 1

BeforeSuite(async({ I})=>{// Connects to a database// The first parameter is the key that will hold a reference to the databaseI.connect("testdb","mysql://root:mypassword@localhost:3306/testdb");});AfterSuite(async({ I})=>{// Disconnects and removes the reference to the databaseawaitI.removeConnection("testdb");});Before(async({ I})=>{// Deletes all the records from the table 'user'awaitI.run("testdb","DELETE FROM user");// Inserting some usersawaitI.run("testdb","INSERT INTO user ( username, password ) VALUES ( ?, ? )","admin","123456");awaitI.run("testdb","INSERT INTO user ( username, password ) VALUES ( ?, ? )","bob","654321");awaitI.run("testdb","INSERT INTO user ( username, password ) VALUES ( ?, ? )","alice","4lic3p4s$");});// ... your feature ...// ... your scenarios ...

Example 2

Feature('Foo');Scenario('Bar',async({ I})=>{// Queries a user from the databaseconstresults=awaitI.query("testdb","SELECT username, password FROM user WHERE username = ?","bob");constuser=results[0];// object in the first rowI.amOnPage('/login');I.fillField('#username',user.username);// bobI.fillField('#password',user.password);// 654321I.click('#ok');I.see('Welcome');});

API

/**     * Connects to the database described by the given connection string.     *     *@param {string|number}    key         Identification for using in other commands.     *@param {string|object}    conn        JDBC-like connection string or a connection object accepted by `database-js`.     *@param {object|undefined} [driver]    [OPTIONAL] Driver object, used by `database-js`.     *     *@returns {Connection} DatabaseJS' connection     */connect(key:string|number,conn:string|object,driver?:object|undefined): any;/**     * Disconnects and removes the database connection identified by the given key.     *     *@param {string|number} key Database identification key set in connect().     *     *@returns {Promise<boolean>} If it was successful.     */disconnect(key:string|number):Promise<boolean>;/**     * Disconnects and removes the database connection identified by the given key.     *     *@param {string|number} key Database identification key set in connect().     *     *@returns {Promise<boolean>} If it was successful.     */removeConnection(key:string|number):Promise<boolean>;/**     * Performs a query.     *     *@param {string|number}      key       Database identification key set in connect().     *@param {string}             command   Query to run.     *@param {...any[]|undefined} [params]  [OPTIONAL] Query parameters.     *     *@returns {Promise<any[]>} Query results.     */query(key:string|number,command: string, ...params?:(any[]|undefined)[]):Promise<any[]>;/**     * Executes a command.     *     *@param {string|number} key       Database identification key set in connect().     *@param {string}        command   Command to run.     *@param {any[]}         [params]  [OPTIONAL] Command parameters.     *     *@returns {Promise<any[]>} Command results.     */run(key:string|number,command: string, ...params?: any[]):Promise<any[]>;/**     * Creates a database connection.     *     *@param {string|object}       conn     JDBC-like connection string or a connection object accepted by `database-js`.     *@param {object|undefined}    [driver] [OPTIONAL] Driver object, used by `database-js`.     *     *@returns {Connection} DatabaseJS' connection     */createConnection(conn:string|object,driver?:object|undefined): any;/**     * Checks if there is a database connection with the given key.     *     *@param {string|number} key Database identification key set in connect().     *     *@returns {boolean}     */hasConnection(key:string|number): boolean;/**     * Gets the database connection with the given key.     *     *@param {string|number} key Database identification key set in connect().     *     *@returns {Connection} DatabaseJS' connection.     */getConnection(key:string|number): any;

See also

codeceptjs-cmdhelper - Execute commands in the console/terminal

License

MIT ©Thiago Delgado Pinto


[8]ページ先頭

©2009-2025 Movatter.jp