Before() method of env.groovy is not called in geb automation

656 views Asked by At

I was running a cucumber test but I am getting an error

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). 

I have created env.groovy as follows:

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

and my TestFramework.groovy contains:

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;
  }
}

I used getApp() method in step file:

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

I want to know why I am getting NullPointerException?

1

There are 1 answers

0
Jitesh Mohite On BEST ANSWER

make sure your env.groovy file present inside steps package and make sure to have test runner for it

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


)

please let me know if it don't work for you.