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-Docker. Code from this page is available at gerrit:675813. For more examples see Selenium/Reference/Example Code.

In this example, we will check if a page exists using MediaWiki action API.

StackEdit

The stack:

Language JavaScript/Node.js
Assertion library Assert (ships with Node.js)
Testing framework Mocha
Mediawiki API mwbot

AdvantagesEdit

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

DisadvantagesEdit

  • Several new tools to learn.

CodeEdit

'use strict';

const assert = require( 'assert' ),
	MWBot = require( 'mwbot' );
const bot = new MWBot( {
	apiUrl: 'http://localhost:8080/w/api.php'
} );

describe( 'API', function () {

	it( 'Main Page should exist', function () {

		return bot.read( 'Main Page', { timeout: 8000 } ).then( ( response ) => {

			// console.log( response );
			// { batchcomplete: '', query: { pages: { '1': [Object] } } }

			// console.log( response.query );
			// { '1': { pageid: 1, ns: 0, title: 'Main Page', revisions: [Array] } } }

			assert.strictEqual( response.query.pages[ '1' ].pageid, 1 );

		} );

	} );

	it( 'Missing Page should not exist', function () {

		return bot.read( 'Missing Page', { timeout: 8000 } ).then( ( response ) => {

			// console.log( response );
			// { batchcomplete: '', query: { pages: { '-1': [Object] } } }

			// console.log( response.query );
			// { pages: { '-1': { ns: 0, title: 'Missing Page', missing: '' } } }

			assert.strictEqual( response.query.pages[ '-1' ].missing, '' );

		} );

	} );

} );

OutputEdit

npx mocha tests/api/api.js

  API
    ✓ Main Page should exist (313ms)
    ✓ Missing Page should not exist (93ms)

  2 passing (413ms)