How to test jQuery UI Dialog during headless execution - javascript

Our app uses jQuery dialogs for modal popups. We're trying to write some unit tests for our javascript, and part of this is the creation of the dialogs. We're using Jasmine and the Jasmine Maven plugin. When running the tests in a browser, it all works fine. However, when we trying to run a headless execution of the tests (mvn jasmine:test), the tests fail on the lines creating the dialog. There error looks something like this:
* TypeError: Cannot find function dialog in object [object Object].
The line this happens on is simply like this:
$(element).dialog(popupProperties);
If it helps, our pom.xml has this for the plugin:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.2.0.0</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<preloadSources>
<source>${project.basedir}/src/main/webapp/resources/jquery.js</source>
<source>${project.basedir}/src/main/webapp/resources/jquery-ui.js</source>
<source>${project.basedir}/src/main/webapp/resources/angular.js</source>
<source>${project.basedir}/src/test/javascript/lib/angular-mocks.js</source>
<source>${project.basedir}/src/test/javascript/lib/jasmine-fixture.js</source>
</preloadSources>
<jsSrcDir>${project.basedir}/src/main/webapp/resources/</jsSrcDir>
<jsTestSrcDir>${project.basedir}/src/test/javascript/unit/</jsTestSrcDir>
</configuration>
</plugin>
Is there something we can do to get the headless execution working with the dialog stuff?

We figured out what the problem was. Basically the preloadSources from our src directory were getting added twice to the spec runner. It would do all the preoloadSources first, and then add everything from the src directory. So, jquery.js got added a 2nd time under the 2nd jquery-ui.js, and thus it didn't know what dialog was. We fixed it by changing our pom.xml to the following:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.2.0.0</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<preloadSources>
<source>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/jquery.js</source>
<source>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/jquery-ui.js</source>
<source>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/angular.js</source>
<source>${project.basedir}/src/test/javascript/lib/angular-mocks.js</source>
<source>${project.basedir}/src/test/javascript/lib/jasmine-fixture.js</source>
</preloadSources>
<jsSrcDir>${project.basedir}/src/main/webapp/resources/${ivplugin.client.version}/</jsSrcDir>
<jsTestSrcDir>${project.basedir}/src/test/javascript/unit/</jsTestSrcDir>
<sourceExcludes>
<exclude>**/jquery.js</exclude>
<exclude>**/jquery-ui.js</exclude>
<exclude>**/angular.js</exclude>
</sourceExcludes>
</configuration>
</plugin>
The sourceExcludes prevents it from getting added the 2nd time. This all seems like a bug with the plugin, but this allowed us to get around it.

Related

How to generate JavaScript file from Java Properties with Maven in Eclipse?

In an old Java web application there are many keys in properties files for internationalization. JavaScript functions also use this key to show some messages.
Actually this file is inline in HTML, dynamicly generated on JSPs, causing an increase of approximately 400 KB on each page request.
To improve performance slightly I generated JavaScript files with key|value with content like this:
var b = {
keys: {
"key.one": "value.one",
"key.two": "value.two"
}
};
The JavaScript file path look like this:
The message properties files are in the directory like this:
I need to read the message property files in the build to create the JavaScript files.
To do this, I am using exec-maven-plugin, set up this way:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<arguments>
<argument>${project.build.directory}/${project.build.finalName}</argument>
</arguments>
<mainClass>com.brunocesar.build.GenerateI18NJSFiles</mainClass>
</configuration>
</plugin>
GenerateI18NJSFiles reads the properties files and generates the JavaScript files, but only when running maven build (e.g.: mvn clean compile war:exploded).
I need to generate them also in Eclipse.
SO, I set up the Eclipse lifecycle-mapping plugin like this:
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<versionRange>[${exec-maven-plugin.version},)</versionRange>
<goals>
<goal>java</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute>
<runOnIncremental>false</runOnIncremental>
</execute>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
But it still is not generating the files in the Eclipse build, but only by Maven.
Is there is a way to set up Eclipse (maybe in m2e-wtp, maybe using another plugin) to generate the files and deploy in a container (such as JBoss, Tomcat, etc.)?

How can I change the lifecycle mapping in javascript-maven-plugin

Now, I using javascript-maven-plugin to develop my project.
In the documents, the lifecycle mapping is as follows:
http://mojo.codehaus.org/javascript-maven-tools/javascript-maven-plugin/extension-information.html
I want to import other library of files installed in repository of maven.
I expected that jslint-maven-plugin lifecycle was called after js-import-maven-plugin lifecycle.
However, the result turn out to be error, because the real lifecycle is as follows:
validate: Not defined
initialize: Not defined
generate-sources: Not defined
process-sources: org.codehaus.mojo:jslint-maven-plugin:1.0.1:jslint, org.codehaus.mojo:js-import-maven-plugin:1.0.1:import-js
generate-resources: org.codehaus.mojo:js-import-maven-plugin:1.0.1:generate-html
process-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:resources
compile: Not defined
process-classes: Not defined
generate-test-sources: Not defined
process-test-sources: org.codehaus.mojo:jslint-maven-plugin:1.0.1:test-jslint, org.codehaus.mojo:js-import-maven-plugin:1.0.1:test-import-js
generate-test-resources: org.codehaus.mojo:js-import-maven-plugin:1.0.1:test-generate-html
process-test-resources: org.apache.maven.plugins:maven-resources-plugin:2.5:testResources
test-compile: org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile
process-test-classes: Not defined
test: org.apache.maven.plugins:maven-surefire-plugin:2.9:test
prepare-package: org.codehaus.mojo:webminifier-maven-plugin:1.0.1:minify-js
package: Not defined
pre-integration-test: Not defined
integration-test: Not defined
post-integration-test: Not defined
verify: Not defined
install: org.apache.maven.plugins:maven-install-plugin:2.3.1:install
deploy: org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy
Now, I want to change the lifecycle mapping by editing the pom file like,
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jslint-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>default-jslint</id>
<phase>process-resources</phase>
</execution>
</executions>
</plugin>
However, it doesn't work.
How can I change the lifecycle mapping?
I’m not sure, but when you wanna change the phase, perhaps you must also specify the goal. Try this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jslint-maven-plugin</artifactId>
<version>1.0.1</version>
<executions>
<execution>
<id>default-jslint</id>
<phase>process-resources</phase>
<goals>
<goal>jslint</goal>
</goals>
</execution>
</executions>
</plugin>

jasmine-maven-plugin load source files and their dependencies

I've got a bunch of javascript files I'd like to test, located in src/main/webapp/js, but when I try to build on the command line, I get a bunch of errors due to the missing library files that each of my source scripts depend upon. I am trying to include them within the "sourceIncludes" tag, but this seems to do nothing. I can, however, place them all, one at a time, in the "preloadSources" tags, but I'd rather not do this.
Also, some of my sources files have functions that interact with the DOM, and including the .html file that contains the elements that these functions need just gives me:
"net.sourceforge.htmlunit.corejs.javascript.EcmaError: ReferenceError: "XML" is not defined"
my pom.xml below:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>src/main/webapp/js</jsSrcDir>
<jsTestSrcDir>src/test/javascript/js</jsTestSrcDir>
<sourceIncludes>
<include>src/main/webapp/js/lib/**/*.js</include>
<include>**/*.js</include>
</sourceIncludes>
<preloadSources>
<preloadSource>src/main/webapp/js/lib/jquery/jquery-1.7.2.js</preloadSource>
</preloadSources>
</configuration>
</plugin>
I hate having to list my dependencies in order. That's why I use a dependency loader like Require.js. I wrote a quick post summarizing my experience with jasmine-maven-plugin, but using require.js to load dependencies. It keeps my pom.xml clean, and I let my JavaScript files load their own dependencies. jasmine+maven+requirejs+code coverage

jasmine-maven-plugin and require-js result in path issues

I am using require-js to model dependencies in my java script project. I also use jasmine to write BDD styled test cases and the jasmine-maven-plugin:1.1.0 to execute these tests in headless mode.
This is my source project structure
project/
| src/main/javascript
| | api/*.js
| | main.js
| src/test/javascript
| | spec/*spec.js
This is my webapp pom jasmine configuration:
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>generateManualRunner</goal>
<goal>resources</goal>
<goal>testResources</goal>
<goal>test</goal>
<goal>preparePackage</goal>
</goals>
</execution>
</executions>
<configuration>
<specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
<scriptLoaderPath>lib/require-jquery.js</scriptLoaderPath>
<sourceIncludes>
<include>lib/require-jquery.js</include>
<include>lib/underscore.js</include>
<include>lib/backbone.js</include>
<include>lib/d3.js</include>
<include>lib/d3.layout.js</include>
<include>lib/bootstrap.js</include>
<include>main.js</include>
</sourceIncludes>
<preloadSources>
<source>lib/require-jquery.js</source>
<source>lib/underscore.js</source>
<source>lib/backbone.js</source>
<source>lib/d3.js</source>
<source>lib/d3.layout.js</source>
<source>lib/bootstrap.js</source>
<source>main.js</source>
</preloadSources>
<browserVersion>FIREFOX_3_6</browserVersion>
</configuration>
</plugin>
During the maven build all js sources are copied to target/jasmine/src. The runner.html that refers to my main.js is at target/jasmine.
Now the problem is that my require-js modules define their dependencies relative to the main.js, whereas in the maven test run, the runner assumes them to be relative to target/jasmine/runner.html. Since my modules are not (and must not be) aware of the additional jasmine/src folder, IMHO that is an issue.
I receive the following error message when I run mvn clean install:
Failed to execute goal
com.github.searls:jasmine-maven-plugin:1.1.0:test (default) on project
mywebapp: The jasmine-maven-plugin encountered an
exception: java.lang.RuntimeException:
org.openqa.selenium.WebDriverException:
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
com.gargoylesoftware.htmlunit.ScriptException: Wrapped
java.lang.RuntimeException: java.io.FileNotFoundException:
C:\Jenkins\jobs\job1\workspace\mywebapp\target\jasmine\api\data-util.js
(The system cannot find the path specified)
Anybody out there having some idea to work around or configure that?
I faced the same problem:
java.io.FileNotFoundException
with requireJS and jasmine
but you don't have to use the maven-resources-plugin and hack the js files to a second output directory.
I used 'srcDirectoryName'
you can find all possible params at https://github.com/searls/jasmine-maven-plugin/blob/master/src/main/java/com/github/searls/jasmine/mojo/AbstractJasmineMojo.java
and advanced examples under https://github.com/searls/jasmine-maven-plugin/tree/master/src/test/resources/examples
In the meantime I found a workaround or a fix (not sure) by configuring the maven-resources-plugin
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-js-files</id>
<phase>generate-test-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/jasmine</outputDirectory>
<resources>
<resource>
<directory>src/main/javascript</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/webapp</directory>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/test/webapp</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
I put that configuration in a separate profile which I activate during testing.
I see that you've got lib/require-jquery.js, but your file tree doesn't show where "lib" is located. is loaded relative to , but you don't have one. Maybe that's your problem. I wrote a quick post summarizing my experience with the jasmine-maven-plugin and using Require.js. In it I've got my pom.xml. My pom.xml seems quite a bit smaller than yours but the plugin works fine for me. Maybe my post will help. Jasmine+Maven+Requirejs+Coverage

How can I stop a default plugin execution?

I'm using a javascript maven plugin. I like the war-package goal and want to use it.
However it also comes with a jsunit goal bound to the test phase. This part of the plugin isn't great so I want to just turn it off. How can I do this?
Plugin xml is below.
Edit : Have tried adding executions with phase none, but the goals still run.
<plugin>
<groupId>org.codehaus.mojo.javascript</groupId>
<artifactId>javascript-maven-plugin</artifactId>
<version>1.0</version>
<extensions>true</extensions>
<executions>
<execution>
<id>copyDependenciesForJasmine</id>
<phase>compile</phase>
<goals>
<goal>war-package</goal>
</goals>
<configuration>
<webappDirectory>${project.build.directory}/javascript</webappDirectory>
<scriptsDirectory>src</scriptsDirectory>
<libsDirectory>../dependencies</libsDirectory>
</configuration>
</execution>
<execution>
<id>jsunit</id>
<phase>none</phase>
</execution>
<execution>
<id>prepare-tests</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
A similar question was already asked here. You may just specify execution phase to none on the specific plugin.

Categories