如何获取所有JS文件名

发布于 2024-10-13 01:48:30 字数 342 浏览 1 评论 0原文

如何获取我的应用程序的所有 JS 文件名称?

我测试过:

@Inject
private ClientInfrastructure javascriptStack;

void onActivate(){
    mesJavaScripts=javascriptStack.getJavascriptStack();

    for(Asset javascript : mesJavaScripts){
      System.out.println(javascript.toString());
    }
}

但是我没有所有的JS。我使用优质服务吗?

谢谢

How can I get all JS files name of my application ?

I tested :

@Inject
private ClientInfrastructure javascriptStack;

void onActivate(){
    mesJavaScripts=javascriptStack.getJavascriptStack();

    for(Asset javascript : mesJavaScripts){
      System.out.println(javascript.toString());
    }
}

But I do not have all the JS. Do I use the good service ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁我热情 2024-10-20 01:48:30

我还是不符合这里的需要;响应中是否包含必要的JS文件是一个集成测试问题;您可以使用 PageTester(通过遍历返回的 DOM)或在 Selenium 中(再次通过遍历客户端 DOM)来确定这一点。

事实上,理想情况下(这很困难),您可以在 Selenium 中编写测试,只有在必要的 JS 就位时才能通过。

鉴于 Tapestry 会在生产模式下将您的脚本聚合到 JS 堆栈中(这可能在 5.3 中以某种方式发生变化),您正在测试状态而不是行为。始终测试行为

I still don't follow the need here; the question of whether necessary JS files are included in the response is an integration test issue; you can determine this using PageTester (by walking the returned DOM) or in Selenium (again, by walking the client DOM).

In fact, ideally (and this is difficult) you could write tests, in Selenium, that only pass if the necessary JS is in place.

Given that Tapestry will, in production mode, aggregate your scripts together into JS stacks (and that may change in some way in 5.3) you are testing state rather than behavior. Always test behavior.

冰火雁神 2024-10-20 01:48:30

这种方式似乎有效,尽管它依赖于一些可能改变的内部结构,并且可能会减慢速度。根据您的需要,您可能希望根据您关心的页面中设置的请求参数进行过滤(添加 RequestGlobals 作为参数并使用 set/getAttribute)。

更好的方法可能是提供您自己的 DocumentLinker,在添加脚本时捕获脚本。

将其添加到您的 AppModule:

public void contributeMarkupRenderer(OrderedConfiguration<MarkupRendererFilter> configuration, final Environment environment, final RequestGlobals request) {
  MarkupRendererFilter getScripts = new MarkupRendererFilter() {
    public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer) {
      DocumentLinker dl = environment.peek(DocumentLinker.class);
      renderer.renderMarkup(writer);
      try {
        List<String> scripts = (List<String>)request.getAttribute("scripts");
        if (scripts != null) {
          scripts.clear();
          Field f = dl.getClass().getDeclaredField("scripts");
          f.setAccessible(true);
          scripts.addAll((List<String>)f.get(dl));
        }
      } catch (Exception e) {}
    }
  };
  configuration.add("getScripts", getScripts, "after:DefaultValidationDecorator");
}

在您的测试中:

request.setAttribute("scripts", new ArrayList<String>());

执行页面

assert request.getAttribute("scripts").contains(path_to_script_you_want_to_check);

您可能必须更改任一侧以去除 Tapestry 添加的额外路径信息。

This way seems to work, although it relies on some internals that could change and it may slow things down a bit. Depending on your need, you might want to filter that based on a request parameter set in the pages you care about (add RequestGlobals as a parameter and use set/getAttribute).

A better way may be to provide your own DocumentLinker that captures the scripts as they get added.

Add this to your AppModule:

public void contributeMarkupRenderer(OrderedConfiguration<MarkupRendererFilter> configuration, final Environment environment, final RequestGlobals request) {
  MarkupRendererFilter getScripts = new MarkupRendererFilter() {
    public void renderMarkup(MarkupWriter writer, MarkupRenderer renderer) {
      DocumentLinker dl = environment.peek(DocumentLinker.class);
      renderer.renderMarkup(writer);
      try {
        List<String> scripts = (List<String>)request.getAttribute("scripts");
        if (scripts != null) {
          scripts.clear();
          Field f = dl.getClass().getDeclaredField("scripts");
          f.setAccessible(true);
          scripts.addAll((List<String>)f.get(dl));
        }
      } catch (Exception e) {}
    }
  };
  configuration.add("getScripts", getScripts, "after:DefaultValidationDecorator");
}

In your test:

request.setAttribute("scripts", new ArrayList<String>());

execute the page

assert request.getAttribute("scripts").contains(path_to_script_you_want_to_check);

You might have to change either side to strip off the extra path information that Tapestry adds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文