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.
Stack[edit]
The stack:
Language | JavaScript/Node.js |
Assertion library | Assert (ships with Node.js) |
Testing framework | Mocha |
Mediawiki API | mwbot |
Advantages[edit]
- Assertions.
- Testing framework (setup, teardown, reporting...).
Disadvantages[edit]
- Several new tools to learn.
Code[edit]
'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, '' );
} );
} );
} );
Output[edit]
npx mocha tests/api/api.js
API
â Main Page should exist (313ms)
â Missing Page should not exist (93ms)
2 passing (413ms)