selenium: Initial version of wdio-mediawiki package
[lhc/web/wiklou.git] / tests / selenium / wdio-mediawiki / Api.js
1 const MWBot = require( 'mwbot' );
2
3 // TODO: Once we require Node 7 or later, we can use async-await.
4
5 module.exports = {
6 /**
7 * Shortcut for `MWBot#edit( .. )`.
8 *
9 * @since 1.0.0
10 * @see <https://www.mediawiki.org/wiki/API:Edit>
11 * @param {string} title
12 * @param {string} content
13 * @return {Object} Promise for API action=edit response data.
14 */
15 edit( title, content ) {
16 let bot = new MWBot();
17
18 return bot.loginGetEditToken( {
19 apiUrl: `${browser.options.baseUrl}/api.php`,
20 username: browser.options.username,
21 password: browser.options.password
22 } ).then( function () {
23 return bot.edit( title, content, `Created page with "${content}"` );
24 } );
25 },
26
27 /**
28 * Shortcut for `MWBot#delete( .. )`.
29 *
30 * @since 1.0.0
31 * @see <https://www.mediawiki.org/wiki/API:Delete>
32 * @param {string} title
33 * @param {string} reason
34 * @return {Object} Promise for API action=delete response data.
35 */
36 delete( title, reason ) {
37 let bot = new MWBot();
38
39 return bot.loginGetEditToken( {
40 apiUrl: `${browser.options.baseUrl}/api.php`,
41 username: browser.options.username,
42 password: browser.options.password
43 } ).then( function () {
44 return bot.delete( title, reason );
45 } );
46 },
47
48 /**
49 * Shortcut for `MWBot#request( { acount: 'createaccount', .. } )`.
50 *
51 * @since 1.0.0
52 * @see <https://www.mediawiki.org/wiki/API:Account_creation>
53 * @param {string} username
54 * @param {string} password
55 * @return {Object} Promise for API action=createaccount response data.
56 */
57 createAccount( username, password ) {
58 let bot = new MWBot();
59
60 // Log in as admin
61 return bot.loginGetCreateaccountToken( {
62 apiUrl: `${browser.options.baseUrl}/api.php`,
63 username: browser.options.username,
64 password: browser.options.password
65 } ).then( function () {
66 // Create the new account
67 return bot.request( {
68 action: 'createaccount',
69 createreturnurl: browser.options.baseUrl,
70 createtoken: bot.createaccountToken,
71 username: username,
72 password: password,
73 retype: password
74 } );
75 } );
76 }
77 };