在geb自动化中不调用env.groovy的Before()方法

时间:2016-12-30 17:19:33

标签: groovy cucumber automated-tests geb

我正在进行黄瓜测试但是我收到了错误

Assertion failed: 
assert mApp != null
        |    |
       null false

at org.codehaus.groovy.runtime.InvokerHelper.assertFailed(InvokerHelper.java:398)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.assertFailed(ScriptBytecodeAdapter.java:648)
at Utils.TestFramework.getApp(TestFramework.groovy:30)
at Utils.TestFramework$getApp.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:112)
at Cucumber_Steps.Home$_run_closure1.doCall(Home.groovy:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:324)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:292)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1015)
at groovy.lang.Closure.call(Closure.java:423)
at cucumber.runtime.groovy.GroovyBackend.invoke(GroovyBackend.java:155)
at cucumber.runtime.groovy.GroovyHookDefinition$1.call(GroovyHookDefinition.java:46)
at cucumber.runtime.Timeout.timeout(Timeout.java:13)
at cucumber.runtime.groovy.GroovyHookDefinition.execute(GroovyHookDefinition.java:43)
at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:222)
at cucumber.runtime.Runtime.runHooks(Runtime.java:210)
at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:200)
at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:44)
at cucumber.runtime.junit.ExecutionUnitRunner.run(ExecutionUnitRunner.java:91)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.runtime.junit.ExamplesRunner.run(ExamplesRunner.java:59)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.runtime.junit.ScenarioOutlineRunner.run(ScenarioOutlineRunner.java:53)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:63)
at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:18)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:70)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:93)
at cucumber.api.junit.Cucumber.runChild(Cucumber.java:37)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at cucumber.api.junit.Cucumber.run(Cucumber.java:98). 

我创建了env.groovy如下:

import cucumber.api.Scenario
import cucumber.runtime.ScenarioImpl
import geb.Browser
import geb.binding.BindingUpdater
import io.appium.java_client.InteractsWithApps
import org.openqa.selenium.logging.LogEntries
import org.openqa.selenium.remote.RemoteLogs

import static cucumber.api.groovy.Hooks.*

BindingUpdater bindingUpdater
Browser theBrowser = null


Before(100) { Scenario scenario ->

(theBrowser?.driver as InteractsWithApps)?.resetApp()
sleep(5000) //give time for app to fully reset before starting test

theBrowser = new Browser()
bindingUpdater = new BindingUpdater(binding, theBrowser)
bindingUpdater.initialize()
TestFramework.init(theBrowser)
println("***In Init()")
}

After(100) { Scenario scenario ->
bindingUpdater?.remove()

// embed screenshot into cucumber report
if(theBrowser && scenario.failed) {

    RemoteLogs logs = theBrowser.driver.manage().logs()

    LogEntries logcat
    if (false && "logcat" in logs.availableLogTypes) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream()
        ObjectOutputStream oos = new ObjectOutputStream(bos)
        logs.get("logcat")?.each {
            oos.writeObject(it.toString() + '\n');
        }
        scenario.embed(bos.toByteArray(), "text/plain")
    }

    String failedStep = (scenario as ScenarioImpl).stepResults.findIndexOf { it.error }
    String reportLabel = "${scenario.name}-step-${failedStep}-${scenario.status}"
    theBrowser.report(reportLabel)
}
}

我的TestFramework.groovy包含:

public class TestFramework {

/**
 * The application under test.
 */
private static MyApplication mApp;

/**
 * Initialize the test framework.
 *
 * @param browser The application browser.
 */
public static void init(Browser browser) {
    mApp = new MyApplication(browser)
}

/**
 * Gets the application.
 *
 * @return The application.
 */
public static MyApplication getApp() {
    assert mApp != null
    return mApp;
  }
}

我在步骤文件中使用了getApp()方法:

MyApplication mApp
String testNote

Before { Scenario scenario ->
mApp = TestFramework.getApp()
}

Given(~/^app is in session$/){ ->
// nothing is in given
}

When(~/^the (.*?) is clicked$/){String team ->
mApp.home.selectTeam(getTeam(team))
}

我想知道为什么我会收到NullPointerException?

1 个答案:

答案 0 :(得分:3)

确保您的 env.groovy 文件存在于步骤包中,并确保为其提供测试运行

@RunWith(Cucumber)
@CucumberOptions(
        plugin = ['pretty'],
        features=["src/test/groovy/cucumber/features"],
        glue=["src/test/groovy/cucumber/steps"],
        tags="@manual"


)

如果它不适合你,请告诉我。