无法从规范文件中的 GebConfig.groovy 文件读取系统变量集
Not able to read the system variable set from GebConfig.groovy file in spec file
我正在使用 geb spock。我正在尝试从 GebConfig 文件中读取系统变量,但是它 returns 是空值。
下面是我的 GebConfig.groovy 文件。
def final DEFAULT_BROWSER = "chrome"
def final DEFAULT_LANGUAGE = "nl" //"en" or "nl"
def browser = System.getProperty("geb.env")
//Default browser
if (!correctBrowser(browser)) {
browser = DEFAULT_BROWSER
}
def envLang = System.getProperty("geb.env.lang")
//Default language
if (!correctLanguage(envLang)) {
envLang = DEFAULT_LANGUAGE
}
System.setProperty("geb.env.lang", envLang)
System.setProperty("geb.env", browser)
environments {
driver = { getDriver(browser, envLang) }
}
下面是我的规范文件,我试图在其中获取变量中的语言值。
@Stepwise
class TC001_SMO_Scenario_Spec extends GebReportingSpec {
@Shared
def lang = System.getProperty("geb.env.lang")
def "Step 1: Perform Login"() {
when: "Load File"
to WUPage
then: " File loaded successfully"
println " Getting data from Geb Config File: " + lang
}
}
你能帮我怎么做吗,因为这对我来说非常重要,可以访问它并将其存储在变量中。谢谢
问题出在您的 @Shared
变量上。 Geb manual 表示:
(...) declare a @Shared field. Again it’s best to initialize the field right at the point of declaration. (Semantically, this is equivalent to initializing the field at the very beginning of the setupSpec()
method.)
事情是,setupSpec()
运行s before GebConfig
被评估。如果将此添加到 GebConfig
:
的末尾,您可以看到它
println "Finished evaluating GebConfig"
然后 运行 这个 Geb 规范(我已经将变量赋值包装到闭包中并添加了 print 语句,然后我正在评估闭包以使赋值工作):
package de.scrum_master.Whosebug
import geb.spock.GebReportingSpec
import spock.lang.Shared
class GebConfigIT extends GebReportingSpec {
@Shared
def sharedLang = {
println "Initialising sharedLang"
System.getProperty("geb.env.lang")
}()
def normalLang = {
println "Initialising normalLang"
System.getProperty("geb.env.lang")
}()
def setup() {
println "sharedLang = $sharedLang"
println "normalLang = $normalLang"
}
def foo() {
expect:
!sharedLang
normalLang
}
def bar() {
expect:
!sharedLang
normalLang
}
}
Initialising sharedLang
Finished evaluating GebConfig
Initialising normalLang
sharedLang = null
normalLang = nl
Initialising normalLang
Finished evaluating GebConfig
sharedLang = null
normalLang = nl
你能看出 sharedLang
是如何在一开始就只初始化一次的吗,甚至在 GebConfig
有机会开始之前?
底线:只需从您的代码中删除 @Shared
。无论如何,它被大多数人过度使用,他们认为他们以这种方式节省时间以获得廉价资源并篡改他们干净的夹具设置。 @Shared
确实只适用于非常昂贵的资源和非常特殊的情况。
我正在使用 geb spock。我正在尝试从 GebConfig 文件中读取系统变量,但是它 returns 是空值。
下面是我的 GebConfig.groovy 文件。
def final DEFAULT_BROWSER = "chrome"
def final DEFAULT_LANGUAGE = "nl" //"en" or "nl"
def browser = System.getProperty("geb.env")
//Default browser
if (!correctBrowser(browser)) {
browser = DEFAULT_BROWSER
}
def envLang = System.getProperty("geb.env.lang")
//Default language
if (!correctLanguage(envLang)) {
envLang = DEFAULT_LANGUAGE
}
System.setProperty("geb.env.lang", envLang)
System.setProperty("geb.env", browser)
environments {
driver = { getDriver(browser, envLang) }
}
下面是我的规范文件,我试图在其中获取变量中的语言值。
@Stepwise
class TC001_SMO_Scenario_Spec extends GebReportingSpec {
@Shared
def lang = System.getProperty("geb.env.lang")
def "Step 1: Perform Login"() {
when: "Load File"
to WUPage
then: " File loaded successfully"
println " Getting data from Geb Config File: " + lang
}
}
你能帮我怎么做吗,因为这对我来说非常重要,可以访问它并将其存储在变量中。谢谢
问题出在您的 @Shared
变量上。 Geb manual 表示:
(...) declare a @Shared field. Again it’s best to initialize the field right at the point of declaration. (Semantically, this is equivalent to initializing the field at the very beginning of the
setupSpec()
method.)
事情是,setupSpec()
运行s before GebConfig
被评估。如果将此添加到 GebConfig
:
println "Finished evaluating GebConfig"
然后 运行 这个 Geb 规范(我已经将变量赋值包装到闭包中并添加了 print 语句,然后我正在评估闭包以使赋值工作):
package de.scrum_master.Whosebug
import geb.spock.GebReportingSpec
import spock.lang.Shared
class GebConfigIT extends GebReportingSpec {
@Shared
def sharedLang = {
println "Initialising sharedLang"
System.getProperty("geb.env.lang")
}()
def normalLang = {
println "Initialising normalLang"
System.getProperty("geb.env.lang")
}()
def setup() {
println "sharedLang = $sharedLang"
println "normalLang = $normalLang"
}
def foo() {
expect:
!sharedLang
normalLang
}
def bar() {
expect:
!sharedLang
normalLang
}
}
Initialising sharedLang
Finished evaluating GebConfig
Initialising normalLang
sharedLang = null
normalLang = nl
Initialising normalLang
Finished evaluating GebConfig
sharedLang = null
normalLang = nl
你能看出 sharedLang
是如何在一开始就只初始化一次的吗,甚至在 GebConfig
有机会开始之前?
底线:只需从您的代码中删除 @Shared
。无论如何,它被大多数人过度使用,他们认为他们以这种方式节省时间以获得廉价资源并篡改他们干净的夹具设置。 @Shared
确实只适用于非常昂贵的资源和非常特殊的情况。