SuperAPI AI 工具集

geb-skill

测试与质量

查看项目

更新于 10-15 15:18

技能介绍

用途

Geb 是一个基于 Groovy 的浏览器自动化测试框架。本技能(geb-skill)用于生成 Geb 浏览器自动化测试代码,并与 Spock 测试框架集成。当用户提到 "Geb"、"Groovy" 等关键词时,可使用该技能。

主要能力

  • 使用 Groovy 编写 Geb 浏览器自动化测试,并与 Spock 集成。
  • 采用类似 jQuery 的内容 DSL 和页面对象(Page Object)模式。
  • 支持在 TestMu AI 云平台上执行测试。

使用方式

基础测试(Spock)

import geb.spock.GebSpec

class LoginSpec extends GebSpec {
    def "login with valid credentials"() {
        when:
        to LoginPage
        emailInput.value("user@test.com")
        passwordInput.value("password123")
        loginButton.click()

        then:
        at DashboardPage
        welcomeMessage.text().contains("Welcome")
    }

    def "login shows error for invalid credentials"() {
        when:
        to LoginPage
        emailInput.value("wrong@test.com")
        passwordInput.value("wrong")
        loginButton.click()

        then:
        at LoginPage
        errorMessage.displayed
        errorMessage.text().contains("Invalid")
    }
}

页面对象(Page Objects)

class LoginPage extends geb.Page {
    static url = "/login"
    static at = { title == "Login" }
    static content = {
        emailInput { $("#email") }
        passwordInput { $("#password") }
        loginButton { $("button[type='submit']") }
        errorMessage(required: false) { $(".error") }
    }
}

class DashboardPage extends geb.Page {
    static url = "/dashboard"
    static at = { $(".dashboard").displayed }
    static content = {
        welcomeMessage { $(".welcome") }
        userName { $(".user-name") }
    }
}

Navigator API

$("css-selector")
$("div", class: "active")
$("input", name: "email")
$("div.items li", 0)        // First match
$("div.items li").size()     // Count

// Actions
element.click()
element.value("text")
element << "text"            // Append text
element.text()
element.displayed
element.@href                // Attribute

云执行

driver = new RemoteWebDriver(new URL(gridUrl), caps)

安装配置

使用 Gradle,依赖 geb-spock 和 selenium-support。

在 TestMu AI 上云执行

设置环境变量:LT_USERNAME、LT_ACCESS_KEY。

// GebConfig.groovy
environments {
    lambdatest {
        driver = {
            def ltOptions = [
                user: System.getenv("LT_USERNAME"),
                accessKey: System.getenv("LT_ACCESS_KEY"),
                build: "Geb Build",
                name: "Geb Test",
                platformName: "Windows 11",
                video: true,
                console: true,
                network: true,
            ]
            def caps = new ChromeOptions()
            caps.setCapability("LT:Options", ltOptions)
            new RemoteWebDriver(
                new URL("https://hub.lambdatest.com/wd/hub"), caps)
        }
    }
}

运行:./gradlew test -Dgeb.env=lambdatest

运行:./gradlew test

深入模式

参见 reference/playbook.md 获取生产级模式:

  • §1 项目搭建:build.gradle、GebConfig.groovy、environments、等待配置
  • §2 页面对象:内容 DSL、at 检查、可复用组件的模块
  • §3 Spec 测试:Spock 集成、使用 @Unroll 的数据驱动、@Stepwise 流程
  • §4 等待与异步:等待预设、JavaScript 交互、alert/confirm
  • §5 高级模式:文件上传/下载、窗口、frame、自定义扩展
  • §6 API 测试:使用 Groovy JsonSlurper 的 REST API 规格
  • §7 CI/CD 集成:使用无头 Chrome 的 GitHub Actions、Gradle 缓存
  • §8 调试表:12 个常见问题及其原因与修复
  • §9 最佳实践:14 项 Geb 测试检查清单

适用场景

适用于需要使用 Groovy 编写浏览器自动化测试、并希望与 Spock 集成、采用页面对象模式组织测试代码的场景,也支持在 TestMu AI 云平台上执行测试。

限制

关于收费、价格、平台支持范围等具体信息,资料中暂未提供。