Replace pauses in rollback tests w/ dynamic waits
authorTim Eulitz <tim.eulitz@wikimedia.de>
Mon, 15 Apr 2019 10:21:22 +0000 (12:21 +0200)
committerTim Eulitz <tim.eulitz@wikimedia.de>
Tue, 30 Apr 2019 08:55:24 +0000 (10:55 +0200)
Bug: T220479
Change-Id: I95cf06a4d6a677ca14b56f11f5c6bd98aa0abd05

tests/selenium/specs/rollback.js
tests/selenium/wdio-mediawiki/LoginPage.js
tests/selenium/wdio-mediawiki/Util.js

index 970fb9e..51a1fc6 100644 (file)
@@ -17,7 +17,7 @@ describe( 'Rollback with confirmation', function () {
                // Requires user to log in again, handled by deleteCookie() call in beforeEach function
                UserLoginPage.loginAdmin();
 
-               browser.pause( 300 );
+               UserLoginPage.waitForScriptsToBeReady();
                browser.execute( function () {
                        return ( new mw.Api() ).saveOption(
                                'showrollbackconfirmation',
@@ -48,22 +48,22 @@ describe( 'Rollback with confirmation', function () {
                assert.strictEqual( HistoryPage.rollbackConfirmableNo.getText(), 'Cancel' );
        } );
 
-       it.skip( 'should offer a way to cancel rollbacks', function () {
+       it( 'should offer a way to cancel rollbacks', function () {
                HistoryPage.rollback.click();
 
-               browser.pause( 300 );
+               HistoryPage.rollbackConfirmableNo.waitForVisible( 5000 );
 
                HistoryPage.rollbackConfirmableNo.click();
 
-               browser.pause( 500 );
+               browser.pause( 1000 ); // Waiting to ensure we are NOT redirected and stay on the same page
 
                assert.strictEqual( HistoryPage.heading.getText(), 'Revision history of "' + name + '"' );
        } );
 
-       it.skip( 'should perform rollbacks after confirming intention', function () {
+       it( 'should perform rollbacks after confirming intention', function () {
                HistoryPage.rollback.click();
 
-               browser.pause( 300 );
+               HistoryPage.rollbackConfirmableYes.waitForVisible( 5000 );
 
                HistoryPage.rollbackConfirmableYes.click();
 
@@ -104,7 +104,7 @@ describe( 'Rollback without confirmation', function () {
                // Requires user to log in again, handled by deleteCookie() call in beforeEach function
                UserLoginPage.loginAdmin();
 
-               browser.pause( 300 );
+               UserLoginPage.waitForScriptsToBeReady();
                browser.execute( function () {
                        return ( new mw.Api() ).saveOption(
                                'showrollbackconfirmation',
index 8838530..60855f8 100644 (file)
@@ -1,4 +1,5 @@
-const Page = require( './Page' );
+const Page = require( './Page' ),
+       Util = require( 'wdio-mediawiki/Util' );
 
 class LoginPage extends Page {
        get username() { return browser.element( '#wpName1' ); }
@@ -20,6 +21,10 @@ class LoginPage extends Page {
        loginAdmin() {
                this.login( browser.options.username, browser.options.password );
        }
+
+       waitForScriptsToBeReady() {
+               Util.waitForModuleState( 'mediawiki.api' );
+       }
 }
 
 module.exports = new LoginPage();
index 247c958..dd08ee9 100644 (file)
@@ -1,5 +1,21 @@
 module.exports = {
        getTestString( prefix = '' ) {
                return prefix + Math.random().toString() + '-Iñtërnâtiônàlizætiøn';
+       },
+
+       /**
+        * Wait for a given module to reach a specific state
+        * @param {string} moduleName The name of the module to wait for
+        * @param {string} moduleStatus 'registered', 'loaded', 'loading', 'ready', 'error', 'missing'
+        * @param {int} timeout The wait time in milliseconds before the wait fails
+        */
+       waitForModuleState( moduleName, moduleStatus = 'ready', timeout = 2000 ) {
+               browser.waitUntil( () => {
+                       const result = browser.execute( ( module ) => {
+                               return typeof mw !== 'undefined' &&
+                                       mw.loader.getState( module.name ) === module.status;
+                       }, { status: moduleStatus, name: moduleName } );
+                       return result.value;
+               }, timeout, 'Failed to wait for ' + moduleName + ' to be ' + moduleStatus + ' after ' + timeout + ' ms.' );
        }
 };