【问题标题】:GEB: driver is not set as Browser.driverGEB:驱动程序未设置为 Browser.driver
【发布时间】: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() 时,会创建一个新实例然后关闭(第一个实例仍然打开)。

问题

  1. 如何正确设置浏览器驱动,然后通过browser.quit()关闭?

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


    【解决方案1】:

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

    事实上,您在 cleanupSpec 中获得了不同的实例,这仅仅是因为 getBrowser 被实现为惰性 getter。如有必要,它会创建一个新实例,如您在 code 中所见。

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

    更新

    这是GebSpecYourSpec 中发生的情况:

    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! 正如您在code 中看到的那样,resetBrowser 被调用,除非YourSpec@Stepwise 并且设置@ 987654351@ 为空,如您所见 here
    8. YourSpec.cleanupSpec 被触发 ⇒ _browsernull 除非你使用它,所以它会被重新初始化
    9. GebSpec.cleanupSpec 被触发⇒ _browser 仍然是 null

    【讨论】:

    • 不,它没有。至少,就我而言,如果我不调用 quit() 方法,它不会在测试后关闭浏览器。我实际上删除了除了在此处列出的配置和示例中的初始化之外的所有代码,但浏览器仍然没有自动关闭,并且在调用 quit() 后创建了新实例。
    • @BohdanN 你能添加你的 GebConfig.groovy 吗?
    • 请尝试将quitCachedDriverOnShutdown = true添加到您的GebConfig.groovy
    • 是的,试过了。这是相同的行为 - 在清理规范中打开了新浏览器。如果有人可以尝试我在问题的更新部分中列出的代码示例,我将不胜感激。将完整的 geb 配置添加到问题中
    • @BohdanN 我更新了我的答案来解释你的观察。
    【解决方案2】:

    您看到浏览器重新初始化以进行清理,这似乎很奇怪,您所显示的内容是正确的。

    对于第 1 点:您在 gebconfig 中正确设置。

    对于第 2 点:您不需要在 setupSpec() 中初始化浏览器,只需要配置条目。

    一旦运行所有测试,浏览器应该会自动关闭,除非您已将以下内容添加到您的 gebconfig 并设置为 false:

    quitCachedDriverOnShutdown = false 
    

    setupSpec() 在规范中的所有方法都运行后调用。您向我们展示的代码是您规范中唯一的代码吗?您的规范是扩展 GebSpec 或 GebReportingSpec 还是自定义基类?

    我唯一能想到的另一件事是,您在该规范中有 2 个测试,因此您看到“已创建驱动程序”两次,并且在运行所有测试后调用 cleanUpSpec(),因此您会看到在结尾。如果您调用了 cleanup(),它将在每个测试之间运行。

    【讨论】:

    • 我添加了更多描述。我使用自定义 BaseTest 扩展 GebReportingSpec
    猜你喜欢
    • 2015-01-11
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 2020-05-19
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    相关资源
    最近更新 更多