【发布时间】:2019-04-10 20:01:32
【问题描述】:
我正在使用 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那里?或者在先决条件下初始化驱动程序的最佳方法是什么?
更新:
经过一些调试,我发现由于某种原因,浏览器 gecomes null 并在 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
最后两行显示cleanupSpec中的browser对象与setupSpec中创建的对象不同。
【问题讨论】:
标签: java groovy automated-tests spock geb