背景:我希望能够排列给定路线的 Angular2 屏幕的所有可能配置,并使用量角器对它们进行截图:http : //www.protractortest.org/#/debugging。
问题:我似乎无法在 e2e 测试期间找到访问指令(例如 ng-if)的方法。
可能的解决方案路径:以下是我如何访问路由器以导航到特定路线的示例:
it('should get the router', () => {
browser.executeScript(() => {
const ng = window['ng'];
const root = document.getElementsByTagName('app-root')[0];
const router = ng.probe(root)
.injector.get(ng.coreTokens.Router);
const routes = router.config;
...
...
return [];
}).then(ret=> console.log(ret));
});
想法:
我会感谢有关我的问题的任何提示。
你可以这样做:
html
<button id="find">Find ngIf directives and show all</button>
脚本
var findComments = function(el) {
var arr = [];
for(var i = 0; i < el.childNodes.length; i++) {
var node = el.childNodes[i];
if(node.nodeType === 8) {
arr.push(node);
} else {
arr.push.apply(arr, findComments(node));
}
}
return arr;
};
function findNgIfDirectives() {
var commentNodes = findComments(document);
const ng = window['ng'];
commentNodes.forEach(function(node) {
var debugNode = ng.probe(node);
var ngIf = (debugNode.providerTokens[0]);
var ngIfInstance = debugNode.injector.get(ngIf);
ngIfInstance.ngIf = true;
});
}
document.getElementById('find').addEventListener('click', findNgIfDirectives)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句