使用Golang通过Selenium服务新方法调用Edge浏览器及配置Capabilities和Service选项

使用Golang通过Selenium服务新方法调用Edge浏览器及配置Capabilities和Service选项 我目前正在使用:

github.com/cucumber/gherkin-go/v19 v19.0.3

GitHub - cucumber/godog: Cucumber for golang v0.12.4

GitHub - tebeka/selenium: Selenium/Webdriver client for Go v0.9.9

我希望能够使用 Selenium GoDog 框架通过 Microsoft Edge 浏览器自动化我的测试。有人能提供关于如何使用新的 Selenium 服务方法调用 Edge 浏览器,以及如何设置功能(capabilities)和服务选项的建议吗?

Edge 浏览器版本:106.0.1370.47 - 在 Windows X64 上

从以下链接下载了 Edge 驱动程序:106 支持版本

Microsoft Edge WebDriver - Microsoft Edge Developer

Microsoft Edge WebDriver - Microsoft Edge Developer

通过使用 Microsoft Edge WebDriver 自动化测试您在 Microsoft Edge 中的网站,来完善您的开发周期。


更多关于使用Golang通过Selenium服务新方法调用Edge浏览器及配置Capabilities和Service选项的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于使用Golang通过Selenium服务新方法调用Edge浏览器及配置Capabilities和Service选项的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


以下是使用Go通过Selenium服务新方法调用Edge浏览器并配置Capabilities和Service选项的示例代码:

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/tebeka/selenium"
)

func main() {
    // 1. 设置Edge WebDriver路径
    const (
        edgeDriverPath = "C:\\path\\to\\msedgedriver.exe"
        port           = 9515
    )

    // 2. 配置Edge浏览器选项
    caps := selenium.Capabilities{
        "browserName": "MicrosoftEdge",
        "ms:edgeOptions": map[string]interface{}{
            "args": []string{
                "--start-maximized",
                "--disable-infobars",
                "--disable-extensions",
                "--disable-gpu",
                "--no-sandbox",
            },
        },
    }

    // 3. 创建并配置Service选项
    service, err := selenium.NewEdgeDriverService(edgeDriverPath, port)
    if err != nil {
        log.Fatalf("创建Edge驱动服务失败: %v", err)
    }
    defer service.Stop()

    // 4. 启动服务
    if err := service.Start(); err != nil {
        log.Fatalf("启动Edge驱动服务失败: %v", err)
    }

    // 5. 连接到WebDriver
    wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d", port))
    if err != nil {
        log.Fatalf("连接到WebDriver失败: %v", err)
    }
    defer wd.Quit()

    // 6. 执行测试操作
    err = wd.Get("https://www.example.com")
    if err != nil {
        log.Fatalf("导航到页面失败: %v", err)
    }

    // 等待页面加载
    time.Sleep(2 * time.Second)

    // 获取页面标题
    title, err := wd.Title()
    if err != nil {
        log.Fatalf("获取页面标题失败: %v", err)
    }
    fmt.Printf("页面标题: %s\n", title)

    // 7. 与GoDog框架集成示例
    // 可以在GoDog步骤定义中使用wd对象进行浏览器操作
}

// GoDog步骤定义示例
func iNavigateToHomePage() error {
    // 这里可以使用全局的wd对象或通过依赖注入的方式
    return wd.Get("https://www.example.com")
}

对于更复杂的Capabilities配置,可以使用以下扩展配置:

// 高级Capabilities配置
caps := selenium.Capabilities{
    "browserName": "MicrosoftEdge",
    "ms:edgeOptions": map[string]interface{}{
        "args": []string{
            "--headless",                    // 无头模式
            "--window-size=1920,1080",       // 窗口大小
            "--disable-dev-shm-usage",       // 禁用共享内存
            "--remote-debugging-port=9222",  // 远程调试端口
        },
        "prefs": map[string]interface{}{
            "download.default_directory": "C:\\Downloads",
            "profile.default_content_setting_values.notifications": 2,
        },
        "excludeSwitches": []string{"enable-automation"},
    },
    "ms:loggingPrefs": map[string]string{
        "browser": "ALL",
        "driver":  "ALL",
    },
}

// 如果需要特定版本的Edge浏览器
caps["ms:edgeOptions"].(map[string]interface{})["binary"] = "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe"

在GoDog测试套件中集成的完整示例:

package features

import (
    "context"
    "fmt"
    "testing"

    "github.com/cucumber/godog"
    "github.com/tebeka/selenium"
)

var wd selenium.WebDriver

func TestMain(m *testing.M) {
    // 初始化Selenium WebDriver
    initSelenium()
    
    // 运行GoDog测试
    status := godog.TestSuite{
        Name: "Edge Browser Tests",
        ScenarioInitializer: InitializeScenario,
        Options: &godog.Options{
            Format:   "pretty",
            Paths:    []string{"features"},
            TestingT: &testing.T{},
        },
    }.Run()
    
    // 清理资源
    if wd != nil {
        wd.Quit()
    }
    
    if status != 0 {
        t.Fail()
    }
}

func initSelenium() {
    caps := selenium.Capabilities{
        "browserName": "MicrosoftEdge",
        "ms:edgeOptions": map[string]interface{}{
            "args": []string{"--start-maximized"},
        },
    }
    
    var err error
    wd, err = selenium.NewRemote(caps, "http://localhost:9515")
    if err != nil {
        panic(fmt.Sprintf("初始化WebDriver失败: %v", err))
    }
}

func InitializeScenario(ctx *godog.ScenarioContext) {
    ctx.Step(`^我打开 "([^"]*)" 网站$`, iOpenWebsite)
    ctx.Step(`^页面标题应该包含 "([^"]*)"$`, pageTitleShouldContain)
}

func iOpenWebsite(url string) error {
    return wd.Get(url)
}

func pageTitleShouldContain(expectedText string) error {
    title, err := wd.Title()
    if err != nil {
        return err
    }
    
    if !strings.Contains(title, expectedText) {
        return fmt.Errorf("页面标题 '%s' 不包含 '%s'", title, expectedText)
    }
    return nil
}

确保Edge WebDriver版本与Edge浏览器版本匹配,并正确设置系统PATH或指定完整的驱动路径。

回到顶部