Search code examples
javaseleniumgradlejboss-arquillianshrinkwrap

How to add Gradle testCompile dependency to WebArchive using ShrinkWrap for Arquillian?


I have the following dependency on my gradle.build file, which I don't want to be placed on my final war file, but I need it in order to run Arquillian tests on test phase:

/* Snippet from build.gradle. */
testCompile 'org.seleniumhq.selenium:selenium-java:2.48.2'

If I were using Maven, I could write something like:

File[] files = Maven.resolver().resolve("org.seleniumhq.selenium:selenium-java:2.48.2").withTransitivity().asFile();

But, since I'm using Gradle resolver, I believe I can't do that:

@RunWith(Arquillian.class)
public abstract class ArquillianTestCase extends SeleniumTestCase {

    @Deployment
    public static WebArchive createDeployment() {

        /* Create the war file according to build.gradle. */
        WebArchive war = ShrinkWrap.create(EmbeddedGradleImporter.class)
                .forThisProjectDirectory().importBuildOutput()
                .as(WebArchive.class);

        /* Abstract test classes which depends on Selenium */
        war.addClasses(ArquillianTestCase.class, SeleniumTestCase.class);
        return war;
    }
}

Is there a way of doing this on Gradle? If I don't add the said dependency, my tests fail by NoClassDefFoundError on Selenium classes. However, if I change Selenium dependency to compile instead of testCompile, it works, but my war file size jumps from 3 to 20 megabytes, filled with test classes.

Thanks in advance.


Solution

  • Generally although you are using Gradle, you still can use those maven parts, since probably all your dependencies are in maven repositories. However sometimes you might need to explicitly define additionally maven repositories, since you don't have pom which sets them and ShrinkWrap can't set them automatically.

    You can combine these two provided by you calls into sth similiar to this:

    File[] files = Maven.resolver().resolve("org.seleniumhq.selenium:selenium-java:2.48.2").withTransitivity().asFile();
    
    WebArchive war = ShrinkWrap.create(EmbeddedGradleImporter.class)
                    .forThisProjectDirectory().importBuildOutput()
                    .as(WebArchive.class).addAsLibraries(files);
    

    Of course you have to add dependency for ShrinkWrap Resolver maven related modules.

    testCompile 'org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-api-maven:2.2.0'
    testCompile 'org.jboss.shrinkwrap.resolver:shrinkwrap-resolver-impl-maven:2.2.0'
    

    Btw, won't Arquillian Graphene solve your issue related to using both Selenium and Arquillian?