selenium: Minor clean-up in preparation for packaging

This is functionally a no-op, purely refactoring (mostly style).

* Consistently require packages at the top of a file.
  (e.g. MWBot in edit.page.js).

* Remove unused .call(this) from mwbot interaction closures,
  which didn't use 'this'.

* Use Node.js regular Promise chaining with then(), instead of
  complex bluebird.coroutine generator function yields, which
  are intended to emulate async-await, but the syntax is quite
  error-prone for inexperienced developers and hard to debug.
  Once we require Node 7+ for the selenium tests, we can use
  async-await here natively, but until then, might as well use
  regular then() syntax, which we already use elsewhere in the
  tests, and is also what MWBot documentation uses.

* Also applied some minor whitespace changes for consistency
  among these files and other MediaWiki JS. E.g. no empty line
  before the first statement of a function. Add a new line between
  different methods, and between the end of a class and the
  export statement.

* Remove 'use strict' from test files. The patterns that would expose
  the bad non-strict behaviour are mostly already forbidden by ESLint,
  and the run-time optimisation to disable non-strict can't be noticed
  in tests (more useful in prod where e.g. the same process would run
  a function 1 million times). Main reason here is to keep things
  simple for new-comers and reduce boilerplate, given that these tests
  will mainly be worked on by browser-JS developers, not Node.js devs,
  and we don't currently use strict mode in our front-end code, either.

* Remove unused bluebird dependency.

Bug: T193088
Change-Id: I59f9211299e8e884c28c7733bcee3b7b28542610
This commit is contained in:
Timo Tijhof 2018-05-02 04:48:36 +01:00 committed by Krinkle
parent 911e74ea89
commit 2729bf4653
12 changed files with 42 additions and 85 deletions

View file

@ -9,7 +9,6 @@
"selenium-test": "wdio ./tests/selenium/wdio.conf.js"
},
"devDependencies": {
"bluebird": "3.5.1",
"deepmerge": "1.3.2",
"eslint": "4.9.0",
"eslint-config-wikimedia": "0.5.0",

View file

@ -1,5 +1,6 @@
'use strict';
const Page = require( './page' );
const Page = require( './page' ),
// https://github.com/Fannon/mwbot
MWBot = require( 'mwbot' );
class CreateAccountPage extends Page {
@ -22,18 +23,14 @@ class CreateAccountPage extends Page {
}
apiCreateAccount( username, password ) {
const MWBot = require( 'mwbot' ), // https://github.com/Fannon/mwbot
Promise = require( 'bluebird' );
let bot = new MWBot();
return Promise.coroutine( function* () {
yield bot.loginGetCreateaccountToken( {
apiUrl: `${browser.options.baseUrl}/api.php`,
username: browser.options.username,
password: browser.options.password
} );
yield bot.request( {
return bot.loginGetCreateaccountToken( {
apiUrl: `${browser.options.baseUrl}/api.php`,
username: browser.options.username,
password: browser.options.password
} ).then( function () {
return bot.request( {
action: 'createaccount',
createreturnurl: browser.options.baseUrl,
createtoken: bot.createaccountToken,
@ -41,9 +38,8 @@ class CreateAccountPage extends Page {
password: password,
retype: password
} );
} ).call( this );
} );
}
}
module.exports = new CreateAccountPage();

View file

@ -1,8 +1,8 @@
'use strict';
const Page = require( './page' );
const Page = require( './page' ),
// https://github.com/Fannon/mwbot
MWBot = require( 'mwbot' );
class DeletePage extends Page {
get reason() { return browser.element( '#wpReason' ); }
get watch() { return browser.element( '#wpWatch' ); }
get submit() { return browser.element( '#wpConfirmB' ); }
@ -19,21 +19,16 @@ class DeletePage extends Page {
}
apiDelete( name, reason ) {
const MWBot = require( 'mwbot' ), // https://github.com/Fannon/mwbot
Promise = require( 'bluebird' );
let bot = new MWBot();
return Promise.coroutine( function* () {
yield bot.loginGetEditToken( {
apiUrl: `${browser.options.baseUrl}/api.php`,
username: browser.options.username,
password: browser.options.password
} );
yield bot.delete( name, reason );
} ).call( this );
return bot.loginGetEditToken( {
apiUrl: `${browser.options.baseUrl}/api.php`,
username: browser.options.username,
password: browser.options.password
} ).then( function () {
return bot.delete( name, reason );
} );
}
}
module.exports = new DeletePage();

View file

@ -1,8 +1,8 @@
'use strict';
const Page = require( './page' );
const Page = require( './page' ),
// https://github.com/Fannon/mwbot
MWBot = require( 'mwbot' );
class EditPage extends Page {
get content() { return browser.element( '#wpTextbox1' ); }
get displayedContent() { return browser.element( '#mw-content-text' ); }
get heading() { return browser.element( '#firstHeading' ); }
@ -19,21 +19,16 @@ class EditPage extends Page {
}
apiEdit( name, content ) {
const MWBot = require( 'mwbot' ), // https://github.com/Fannon/mwbot
Promise = require( 'bluebird' );
let bot = new MWBot();
return Promise.coroutine( function* () {
yield bot.loginGetEditToken( {
apiUrl: `${browser.options.baseUrl}/api.php`,
username: browser.options.username,
password: browser.options.password
} );
yield bot.edit( name, content, `Created page with "${content}"` );
} ).call( this );
return bot.loginGetEditToken( {
apiUrl: `${browser.options.baseUrl}/api.php`,
username: browser.options.username,
password: browser.options.password
} ).then( function () {
return bot.edit( name, content, `Created page with "${content}"` );
} );
}
}
module.exports = new EditPage();

View file

@ -1,13 +1,11 @@
'use strict';
const Page = require( './page' );
class HistoryPage extends Page {
get comment() { return browser.element( '#pagehistory .comment' ); }
open( name ) {
super.open( name + '&action=history' );
}
}
module.exports = new HistoryPage();

View file

@ -1,8 +1,11 @@
// From http://webdriver.io/guide/testrunner/pageobjects.html
'use strict';
/**
* Based on http://webdriver.io/guide/testrunner/pageobjects.html
*/
class Page {
open( path ) {
browser.url( browser.options.baseUrl + '/index.php?title=' + path );
}
}
module.exports = Page;

View file

@ -1,8 +1,6 @@
'use strict';
const Page = require( './page' );
class PreferencesPage extends Page {
get realName() { return browser.element( '#mw-input-wprealname' ); }
get save() { return browser.element( '#prefcontrol' ); }
@ -15,6 +13,6 @@ class PreferencesPage extends Page {
this.realName.setValue( realName );
this.save.click();
}
}
module.exports = new PreferencesPage();

View file

@ -1,4 +1,3 @@
'use strict';
const Page = require( './page' );
class RestorePage extends Page {
@ -16,6 +15,6 @@ class RestorePage extends Page {
this.reason.setValue( reason );
this.submit.click();
}
}
module.exports = new RestorePage();

View file

@ -1,8 +1,6 @@
'use strict';
const Page = require( './page' );
class UserLoginPage extends Page {
get username() { return browser.element( '#wpName1' ); }
get password() { return browser.element( '#wpPassword1' ); }
get loginButton() { return browser.element( '#wpLoginAttempt' ); }
@ -22,6 +20,6 @@ class UserLoginPage extends Page {
loginAdmin() {
this.login( browser.options.username, browser.options.password );
}
}
module.exports = new UserLoginPage();

View file

@ -1,4 +1,3 @@
'use strict';
const assert = require( 'assert' ),
DeletePage = require( '../pageobjects/delete.page' ),
RestorePage = require( '../pageobjects/restore.page' ),
@ -7,7 +6,6 @@ const assert = require( 'assert' ),
UserLoginPage = require( '../pageobjects/userlogin.page' );
describe( 'Page', function () {
var content,
name;
@ -28,14 +26,12 @@ describe( 'Page', function () {
} );
it( 'should be creatable', function () {
// create
EditPage.edit( name, content );
// check
assert.equal( EditPage.heading.getText(), name );
assert.equal( EditPage.displayedContent.getText(), content );
} );
it( 'should be re-creatable', function () {
@ -61,7 +57,6 @@ describe( 'Page', function () {
} );
it( 'should be editable', function () {
// create
browser.call( function () {
return EditPage.apiEdit( name, content );
@ -73,11 +68,9 @@ describe( 'Page', function () {
// check
assert.equal( EditPage.heading.getText(), name );
assert.equal( EditPage.displayedContent.getText(), content );
} );
it( 'should have history', function () {
// create
browser.call( function () {
return EditPage.apiEdit( name, content );
@ -86,11 +79,9 @@ describe( 'Page', function () {
// check
HistoryPage.open( name );
assert.equal( HistoryPage.comment.getText(), `(Created page with "${content}")` );
} );
it( 'should be deletable', function () {
// login
UserLoginPage.loginAdmin();
@ -107,11 +98,9 @@ describe( 'Page', function () {
DeletePage.displayedContent.getText(),
'"' + name + '" has been deleted. See deletion log for a record of recent deletions.\nReturn to Main Page.'
);
} );
it( 'should be restorable', function () {
// login
UserLoginPage.loginAdmin();
@ -130,7 +119,5 @@ describe( 'Page', function () {
// check
assert.equal( RestorePage.displayedContent.getText(), name + ' has been restored\nConsult the deletion log for a record of recent deletions and restorations.' );
} );
} );

View file

@ -1,11 +1,9 @@
'use strict';
const assert = require( 'assert' ),
CreateAccountPage = require( '../pageobjects/createaccount.page' ),
PreferencesPage = require( '../pageobjects/preferences.page' ),
UserLoginPage = require( '../pageobjects/userlogin.page' );
describe( 'User', function () {
var password,
username;
@ -22,17 +20,14 @@ describe( 'User', function () {
} );
it( 'should be able to create account', function () {
// create
CreateAccountPage.createAccount( username, password );
// check
assert.equal( CreateAccountPage.heading.getText(), `Welcome, ${username}!` );
} );
it( 'should be able to log in', function () {
// create
browser.call( function () {
return CreateAccountPage.apiCreateAccount( username, password );
@ -43,11 +38,9 @@ describe( 'User', function () {
// check
assert.equal( UserLoginPage.userPage.getText(), username );
} );
it( 'should be able to change preferences', function () {
var realName = Math.random().toString();
// create
@ -63,7 +56,5 @@ describe( 'User', function () {
// check
assert.equal( PreferencesPage.realName.getValue(), realName );
} );
} );

View file

@ -1,5 +1,3 @@
'use strict';
const fs = require( 'fs' ),
path = require( 'path' ),
logPath = process.env.LOG_DIR || './log/';