groovy - Geb 自动化中未调用 env.groovy 的 Before() 方法

标签 groovy cucumber automated-tests geb

我正在运行 cucumber 测试,但收到错误

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?

最佳答案

确保您的 env.groovy 文件存在于步骤包内,并确保有测试运行程序

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


)

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

关于groovy - Geb 自动化中未调用 env.groovy 的 Before() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41400081/

相关文章:

cucumber - 为众多客户提供 Watir-Webdriver 现场覆盖的方法

javascript - 如何在预请求脚本中更改 Postman 环境?

unit-testing - drone.io headless Dart 测试

java - Selenium Chrome 驱动程序选择证书弹出确认不起作用

groovy - Jenkins Groovy : What triggered the build

java - @Path注释: abstract resource in dropwizard

types - 如何使用 JSoup 从网页获取资源类型?

android - 在 OS X Mavericks 上安装 Calabash-Android 会导致错误

database - 使用 Cucumber 和 databasecleaner 时,我可以在表中保留一个条目吗?

grails - Grails、Geb 和 PhantomJS 的功能测试不起作用