| 1 | // Example code for Selenium/How-to/Use MediaWiki API |
| 2 | // https://www.mediawiki.org/wiki/Selenium/How-to/Use_MediaWiki_API |
| 3 | |
| 4 | 'use strict'; |
| 5 | |
| 6 | const assert = require( 'assert' ); |
| 7 | const MWBot = require( 'mwbot' ); |
| 8 | |
| 9 | // apiUrl is required for our continuous integration. |
| 10 | // If you don't have MW_SERVER and MW_SCRIPT_PATH environment variables set |
| 11 | // you can probably hardcode it to something like this: |
| 12 | // const apiUrl = 'http://localhost:8080/w/api.php'; |
| 13 | const apiUrl = `${ process.env.MW_SERVER }${ process.env.MW_SCRIPT_PATH }/api.php`; |
| 14 | |
| 15 | const bot = new MWBot( { |
| 16 | apiUrl: apiUrl |
| 17 | } ); |
| 18 | |
| 19 | // Since mwbot v2 the script either needs to log in immediately or hardcode MediaWiki version. |
| 20 | // Without the line below, this error message is displayed: |
| 21 | // Invalid version. Must be a string. Got type "object". |
| 22 | bot.mwversion = '0.0.0'; |
| 23 | |
| 24 | describe( 'API', () => { |
| 25 | |
| 26 | it( 'Main Page should exist', async () => { |
| 27 | const response = await bot.read( 'Main Page' ); |
| 28 | |
| 29 | // console.log( response ); |
| 30 | // { batchcomplete: '' (...) query: { pages: { '1': [Object] } } } |
| 31 | |
| 32 | // console.log( response.query ); |
| 33 | // { pages: { '1': { pageid: 1, ns: 0, title: 'Main Page', revisions: [Array] } } } |
| 34 | |
| 35 | assert.strictEqual( response.query.pages[ '1' ].pageid, 1 ); |
| 36 | |
| 37 | } ); |
| 38 | |
| 39 | it( 'Missing Page should not exist', async () => { |
| 40 | const response = await bot.read( 'Missing Page' ); |
| 41 | |
| 42 | // console.log( response ); |
| 43 | // { batchcomplete: '', query: { pages: { '-1': [Object] } } } |
| 44 | |
| 45 | // console.log( response.query ); |
| 46 | // { pages: { '-1': { ns: 0, title: 'Missing Page', missing: '' } } } |
| 47 | |
| 48 | assert.strictEqual( response.query.pages[ '-1' ].missing, '' ); |
| 49 | |
| 50 | } ); |
| 51 | |
| 52 | } ); |