Selenium/Node.js/MediaWiki API

From MediaWiki.org
Jump to navigation Jump to search

This tutorial will assume that you are running tests from your machine, targeting beta cluster. Code from this page is available at mediawiki-selenium-nodejs repository. mediawiki/core repository has more code examples in tests/selenium folder.

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

Examples 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' );
let bot = new MWBot( {
    apiUrl: 'https://en.wikipedia.beta.wmflabs.org/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.equal( 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.equal( response.query.pages[ '-1' ].missing, '' );

        } );

    } );

} );

Output[edit]

$ ./node_modules/.bin/mocha test/api.js 


  API
    ✓ Main Page should exist (790ms)
    ✓ Missing Page should not exist (458ms)


  2 passing (1s)