0

我创建了一个项目:

mvn archetype:generate -DarchetypeGroupId=com.github.searls -DarchetypeArtifactId=jasmine-archetype -DarchetypeVersion=1.3.1.5 -DgroupId=com.acme -DartifactId=my-jasmine-project -Dversion=0.0.1-SNAPSHOT

首先我用mvn jasmine:test 测试测试运行正常。

注意:我使用 Maven 3.2.1

在“目标”目录中,我添加了一个文件夹“phantomjs-1.9.7-windows”,在其中放置“phantomjs.exe”

我修改了我原来的 pom.xml 以添加

                ...
    <configuration>
        <srcDirectoryName>${project.artifactId}/src/main/javascript</srcDirectoryName>
        <webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
        <webDriverCapabilities>
            <capability>
                <name>phantomjs.binary.path</name>
                <value>${project.build.directory}/phantomjs-1.9.7-${os.phantomJS}/${run.phantomJS}</value>
            </capability>
        </webDriverCapabilities>


        <preloadSources>
            <source>${project.basedir}/src/main/javascript/libs/jquery-1.9.1.js</source>
            <source>${project.basedir}/src/main/javascript/libs/angular.js</source>
            <source>${project.basedir}/src/main/javascript/libs/angular-ui-router.js</source>
            <source>${project.basedir}/src/main/javascript/libs/angular-resource.js</source>
        </preloadSources>
    </configuration>
    ...

我在“src/main/javascript”下创建了一个文件夹“libs”并将其放入: - jquery-1.9.1.js - angular.js - angular-ui-router.js - angular-resource.js

注意:角度在版本 1.2.23 中

之后,我在“src/main/javascript”下创建了另一个文件夹“app”并将其放入:“app.module.js”

(function() {
    'use strict';

    angular.module('app', [
         'app.Security.modules'
    ]);
})();

在“src/main/javascript/app”下,我创建了一个文件夹“security”并将其放入:“security.module.js”

(function() {
'use strict';
    angular.module('app.Security.modules', []);
})();

和“cha_directive.js”

angular.module('app.Security.modules').directive('inputOld', InputAccessKeyManagement);
function InputAccessKeyManagement () {}

如果我用mvn jasmine:test 进行测试,我会得到一个错误:

[ERROR - 2015-02-26T08:12:49.751Z] Session [40eacc20-bd8f-11e4-a736-9934c466a5ff] - page.onError - stack:
  (anonymous function) (http://localhost:51170/jasmine-project/src/main/javascript/libs/angular.js:1679)
  ensure (http://localhost:51170/jasmine-project/src/main/javascript/libs/angular.js:1601)
  module (http://localhost:51170/jasmine-project/src/main/javascript/libs/angular.js:1892)
  (anonymous function) (http://localhost:51170/jasmine-project/src/main/javascript/app/security/cha_directive.js:1)
[WARNING] JavaScript Console Errors:

  * Error: [$injector:nomod] Module 'app.Security.modules' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.2.23/$injector/nomod?p0=app.Security.modules


[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.163s
[INFO] Finished at: Thu Feb 26 09:12:49 CET 2015
[INFO] Final Memory: 12M/210M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.searls:jasmine-maven-plugin:1.3.1.5:test (default-cli) on project jasmine-project: The jasmine-maven-plugin encountered an exception:
[ERROR] java.lang.RuntimeException: java.lang.RuntimeException: There were javascript console errors.
[ERROR] at com.github.searls.jasmine.runner.SpecRunnerExecutor.execute(SpecRunnerExecutor.java:46)
[ERROR] at com.github.searls.jasmine.mojo.TestMojo.executeSpecs(TestMojo.java:86)

如果我只是在“toto_directive.js”重命名“cha_directive.js ”(使用相同的源代码)并再次运行mvn jasmine:test,测试将正常运行。

如果我使用“cha_directive.js”名称文件并运行mvn jasmine:bdd。之后我在 FF 中运行 url http://localhost:8234测试正常运行。

唯一的事情(如果我用 fireBug 编辑“Jasmine Spec Runner”的 HTML 代码)是我可以看到这个标签:

<head jmp_jserror="Error: [$injector:nomod] Module 'app.Security.modules' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument. http://errors.angularjs.org/1.2.23/$injector/nomod?p0=app.Security.modules">

如果我在“checkForConsoleErrors”中查看 SpecRunnerExecutor.java 类,则有一个测试:

WebElement head = driver.findElement(By.tagName("head"));
if (head != null) {
    String jserrors = head.getAttribute("jmp_jserror");
    if (StringUtils.isNotBlank(jserrors)) {
        throw new RuntimeException("There were javascript console errors.");
    }
}

注意:其他文件名使测试崩溃:input_directive.js,accessKey_directive.js,...

4

2 回答 2

1

这是导入脚本顺序的问题。

我声明

(function() {
   'use strict';
   angular.module('app.Security.modules', []);
})();

在名为“ s ecurity.module.js”的文件中

我尝试使用我的声明

angular.module('app.Security.modules')
  .directive('inputOld', InputAccessKeyManagement);
  function InputAccessKeyManagement () {}

在名为“ c ha_directive.js”的文件中

如果您未指定自定义导入脚本顺序,则默认顺序为字母顺序,则“ c ha_directive.js”位于“ s ecurity.module.js”之前。

我使用“customRunnerTemplate”解决了这个问题。

但也许还有另一种方法可以做“更漂亮”?

于 2015-03-02T08:08:00.170 回答
1

如果其他人遇到这种情况,一个好的解决方案是在您的 sourceInclude 块中执行类似的操作:

<sourceInclude>
  <include>**/*.module.js</include>
  <include>**/*.controller.js</include>
  <include>**/*.service.js</include>
...

等等。假设您符合将模块定义在一个名为“example.module.js”的文件中的文件中,则模块将首先加载,然后是控制器等

于 2016-12-13T14:58:48.607 回答