首页 > 解决方案 > 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()被调用时,会创建一个新实例然后关闭(第一个仍然打开)。

问题

  1. 如何将驱动程序正确设置为浏览器以关闭它然后通过 browser.quit()

  2. 我是否正确假设如果我需要在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

标签: javagroovyautomated-testsspockgeb

解决方案


我不确定,为什么浏览器在您的cleanupSpec. 可能其他一些机制已经处理了它。

然而,您得到一个不同的实例cleanupSpec的事实仅仅是因为它getBrowser被实现为一个惰性吸气剂。如有必要,它会创建一个新实例,如您在代码中所见。

通常您不需要browser.quit使用 Geb调用。Geb 处理得很好。

更新

GebSpec以下是在and中发生的情况YourSpec

  1. GebSpec.setupSpec被触发⇒_browsernull
  2. YourSpec.setupSpec被触发 ⇒_browser仍然是null,除非你在这里使用它
  3. GebSpec.setup被触发⇒_browser没有改变
  4. YourSpec.setup被触发⇒_browser可能会改变
  5. YouSpec的第一个功能被触发⇒_browser被使用,所以它null不再是
  6. YourSpec.cleanup被触发⇒_browser没有改变
  7. GebSpec.cleanup被触发⇒_browser设置为null正如您在代码中看到的那样,resetBrowser被称为 unless YourSpecis@Stepwise并且设置_browser为 null ,正如您在此处看到的那样。
  8. YourSpec.cleanupSpec被触发 ⇒_browser除非null 你使用它,所以它会被重新初始化
  9. GebSpec.cleanupSpec被触发⇒_browser仍然null

推荐阅读