Last active
December 30, 2015 23:49
-
-
Save amphro/7903051 to your computer and use it in GitHub Desktop.
Run an org's namespace tests from the developer console. Just open the browser's Javascript console and past the code in. Update: Will now use an unsupported servlet instead of the Tooling API to prevent compilation if the SymbolTables are not in the cache. To use the Tooling API, change line 115/116.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function runNamespacedTest(namespace) { | |
var thisOrgsNs = SfdcDevConsole.hasNamespace() ? SfdcDevConsole.namespace : ''; | |
var ns = namespace || thisOrgsNs; | |
function log(msg) { | |
if (console && console.log) console.log(msg); | |
} | |
function showErrorMessage() { | |
Ext.Msg.alert('Running namespaced tests failed', 'Please look at the javascript console for more information.'); | |
} | |
function enqueueTest(ids) { | |
if (ids.length === 0) { | |
Ext.Msg.alert('No tests enqueued', 'No test found in namespace "'+ns+'"'); | |
return; | |
} | |
log('Enqueuing ' + ids.length + ' tests'); | |
Ext.Msg.updateProgress(.6, 'Enqueuing ' + ids.length + ' tests'); | |
if (SfdcDevConsole.ToolingAPI.runTests) { | |
SfdcDevConsole.ToolingAPI.runTests({classids: ids}, { | |
continuation: function() { | |
Ext.Msg.close(); | |
}, | |
failure : showErrorMessage | |
}); | |
} else { | |
//Support for API v29. runTests was added in v30. | |
Ext.Ajax.request({ | |
url : '/_ui/common/apex/test/ApexTestQueueServlet', | |
params : { | |
action:'ENQUEUE', | |
classid:ids | |
}, | |
success : function() { | |
Ext.Msg.close(); | |
} | |
}); | |
} | |
} | |
function getTestIdsFromApexClasses(classes) { | |
var ids = []; | |
for (var i = 0; i < classes.length; i++) { | |
var rec = classes[i]; | |
var st = rec.SymbolTable; | |
var mods = st.tableDeclaration.modifiers; | |
var modLen = mods.length; | |
while (modLen--) { | |
if (mods[modLen] === 'TEST') { | |
log('Found test: '+st.name); | |
ids.push(rec.Id); | |
} | |
} | |
} | |
return ids; | |
} | |
function enqueueTestClassesRetreivedFromToolingAPI() { | |
SfdcDevConsole.ToolingAPI.query("SELECT Id, SymbolTable FROM ApexClass WHERE NamespacePrefix = '"+ns+"'", { | |
continuation : function(r) { | |
Ext.Msg.updateProgress(.3, 'Finding tests classes via symbol tables'); | |
try { | |
enqueueTest(getTestIdsFromApexClasses(r.records)); | |
} catch(e) { | |
showErrorMessage(); | |
if (console && console.error) console.error(e); | |
throw e; | |
} | |
}, | |
failure : showErrorMessage | |
}); | |
} | |
function enqueueTestClassesRetreivedFromInternalServlet() { | |
Ext.Ajax.request({ | |
url : '/_ui/common/apex/test/ApexTestQueueServlet', | |
params : { | |
action:'GET_TESTS' | |
}, | |
success : function(response) { | |
Ext.Msg.updateProgress(.3, 'Finding tests classes via internal servlet'); | |
try { | |
var res = Util.evalAjaxServletOutput(response.responseText); | |
console.log(res); | |
var classes = res.testClasses; | |
var ids = []; | |
//The internal servlet returns "My Namespace" when in the orgs namespace... weird. | |
var nsName = thisOrgsNs === ns ? 'My Namespace' : ns; | |
for (var i = 0; i < classes.length; i++) { | |
var c = classes[i]; | |
if (c.ns.indexOf(nsName) >= 0) { | |
ids.push(c.id); | |
log('Found test: '+c.name); | |
} | |
} | |
enqueueTest(ids); | |
} catch(e) { | |
showErrorMessage(); | |
if (console && console.error) console.error(e); | |
throw e; | |
} | |
} | |
}); | |
} | |
Ext.Msg.progress('Running namespaced tests', 'Fetching apex classes'); | |
//enqueueTestClassesRetreivedFromToolingAPI(); | |
enqueueTestClassesRetreivedFromInternalServlet(); | |
} | |
//Run the org's namespaced tests, or enter a namespace. | |
runNamespacedTest(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment