java - GEB:驱动程序未设置为 Browser.driver
问题描述
我正在用 GEB 和 Spock 编写测试(我都是新手)。
驱动程序在 GebConfig 中声明(更新 - 添加了完整的配置文件):
import geb.report.ReportState
import geb.report.Reporter
import geb.report.ReportingListener
import io.github.bonigarcia.wdm.WebDriverManager
import io.qameta.allure.Allure
import org.openqa.selenium.Dimension
import org.openqa.selenium.Point
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions
import org.openqa.selenium.firefox.FirefoxProfile
import org.slf4j.LoggerFactory
import utils.Configuration
def logger = LoggerFactory.getLogger(this.class)
baseUrl = "${Configuration.getStringProperty("BASE_URL")}/${Configuration.getStringProperty("CONTEXT_PATH")}"
baseNavigatorWaiting = true
autoClearCookies = false
cacheDriver = false
reportsDir = 'build/test-reports'
driver = {
WebDriver dr
switch (Configuration.getStringProperty("BROWSER_NAME", "chrome").trim().toLowerCase()) {
case "firefox":
case "ff":
dr = new FirefoxDriver(setUpFirefoxOptions())
break
case "google chrome":
case "chrome":
default:
dr = new ChromeDriver(setUpGoogleChromeOptions())
}
if (Configuration.getBooleanProperty("SET_DRIVER_POSITION", false)) {
dr.manage().window().setPosition(new Point(
Configuration.getIntProperty("BROWSER_X_POS", 0),
Configuration.getIntProperty("BROWSER_Y_POS", 0)))
dr.manage().window().setSize(new Dimension(
Configuration.getIntProperty("BROWSER_WIDTH", 1600),
Configuration.getIntProperty("BROWSER_HEIGHT", 900)));
} else {
dr.manage().window().maximize()
}
return dr
}
static ChromeOptions setUpGoogleChromeOptions() {
WebDriverManager.chromedriver().setup()
ChromeOptions options = new ChromeOptions()
String args = Configuration.getStringProperty("BROWSER_ARGS")
if (args) {
Arrays.stream(args.split("\\s")).each { options.addArguments(it) }
}
return options
}
static FirefoxOptions setUpFirefoxOptions() {
WebDriverManager.firefoxdriver().setup()
FirefoxOptions options = new FirefoxOptions()
FirefoxProfile profile = new FirefoxProfile()
profile.setPreference("network.automatic-ntlm-auth.trusted-uris", "http://,https://")
options.setProfile(profile).setLegacy(false)
return options
}
reportingListener = new ReportingListener() {
void onReport(Reporter reporter, ReportState reportState, List<File> reportFiles) {
def fileGroups = reportFiles.groupBy { it.name.split("\\.")[-1] }
fileGroups['png']?.each {
Allure.addAttachment(it.name, "image/png", new FileInputStream(it), "png")
}
}
}
测试示例如下所示(BaseTest 代码添加如下):
class SimulationsRunningSpec extends BaseTest {
def "My great test"() {
println("test started")
setup:
to LoginPage
when:
println("when")
then:
println("then")
}
def cleanupSpec() {
browser.quit()
println "Clean up specification"
}
}
我得到以下日志序列:
test started
Created driver
when
then
Created driver
Clean up specification
所以驱动程序在to LoginPage
被调用时被创建。
问题:
它没有设置为浏览器驱动程序,所以当browser.quit()
被调用时,会创建一个新实例然后关闭(第一个仍然打开)。
问题:
如何将驱动程序正确设置为浏览器以关闭它然后通过
browser.quit()
?我是否正确假设如果我需要在setupSpec中创建驱动程序我可以简单地调用
to LoginPage
那里?或者在先决条件下初始化驱动程序的最佳方法是什么?
更新:
经过一些调试,我发现由于某种原因,浏览器 gecomesnull
并在cleanupSpec()
. Spec是否扩展自定义基类的Geb类并不重要。这重现了我的问题:
class TestSpec extends GebReportingSpec {
def setupSpec() {
to Page
println "setupSpec browser: $browser"
}
def setup(){
println "setup browser: $browser"
}
def "My first test"() {
println("test started")
when:
println ''
then:
println ''
}
def cleanup() {
println "cleanup browser: $browser"
}
def cleanupSpec() {
println "cleanupSpec browser: $browser"
}
}
这会产生以下输出:
setupSpec browser: geb.Browser@4beeb0e
setup browser: geb.Browser@4beeb0e
test started
cleanup browser: geb.Browser@4beeb0e
cleanupSpec browser: geb.Browser@5c73f672
最后两行显示 中的browser
对象与 中的cleanupSpec
已创建对象不同setupSpec
。
解决方案
我不确定,为什么浏览器在您的cleanupSpec
. 可能其他一些机制已经处理了它。
然而,您得到一个不同的实例cleanupSpec
的事实仅仅是因为它getBrowser
被实现为一个惰性吸气剂。如有必要,它会创建一个新实例,如您在代码中所见。
通常您不需要browser.quit
使用 Geb调用。Geb 处理得很好。
更新
GebSpec
以下是在and中发生的情况YourSpec
:
GebSpec.setupSpec
被触发⇒_browser
是null
YourSpec.setupSpec
被触发 ⇒_browser
仍然是null
,除非你在这里使用它GebSpec.setup
被触发⇒_browser
没有改变YourSpec.setup
被触发⇒_browser
可能会改变YouSpec
的第一个功能被触发⇒_browser
被使用,所以它null
不再是YourSpec.cleanup
被触发⇒_browser
没有改变GebSpec.cleanup
被触发⇒_browser
设置为null
!正如您在代码中看到的那样,resetBrowser
被称为 unlessYourSpec
is@Stepwise
并且设置_browser
为 null ,正如您在此处看到的那样。YourSpec.cleanupSpec
被触发 ⇒_browser
除非null
你使用它,所以它会被重新初始化GebSpec.cleanupSpec
被触发⇒_browser
仍然null
推荐阅读
- c# - 如何在高性能环境下生成网页图片?
- apache - 如何使 AWS 主机中的 docker 网络服务器在公共互联网上可用?
- sql - 对第一个查询中不匹配的数据运行第二个查询
- linux - 当值为 0 时存储 grep -c 输出时脚本终止
- wpf - wpf DelegateCommand 返回不触发
- javascript - Sublime 文本中的自定义选项卡名称(用于“索引”文件)
- mysql - Mysql'如果不存在则插入',防止'重叠'
- azure - Azure IoT Hub,通过代理上传文件?
- scala - 当 Task 从不同的词法范围引用时,Monix TaskLocal 似乎没有反映来自 bind() 调用的值
- c# - 根据条件更改列表框项的颜色