Code from this page is available at mediawiki/core and gerrit:675813. |
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. For more examples see Selenium/Reference/Example Code.
In this example, we will check if a page exists using MediaWiki action API.
Stack
editThe 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
edittests/selenium/docs/Use_MediaWiki_API/specs/api.js
'use strict';
const assert = require( 'assert' );
const MWBot = require( 'mwbot' );
// 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 bot = new MWBot( {
apiUrl: apiUrl
} );
// Since mwbot v2 the script either needs to log in immediately or hardcode MediaWiki version.
// Without the line below, this error message is displayed:
// Invalid version. Must be a string. Got type "object".
bot.mwversion = '0.0.0';
describe( 'API', () => {
it( 'Main Page should exist', async () => {
const response = await bot.read( 'Main Page' );
// console.log( response );
// { batchcomplete: '' (...) query: { pages: { '1': [Object] } } }
// console.log( response.query );
// { pages: { '1': { pageid: 1, ns: 0, title: 'Main Page', revisions: [Array] } } }
assert.strictEqual( response.query.pages[ '1' ].pageid, 1 );
} );
it( 'Missing Page should not exist', async () => {
const response = await bot.read( 'Missing Page' );
// 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
editnpm run selenium-test -- --spec tests/selenium/docs/Use_MediaWiki_API/specs/api.js
> selenium-test
> wdio ./tests/selenium/wdio.conf.js --spec tests/selenium/docs/Use_MediaWiki_API/specs/api.js
Execution of 1 workers started at 2024-08-14T13:45:37.377Z
[0-0] RUNNING in chrome - /tests/selenium/docs/Use_MediaWiki_API/specs/api.js
[0-0] PASSED in chrome - /tests/selenium/docs/Use_MediaWiki_API/specs/api.js
"spec" Reporter:
------------------------------------------------------------------
[Chrome Headless 120.0.6099.224 linux #0-0] Running: Chrome Headless (v120.0.6099.224) on linux
[Chrome Headless 120.0.6099.224 linux #0-0] Session ID: a04bdbed-614f-4ea1-a600-611e55efbf6d
[Chrome Headless 120.0.6099.224 linux #0-0]
[Chrome Headless 120.0.6099.224 linux #0-0] » /tests/selenium/docs/Use_MediaWiki_API/specs/api.js
[Chrome Headless 120.0.6099.224 linux #0-0] API
[Chrome Headless 120.0.6099.224 linux #0-0] ✓ Main Page should exist
[Chrome Headless 120.0.6099.224 linux #0-0] ✓ Missing Page should not exist
[Chrome Headless 120.0.6099.224 linux #0-0]
[Chrome Headless 120.0.6099.224 linux #0-0] 2 passing (366ms)
Spec Files: 1 passed, 1 total (100% completed) in 00:00:03