无法在Groovy中调用方法propertyMissing()

时间:2014-08-19 07:11:32

标签: groovy geb

我在尝试在Geb中截取屏幕时出现Cannot invoke method propertyMissing()错误。

以下是课程

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"))
    }
}

我在上述课程中遇到与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()

}

错误:

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)

1 个答案:

答案 0 :(得分:1)

似乎DriverInstance为空。您应该能够使用driver属性访问驱动程序:

def getScreenshots(){

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