| 1 | 'use strict'; |
| 2 | |
| 3 | module.exports = { |
| 4 | /** |
| 5 | * Generate a random number string with some additional extended ASCII. |
| 6 | * |
| 7 | * @param {string} prefix A prefix to apply to the generated output. |
| 8 | * @return {string} |
| 9 | */ |
| 10 | getTestString( prefix = '' ) { |
| 11 | return prefix + Math.random().toString() + '-Iñtërnâtiônàlizætiøn'; |
| 12 | }, |
| 13 | |
| 14 | /** |
| 15 | * Check if a page is (or, if it doesn't yet exist, would be by default) a wikitext content |
| 16 | * object, as opposed to e.g. a JSON blob or a content model provided by an extension. This |
| 17 | * is useful for when a target of a test requires wikitext behaviour, such as testing for |
| 18 | * having a talk page, being subject to redirects, being editable, or similar concerns. |
| 19 | * |
| 20 | * @param {string} target The name of the page in question. |
| 21 | * @return {Promise<boolean>} True if the target is not wikitext. |
| 22 | */ |
| 23 | async isTargetNotWikitext( target ) { |
| 24 | // First, make sure that the 'mw' object should exist |
| 25 | await this.waitForModuleState( 'mediawiki.base' ); |
| 26 | |
| 27 | // Then, ask the API for the basic 'info' data about the given page |
| 28 | return browser.executeAsync( ( target_, done ) => { |
| 29 | mw.loader.using( 'mediawiki.api' ).then( () => { |
| 30 | const api = new mw.Api(); |
| 31 | api.get( { |
| 32 | action: 'query', prop: 'info', titles: target_, |
| 33 | format: 'json', formatversion: 2 |
| 34 | } ).then( ( result ) => { |
| 35 | // Finally, return whether said page is wikitext (or would be, if it doesn't yet exist) |
| 36 | done( result.query.pages[ 0 ].contentmodel !== 'wikitext' ); |
| 37 | } ); |
| 38 | } ); |
| 39 | }, target ); |
| 40 | }, |
| 41 | |
| 42 | /** |
| 43 | * Wait for a given module to reach a specific state |
| 44 | * |
| 45 | * @param {string} moduleName The name of the module to wait for |
| 46 | * @param {string} moduleStatus 'registered', 'loaded', 'loading', 'ready', 'error', 'missing' |
| 47 | * @param {number} timeout The wait time in milliseconds before the wait fails |
| 48 | */ |
| 49 | async waitForModuleState( moduleName, moduleStatus = 'ready', timeout = 5000 ) { |
| 50 | await browser.waitUntil( |
| 51 | () => browser.execute( |
| 52 | ( arg ) => typeof mw !== 'undefined' && mw.loader.getState( arg.name ) === arg.status, |
| 53 | { status: moduleStatus, name: moduleName } |
| 54 | ), { |
| 55 | timeout: timeout, |
| 56 | timeoutMsg: 'Failed to wait for ' + moduleName + ' to be ' + moduleStatus + ' after ' + timeout + ' ms.' |
| 57 | } |
| 58 | ); |
| 59 | } |
| 60 | }; |