Recently, I have been trying to use Arquillian Graphene 2 to do JavaScript unit testing. I spent a lot of time trying to get the examples on the Graphene 2 - JavaScript Interface wiki to work. I discovered that they were slightly incorrect and the source of my grief. One of the great things about an Open Source world is that I updated the wiki with the correct information.
I have created a couple of Proof of Concept (POC) projects to demonstrate how to use Graphene to do JS testing. The first example uses Graphene in stand-alone mode. This mode allows you to test your JavaScript outside of a container, but using a browser implementation like: PhantomJS, Chrome, Firefox, or Safari.
The Apache Maven NetBeans 8.0 project can be downloaded from Bitbucket here: graphene-js-poc
You will need to execute this from the command line, or use the JS Unit Test custom goal in NetBeans.
1 | mvn -Dbrowser=phantomjs clean test |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.bluelotusoftware.example; import org.jboss.arquillian.drone.api.annotation.Drone; import org.jboss.arquillian.graphene.javascript.JavaScript; import org.jboss.arquillian.junit.Arquillian; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.openqa.selenium.WebDriver; /** * * @author John Yeary * @version 1.0 */ @RunWith (Arquillian. class ) public class GreetingTest { @JavaScript private Greeting greeting; @Drone private WebDriver driver; @Test public void testGreeting() { assertEquals( "Hello John" , greeting.greeting( "John" )); } @Test public void testWarning() { assertNull(greeting.warning( "John" )); } } |
The Apache Maven NetBeans 8.0 project can be downloaded from Bitbucket here: graphene-poc.
The project includes a JSF example along with testing JavaScript.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.bluelotusoftware.example; import org.jboss.arquillian.drone.api.annotation.Drone; import org.jboss.arquillian.graphene.javascript.JavaScript; import org.jboss.arquillian.junit.Arquillian; import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.openqa.selenium.WebDriver; @RunWith (Arquillian. class ) public class HelloWorldTest { // Required browser injection @Drone private WebDriver browser; @JavaScript private HelloWorld helloWorld; @Test public void testHelloWorld() { assertEquals( "Hello World!" , helloWorld.hello()); } } |