CasperJS不会加载由Angular路由器注入的内容

我在GitHub上准备了一个示例项目 ,以配合我正在编写的关于如何为dockerized应用程序编写functiontesting的课程。 该应用程序有一个是Angular2单页面应用程序的一部分,我试图用CasperJStesting这个应用程序(我也试过与下面解释的相同问题的Behat)。

当在CasperJS中运行testing时,就好像Angular中的路由不会加载testing,因此我可以断言索引模板中的东西存在(例如页面标题),但是<app-root></app-root>标记不会加载testing。

代码

这里是索引模板:

 <!doctype html> <html lang="en"> <head <meta charset="utf-8"> <title>TestProject</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <app-root></app-root> </body> </html> 

testing是:

 casper.test.begin('Tests homepage structure', function suite(test) { casper.start('http://web', function() { // This works because the title is set in the "parent" template. test.assertTitle("TestProject", "Title is correct"); casper.wait(2000); // This fails, I'm guessing because the h2 is only in the "child" template, // it seems that CasperJS doesn't render the angular2 app correctly // and the child route templates are not injected into the page correctly. test.assertVisible('h2'); }); casper.run(function() { test.done(); }); }); 

这是testing的输出:

 Test file: homepage.js # Tests homepage structure [info] [phantom] Starting... [info] [phantom] Running suite: 2 steps PASS Tests homepage structure (NaN test) [debug] [phantom] opening url: http://web/, HTTP GET [debug] [phantom] Navigation requested: url=http://web/, type=Other, willNavigate=true, isMainFrame=true [debug] [phantom] url changed to "http://web/" [debug] [phantom] Successfully injected Casper client-side utilities [info] [phantom] Step anonymous 2/2 http://web/ (HTTP 200) PASS Title is correct FAIL Selector is visible # type: assertVisible # file: homepage.js # subject: false # selector: "h2" [info] [phantom] Step anonymous 2/2: done in 178ms. [info] [phantom] Step _step 3/3 http://web/ (HTTP 200) [info] [phantom] Step _step 3/3: done in 196ms. [info] [phantom] wait() finished waiting for 2000ms. [info] [phantom] Done 3 steps in 2215ms 

请注意,这些testing和Web运行在Docker容器中,这就是为什么上面的代码中的主机名是“web”而不是IP。

这个应用程序在GitHub上。 要运行该应用程序,请执行docker-compose up在根中编写并浏览到http://127.0.0.1:81/ 。 运行CasperJStesting运行./tests/web/run_functional_test.sh ,你不需要事先运行./tests/web/run_functional_test.sh docker-compose up

东西试过了

我试图通过CasperJS的加载页面上输出任何控制台错误,看看是否CasperJS遇到问题,但我还没有发现任何可疑。

在浏览器中运行时,应用程序可以正常工作,我可以看到路由器注入索引模板的内容。

我曾尝试使用resurrectio在Chrome中访问我的页面,单击几件事情,然后将此testing直接导出到CasperJStesting文件,由于相同的原因,此自动生成的testing失败,路由器注入的内容无法访问CasperJS。

我曾尝试使用类似的结果Behat和Selenium。 这表明问题不在于CasperJS / PhantomJS(也许是Angular设置?)。

这个问题

我不是PhantomJS /selenium专家,但我明白,他们可以渲染JavaScript,所以这是正常的,期望CasperJS和Behat应该能够运行Angular路由器插入到页面的东西的断言,我是错误的假设呢?

如果不是,我的testing是怎么回事? 为什么CasperJStesting不能针对路由中的模板运行断言?

最后问题是简单的问题,简单的问题通常是让你把头发拉出来的问题。

在我的web应用程序中,我使用“127.0.0.1:82”作为API的连接string。 这在当地工作,因为docker组成文件映射主机的端口82,但我没有映射端口82的testing,所以networking无法连接到API。 修复在这里 。

我不是PhantomJS /selenium专家,但我明白,他们可以渲染JavaScript,所以这是正常的,期望CasperJS和Behat应该能够运行Angular路由器插入到页面的东西的断言,我是错误的假设呢?

我当然认为这是正确的。