0
votes

Disclaimer: I've gone through so many different sources before I came here to ask this question. I've referenced the GitHub project for geb with maven, book of geb, numerous YouTube tutorials, etc. Nothing has worked.

I'm simply trying to just get a project up and running that does a very simple automated search engine test, just so I can play with the tools.

Here is my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion>
    <groupId>nope</groupId>
    <artifactId>nope</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>testing this</name>
    <description>testing this</description>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.spockframework/spock-core -->
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.1-groovy-2.4</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.13</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.gebish/geb-spock -->
        <dependency>
            <groupId>org.gebish</groupId>
            <artifactId>geb-spock</artifactId>
            <version>2.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-support -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-support</artifactId>
            <version>3.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.9.1</version>
        </dependency>


    </dependencies>
</project>

Main Groovy test class:

package com.na.tests

import spock.lang.Specification
import geb.*

class MyBaseTests extends Specification {

    def "search 'Groovy Browser Automation' in duckduckgo"() {

        given: "we are on the duckduckgo search-engine"
            go "http://duckduckgo.com"

        when: "we search for 'Groovy Browser Automation'"
            $("#search_form_homepage").q = "Groovy Browser Automation"
            $("#search_button_homepage").click()

        then: "the first result is the geb website"
            assert $("#links").find(".links_main a", 0).attr("href") == "http://www.gebish.org/"
    }
}

This is the exception I get in the test. I've run a really simple assert Hello World test that has passed, I eliminated that for the sake of clarity.:

groovy.lang.MissingMethodException: No signature of method: com.na.tests.MyBaseTests.go() is applicable for argument types: (java.lang.String) values: [http://duckduckgo.com] Possible solutions: is(java.lang.Object), Mock(), Spy(), any(), grep(), Mock(groovy.lang.Closure) at com.na.tests.MyBaseTests.search 'Groovy Browser Automation' in duckduckgo(MyBaseTests.groovy:22)

Edit

It's worth noting that in my IDE (Eclipse), it seems some of the keywords are either not recognized or are not legit (ie: go "http://duckduckgo.com"). Makes me feel like I haven't configured something.

2
You need to extend geb.spock.GebSpec and not Specification if you want to use geb. See also gebish.org/manual/current/#spock-junit-testng - Leonard Brünings
Ah.. Missed that. Now we're getting somewhere (exception for no class definition found WebWindowListener). I'm assuming I need to define a driver of some sort - StaticMaine
Confirmed - Now I'm playing with my config and making some progress. Thank you, please feel free to add an answer so I can give you credit! - StaticMaine
Maybe you also want to configure a Groovy compiler for your tests? GMavenPlus or Eclipse Groovy Compiler. - kriegaex

2 Answers

0
votes

You need to extend geb.spock.GebSpec and not Specification if you want to use geb. See also gebish.org/manual/current/#spock-junit-testng

0
votes

On GitHub I have a set of three Maven projects which together set up Spock, Geb and multi-browser testing via Maven configuration. You can switch the browser used easily among HtmlUnit, PhantomJS, Chrome, Firefox, IE, Edge, Opera. It comes with a small series of sample tests and a prepared Geb configuration:

Just clone and build them all mvn clean install in the order listed here and run the tests from the third. You can, of course, put everything into one module, but I think it makes more sense to keep version management and dependency management separate from the actual application so as to be able to re-use those test dependencies, as is good Maven practice.


Update:

Then just add your test to the sample project and run it. I renamed the class so as to end with *Test instead of *Tests because this is how Maven by default finds unit tests via Surefire. If you rather like it to run as an integration test because it opens a browser and basically is slow, rename it to *IT.

I also fixed the base class as Leonard suggested correctly and changed the explicit to an implicit assert.

package com.na.tests

import geb.spock.GebSpec

class MyBaseTest extends GebSpec {
  def "search 'Groovy Browser Automation' in duckduckgo"() {
    given: "we are on the duckduckgo search-engine"
    go "http://duckduckgo.com"

    when: "we search for 'Groovy Browser Automation'"
    $("#search_form_homepage").q = "Groovy Browser Automation"
    $("#search_button_homepage").click()

    then: "the first result (excluding ads) is the geb website"
    $("#links").$(".links_main a", 0).attr("href") == "http://www.gebish.org/"
  }
}