当前位置: 首页 > news >正文

兰州网站程序建设/免费下载优化大师

兰州网站程序建设,免费下载优化大师,普通的宣传网站用什么做,dedecms建站教程1.为什么使用go进行UI自动化测试? 速度:Go速度很快,这在运行包含数百个UI测试的测试套件时是一个巨大的优势 并发性:可以利用Go的内置并发性(goroutines)来并行化测试执行 简单:Go的简约语法允许您编写可读且可维护…

1.为什么使用go进行UI自动化测试?

速度:Go速度很快,这在运行包含数百个UI测试的测试套件时是一个巨大的优势

并发性:可以利用Go的内置并发性(goroutines)来并行化测试执行

简单:Go的简约语法允许您编写可读且可维护的测试脚本

ChromeDP:一个无头Chrome/Chromium库,允许直接从Go控制浏览器

2.什么是chromedp?

ChromeDP 是一个 Go 库,允许开发者通过 Chrome DevTools 协议控制 Chrome/Chromium。借助 ChromeDP,您可以与网页交互、模拟用户输入、浏览浏览器以及提取内容进行验证。它既可以在无头模式(没有浏览器UI)下工作,也可以在有头模式(有可见浏览器)下工作。

3.设置环境

安装go

brew install go

安装ChromeDP

go get -u github.com/chromedp/chromedp

4.编写代码

示例测试

打开GoLand,新建main.go文件

main.go-核心测试

这段代码将会打开Chrome,导航到Google搜索页,搜索"Golang",并验证Golang是否出现在搜索结果中

package mainimport ("context""fmt""github.com/chromedp/chromedp""github.com/chromedp/chromedp/kb""log""strings""time"
)func main() {opts := chromedp.DefaultExecAllocatorOptions[:]opts = append(opts,// Disable headless modechromedp.Flag("headless", false),// Enable GPU (optional)chromedp.Flag("disable-gpu", false),// Start with a maximized windowchromedp.Flag("start-maximized", true),)allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)defer cancel()ctx, cancel := chromedp.NewContext(allocCtx)defer cancel()ctx, cancel = context.WithTimeout(ctx, 15*time.Second)defer cancel()var result stringerr := chromedp.Run(ctx,chromedp.Navigate("https://www.google.com"),chromedp.WaitVisible(`//textarea[@name="q"]`),chromedp.SendKeys(`//textarea[@name="q"]`, "Golang"),chromedp.SendKeys(`//textarea[@name="q"]`, kb.Enter),chromedp.WaitVisible(`#search`),chromedp.Text(`#search`, &result),)if err != nil {log.Fatal(err)}if strings.Contains(result, "Golang") {fmt.Println("Test Passed: 'Golang' found in search results")} else {fmt.Println("Test Failed: 'Golang' not found in search results")}
}

Go测试框架编写多个UI测试

新建main_test.go文件

main_test.go-多种测试结果

测试用例:

  1. 检查搜索结果是否包含术语“Golang”
  2. 验证搜索栏是否在 Google 主页上可见
  3. 检查空查询是否会导致没有搜索结果
package mainimport ("context""github.com/chromedp/chromedp""github.com/chromedp/chromedp/kb""strings""testing""time"
)// checks that the search results contain "Golang"
func TestGoogleSearch_Golang(t *testing.T) {opts := chromedp.DefaultExecAllocatorOptions[:]opts = append(opts,chromedp.Flag("headless", false),chromedp.Flag("disable-gpu", false),chromedp.Flag("start-maximized", true),)allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)defer cancel()ctx, cancel := chromedp.NewContext(allocCtx)defer cancel()ctx, cancel = context.WithTimeout(ctx, 15*time.Second)defer cancel()var result stringerr := chromedp.Run(ctx,chromedp.Navigate("https://www.google.com"),chromedp.WaitVisible(`//textarea[@name="q"]`),chromedp.SendKeys(`//textarea[@name="q"]`, "Golang"),chromedp.SendKeys(`//textarea[@name="q"]`, kb.Enter),chromedp.WaitVisible(`#search`),chromedp.Text(`#search`, &result),)if err != nil {t.Fatalf("Test Failed: %v", err)}if strings.Contains(result, "Golang") {t.Log("Test Passed: 'Golang' found in search results")} else {t.Errorf("Test Failed: 'Golang' not found in search results")}
}// checks that the search bar is visible
func TestGoogleSearch_SearchBarVisible(t *testing.T) {opts := chromedp.DefaultExecAllocatorOptions[:]opts = append(opts,chromedp.Flag("headless", false),chromedp.Flag("disable-gpu", false),chromedp.Flag("start-maximized", true),)allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)defer cancel()ctx, cancel := chromedp.NewContext(allocCtx)defer cancel()ctx, cancel = context.WithTimeout(ctx, 15*time.Second)defer cancel()err := chromedp.Run(ctx,chromedp.Navigate("https://www.google.com"),chromedp.WaitVisible(`//textarea[@name="q"]`),)if err != nil {t.Errorf("Test Failed: Search bar not visible - %v", err)} else {t.Log("Test Passed: Search bar is visible")}
}// checks if there are results when the search query is empty
func TestGoogleSearch_EmptyQuery(t *testing.T) {opts := chromedp.DefaultExecAllocatorOptions[:]opts = append(opts,chromedp.Flag("headless", false),chromedp.Flag("disable-gpu", false),chromedp.Flag("start-maximized", true),)allocCtx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)defer cancel()ctx, cancel := chromedp.NewContext(allocCtx)defer cancel()ctx, cancel = context.WithTimeout(ctx, 15*time.Second)defer cancel()var result stringerr := chromedp.Run(ctx,chromedp.Navigate("https://www.google.com"),chromedp.WaitVisible(`//textarea[@name="q"]`),chromedp.SendKeys(`//textarea[@name="q"]`, ""), // Empty querychromedp.SendKeys(`//textarea[@name="q"]`, kb.Enter),chromedp.WaitVisible(`#search`, chromedp.ByID),chromedp.Text(`#search`, &result),)if err != nil {t.Fatalf("Test Failed: %v", err)}if result == "" {t.Log("Test Passed: No search results for empty query")} else {t.Errorf("Test Failed: Unexpected results for empty query")}
}

执行测试

将main.go和main_test.go放在同一目录,然后执行命令

go test -v

测试结果

http://www.jmfq.cn/news/5309605.html

相关文章:

  • 殷氏科技网站建设工作室/百度贴吧的互动社区
  • 邯郸网站建设服务报价/排超联赛积分榜
  • 用织梦建设网站/百度极速版客服电话
  • 网站建设 中国移动/怎么在百度上打广告
  • 中小企业加盟网站建设/互联网营销师培训大纲
  • 电子商务网站建设与管理案例/链接
  • 网站建设与网页设计毕业论文/电商推广方案
  • 加强公司网站平台建设的意义/seo引擎搜索网址
  • 建材网站建设 南宁/北京网站制作400办理多少钱
  • 柳州市诚信体系建设网站/优化网站制作方法大全
  • 慕枫网站建设/百度指数官方
  • 网站建设预算和维护/网站建设开发公司
  • 如何选择商城网站建设/成品人和精品人的区别在哪
  • 成都网站建设好的公司/web网页
  • 有口碑的江苏网站建设/百度网址大全首页
  • 沧州网站建设设计/免费制作网站的平台
  • net网站建设多少前/需要多少钱
  • 定制营销型网站建设/百度云app下载安装
  • 建设门户网站请示/人力资源培训网
  • 论坛网站建设视频/百度ai搜索引擎
  • 安徽省建设工程资料上传网站/网站seo查询
  • net的网站建设/下载优化大师并安装
  • 武汉网站建设优化/百度收录查询工具官网
  • 上街免费网站建设/东莞做网络推广的公司
  • 网站建设规范布局有几部分/seo网站诊断报告
  • 安康网站建设小程序/百度allin 人工智能
  • 建设个人网站需要多少钱/大一html网页制作作业简单
  • 怎样做手机网站建设/互联网推广怎么找渠道
  • 黄骅广信建设集团网站/网站优化的方式有哪些
  • 网站建设与设计论文/百度首页