0

I have set up Geb + Spock + Groovy and able to run a one sample script successfully. Now I have created one method in another groovy class (this class I have putted in resource folder) which I am calling in my test class but it giving me below error :

Unable to resolve FileHandling as content for Page, or as a property on its Navigator context. Is FileHandling a class you forgot to import?

"FileHandling" is name of my class which contains the method.I am able to run this method successfully as separate entity but when I am calling it in my test class and running it through pom.xml, I am getting above error.

Please let me know how this can be resolved. The code which is causing issue is below.

package test.groovy

import geb.spock.GebReportingSpec
import spock.lang.*
import FileHandling.*

@Stepwise
public class RateTest extends GebReportingSpec {    
    def "open application home page"() {
        when:
        go() // uses base url system property
        def path_act = "C:/Users/abc.xlsx"
        def cellArrayActual = FileHandling.returnExcelResults(path_act, "MEMBER_PREMIUM")

        then:
        title == "Welcome"
    }
}

I feel the problem is not in code, something wrong in POM.xml dependencies, Please let me know what is wrong in it.

http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>Automation</groupId>
<artifactId>Automation</artifactId>
<version>0.0.1-SNAPSHOT</version>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
        </plugin>

        <plugin>
            <groupId>org.codehaus.gmaven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.codehaus.gmaven.runtime</groupId>
                    <artifactId>gmaven-runtime-1.8</artifactId>
                    <version>1.4</version>
                </dependency>
                <dependency>
                    <groupId>org.codehaus.groovy</groupId>
                    <artifactId>groovy-all</artifactId>
                    <version>2.1.8</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>


<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.9</version>
    </dependency>
    <dependency>
        <groupId>org.gebish</groupId>
        <artifactId>geb-spock</artifactId>
        <version>0.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>0.7-groovy-2.0</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.37.1</version>
    </dependency>

</dependencies>

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Looks like the code is cut short. – Peter Niederwieser Nov 05 '13 at 13:54
  • 1
    Why do you put the utility class in a resource folder? You should put it next to your test classes. – Peter Niederwieser Nov 05 '13 at 13:55
  • Thanks Peter for your reply but I have already tried that too with no luck. It always gives me same error. – user2956350 Nov 05 '13 at 15:59
  • I tried with few changes by explicitly declaring my classes as public and sometimes it is able to run the utility method but after that it is throwing below exception: (test.groovy.RateTest): failed to create driver from callback 'test.resources.script1383671498997840719332$_run_closure1@bafdff' (test.groovy.RateTest): failed to create driver from callback 'test.resources.script1383671498997840719332$_run_closure1@bafdff' test.groovy.RateTest: failed to create driver from callback 'test.resources.script1383671498997840719332$_run_closure1@bafdff' – user2956350 Nov 05 '13 at 17:13
  • I'd first try to get a simple Spock unit test to work that doesn't involve Geb. There may be a fundamental problem with your setup, but since you don't provide more details, I can't say what it is. – Peter Niederwieser Nov 05 '13 at 17:13
  • I have added my test class code in above question – user2956350 Nov 05 '13 at 17:17
  • Also I have already setted up seperate Spock, Geb and Groovy projects and all are working fine. – user2956350 Nov 05 '13 at 17:23
  • @PeterNiederwieser - FYI, if I do not use the utility class method then everything works well, is there anything conflicting with apache poi and webdriver dependencies position. – user2956350 Nov 05 '13 at 17:30

2 Answers2

1

If FileHandling is your class, shouldn't your import be import FileHandle rather than import FileHandle.*?

inanutshellus
  • 9,683
  • 9
  • 53
  • 71
  • `import FileHandle.*` is definitely wrong. Actually, Groovy should throw a compile error here. – Peter Niederwieser Nov 05 '13 at 17:33
  • Ya you are absolutely correct, I also do have same thoughts but I am getting below error if I use import FileHandling ------> [ERROR] Failed to execute goal org.codehaus.gmaven:gmaven-plugin:1.5:testCompile (default) on project RateAutomation: startup failed: [ERROR] file:/D:/Spock_Geb/RateAutomation/src/test/groovy/RateTest.groovy: 5: unable to resolve class FileHandling [ERROR] @ line 5, column 1. [ERROR] import FileHandling [ERROR] ^ – user2956350 Nov 05 '13 at 17:35
  • Probably you are missing the package name. Or you didn't put the class in the correct directory. In other words, the cause of your problem isn't related to Spock or Geb. – Peter Niederwieser Nov 05 '13 at 17:36
  • There is no seperate package, both classes (Test and FileHandling) are in same folder (src -> test -> groovy). I am not able to post the screenshot. Sry for the inconvenience caused. – user2956350 Nov 05 '13 at 17:40
0
  1. Import the FileHandling class with its package name, like

     import test.groovy.FileHandling.* 
    
  2. If FileHandling is not extending Page class of GEB then you have to instantiate the class as below.

     static def fileHandle = new FileHandling()
    

    And call the method inside FileHandling class using the above def fileHandle object,

      def cellArrayActual = fileHandle.returnExcelResults(path_act, "MEMBER_PREMIUM")
    
  3. If FileHandling is extending Page class of GEB

      class FileHandling extends Page{}
    

    then you have to use At checker before calling the method.

      when:
           go() // uses base url system property
           def path_act = "C:/Users/abc.xlsx"
           at FileHandling
           def cellArrayActual = FileHandling.returnExcelResults(path_act, "MEMBER_PREMIUM")
    
Nithya
  • 1
  • 1