0

I am getting Cannot invoke method propertyMissing() error while trying to take screenshots in Geb.

Following are the classes

MyLoginPage.groovy

package geb.pages

import geb.Page

class MyLoginPage extends Page {
    static url = "https://JSPNet.com/entry"
    static at = { title == "My Login" }

....
signIn { $("#btnEnter span")}

    def getScreenshots(){

        File scrFile = ((TakesScreenshot)DriverInstance).getScreenshotAs(OutputType.FILE)  ***** Error line ****
        FileUtils.copyFile(scrFile, new File("D:\\tmp\\screenshot.png"))
    }
}

I am getting this error in the above class in relating to DriverInstance

GebConfig.groovy

import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.firefox.FirefoxDriver


driver = {
    System.setProperty("webdriver.chrome.driver", "D:\\MyFolder\\selenium-2.32.0\\chromedriver.exe")
    def newdriver = new ChromeDriver()
    DriverInstance = newdriver
    return newdriver
}

environments {
    chrome { driver = { new ChromeDriver() } }
    firefox {driver = { new FirefoxDriver() }}
}

waiting {timeout = 5}

MyHomePage.groovy

And(~'^hit the Login button$') { ->

     page.signIn.click()

    MyLoginPage = new MyLoginPage()
    MyLoginPage.getScreenshots()

}

Error:

java.lang.NullPointerException: Cannot invoke method propertyMissing() on null object
                at org.codehaus.groovy.runtime.NullObject.invokeMethod(NullObject.java:77)
                at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:45)
                at 

org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:42)
 at geb.pages.MyLoginPage.getScreenshots(MyLoginPage.groovy:70)
                at geb.pages.MyLoginPage$getScreenshots.call(Unknown Source)
Shabar
  • 2,617
  • 11
  • 57
  • 98
  • And(~'^hit the Login button$') { -> Is the -> operator a typo? – twinj Aug 19 '14 at 08:07
  • It's an operator use in cucumber step definition file. – Shabar Aug 19 '14 at 08:11
  • Ahh ok. Unfortunately I don't know cucumber well at all. – twinj Aug 19 '14 at 08:14
  • Is MyLoginPage a variable and if so should it have the same name as the class. Specifically MyLoginPage = – twinj Aug 19 '14 at 08:15
  • No worries error is not relating to cucumber. To you question. I am trying to create an object of `MyLoginPage.groovy ` class and call that method `getScreenshots() ` there – Shabar Aug 19 '14 at 08:23
  • Is `page` null on the `page.signIn.click()` line – tim_yates Aug 19 '14 at 08:49
  • 2
    Cant you do it the gebish way in the click method? And(~'^hit the Login button$') { -> page.signIn.click(MyLoginPage) page.getScreenshots() } Put new lines in too. – twinj Aug 19 '14 at 09:35
  • `page.signIn.click ` method is there in `MyLoginPage.groovy ` I didn't add into the question. Because I thought its not relevant. Added now – Shabar Aug 19 '14 at 10:21
  • @twinj Thank you, As you specified, can access the method in gebish way. – Shabar Aug 19 '14 at 11:06

1 Answers1

1

It seems that DriverInstance is null. You should be able to access driver with driver property:

def getScreenshots(){

    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE)
    FileUtils.copyFile(scrFile, new File("D:\\tmp\\screenshot.png"))
}
Paweł Piecyk
  • 2,749
  • 15
  • 17
  • Perfect. BTW should I include this method in all Page objects groovy class on order to get screenshots Or is there an alternative way – Shabar Aug 19 '14 at 10:55
  • Further, Is returning part is necessary ? i.e. `return newdriver ` – Shabar Aug 19 '14 at 11:04
  • 1
    I'm not an expert in Geb so it should be probably better to do it gebish way as @twinj suggested. Regarding returning from this closure: yes, you need to return driver instance here. Take look at docs: http://www.gebish.org/manual/current/configuration.html#configuration – Paweł Piecyk Aug 19 '14 at 11:15