≥ UI5 1.67
With the commit 54df6ca
, no more workarounds are needed. The modules sap/ui/core/Element
and sap/ui/core/Component
provide public APIs such as .all()
, .filter()
, .forEach()
, .get()
, and .size
. Take a look at the API reference respectively:
Registry of all sap.ui.core.Elements
that currently exist.
Registry of all Components
that currently exist.
Sample
sap.ui.require([
"sap/ui/core/Element" // or "sap/ui/core/Component"
], ({ registry }) => {
const registeredStuff = registry.all(); // or .filter(fn, this), .forEach(fn, this), .get("..."), .size
console.log(registeredStuff);
});
If the application runs in UI5 below 1.67, keep reading for workarounds..
≤ UI5 1.66 (Original answer)
Is there a way to get a full list of instances in the control registry?
With a bit of cheating, yes!
Option 1 - via .mElements
from the real core
getRegisteredElements: function() {
let core;
const fakePlugin = {
startPlugin: realCore => core = realCore
};
// "Core" required from "sap/ui/core/Core"
Core.registerPlugin(fakePlugin);
Core.unregisterPlugin(fakePlugin);
return core.mElements;
},
The API registerPlugin
awaits an object that contains a method startPlugin
(and stopPlugin
) as an argument. It invokes the startPlugin
method immediately as long as the core is initialized. As a parameter, we're getting the real core from which we can get the map of all registered elements via mElements
(thanks to the hint from Serban).
?? .mElements
is a private member. It has been removed in future UI5 releases!
?? Core.registerPlugin()
and .unregisterPlugin()
are deprecated since 1.73.
Option 2 - via Core.byFieldGroupId
(controls only)
getRegisteredControls: function() { // "Core" required from "sap/ui/core/Core"
return Core.byFieldGroupId("" || []); // pass an empty string or an empty array!
},
This will return an array of all registered elements that are of type sap.ui.core.Control
(source). Passing ""
or []
ensures that all controls are returned, regardless whether the control has a field group ID or not.
Option 3 - via Opa Plugin (for tests)
When writing tests, another option is to use the dedicated public API getAllControls
from sap.ui.test.OpaPlugin
:
new OpaPlugin().getAllControls(); // "OpaPlugin" required from "sap/ui/test/OpaPlugin"
Although the name suggests it will return Controls, it actually returns Element instances as well.
The plugin provides some other interesting APIs too, such as getMatchingControls
(with options to provide controlType?
, visible?
, interactable?
, etc..) which might be useful.