Selenium/How-to/Use MediaWiki API

You can use the MediaWiki action API for testing, reusing parts of Selenium/Explanation/Stack, but without Selenium.

This tutorial will assume that you are running tests from your machine, targeting mediawiki-quickstart. For more examples see Selenium/Reference/Example Code.

In this example, we will check if a page exists using MediaWiki action API. This needs wdio-mediawiki 6.0.0 or later.

Stack

The stack:

LanguageJavaScript/Node.js
Assertion libraryAssert (ships with Node.js)
Testing frameworkMocha
Mediawiki APIApi.js in wdio-mediawiki

Advantages

  • Assertions.
  • Testing framework (setup, teardown, reporting...).

Disadvantages

  • Several new tools to learn.

Code

tests/selenium/docs/Use_MediaWiki_API/specs/api.js

// Example code for Selenium/How-to/Use MediaWiki API
// https://www.mediawiki.org/wiki/Selenium/How-to/Use_MediaWiki_API


import assert from 'assert';
import { createApiClient } from 'wdio-mediawiki/Api.js';

// apiUrl is required for our continuous integration.
// If you don't have MW_SERVER and MW_SCRIPT_PATH environment variables set
// you can probably hardcode it to something like this:
// const apiUrl = 'http://localhost:8080/w/api.php';
const apiUrl = `${ process.env.MW_SERVER }${ process.env.MW_SCRIPT_PATH }/api.php`;
const apiClient = await createApiClient( {
 apiUrl: apiUrl
} );


describe( 'API', () => {

 it( 'Main Page should exist', async () => {
   const response = await apiClient.read( 'Main Page' );
   assert.strictEqual( Object.values( response.query.pages )[ 0 ].pageid > 0, true );
 } );

 it( 'Missing Page should not exist', async () => {
   const response = await apiClient.read( 'Missing Page' );
   // { pages: { '-1': { ns: 0, title: 'Missing Page', missing: '' } } }
   assert.strictEqual( response.query.pages[ '-1' ].missing, '' );
 } );
} );

Output

npm run selenium-test -- --spec tests/selenium/docs/Use_MediaWiki_API/specs/api.js 
> selenium-test
> if [ "$CI" = true ]; then node tests/selenium/docs/Stack/webdriverio.js; fi && wdio ./tests/selenium/wdio.conf.js --spec tests/selenium/docs/Use_MediaWiki_API/specs/api.js
Execution of 1 workers started at 2025-10-10T11:19:05.758Z
Run test targeting http://localhost:8080/w
[0-0] RUNNING in chrome - file:///tests/selenium/docs/Use_MediaWiki_API/specs/api.js
[0-0] PASSED in chrome - file:///tests/selenium/docs/Use_MediaWiki_API/specs/api.js
 "spec" Reporter:
------------------------------------------------------------------
[chrome 141.0.7390.77 mac #0-0] Running: chrome (v141.0.7390.77) on mac
[chrome 141.0.7390.77 mac #0-0] Session ID: 30c59ce74cebfad96b510f39e39aa20d
[chrome 141.0.7390.77 mac #0-0]
[chrome 141.0.7390.77 mac #0-0] » tests/selenium/docs/Use_MediaWiki_API/specs/api.js
[chrome 141.0.7390.77 mac #0-0] API
[chrome 141.0.7390.77 mac #0-0]    ✓ Main Page should exist
[chrome 141.0.7390.77 mac #0-0]    ✓ Missing Page should not exist
[chrome 141.0.7390.77 mac #0-0]
[chrome 141.0.7390.77 mac #0-0] 2 passing (216ms)
Spec Files:	 1 passed, 1 total (100% completed) in 00:00:06
Category:Selenium/Node.js
Category:Selenium/Node.js