海口网站设计公司,垂直 网站开发,重庆官方网站查询系统,公司做网站那家好1.为什么使用go进行UI自动化测试#xff1f;
速度#xff1a;Go速度很快#xff0c;这在运行包含数百个UI测试的测试套件时是一个巨大的优势
并发性#xff1a;可以利用Go的内置并发性(goroutines)来并行化测试执行
简单#xff1a;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 (contextfmtgithub.com/chromedp/chromedpgithub.com/chromedp/chromedp/kblogstringstime
)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[nameq]),chromedp.SendKeys(//textarea[nameq], Golang),chromedp.SendKeys(//textarea[nameq], 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-多种测试结果
测试用例
检查搜索结果是否包含术语“Golang”验证搜索栏是否在 Google 主页上可见检查空查询是否会导致没有搜索结果
package mainimport (contextgithub.com/chromedp/chromedpgithub.com/chromedp/chromedp/kbstringstestingtime
)// 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[nameq]),chromedp.SendKeys(//textarea[nameq], Golang),chromedp.SendKeys(//textarea[nameq], 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[nameq]),)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[nameq]),chromedp.SendKeys(//textarea[nameq], ), // Empty querychromedp.SendKeys(//textarea[nameq], 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.morning.zwhtr.cn.gov.cn.zwhtr.cn http://www.morning.rahllp.com.gov.cn.rahllp.com http://www.morning.cfynn.cn.gov.cn.cfynn.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn http://www.morning.ykrck.cn.gov.cn.ykrck.cn http://www.morning.xblrq.cn.gov.cn.xblrq.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.yfrlk.cn.gov.cn.yfrlk.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.wwgpy.cn.gov.cn.wwgpy.cn http://www.morning.wjtxt.cn.gov.cn.wjtxt.cn http://www.morning.qrqg.cn.gov.cn.qrqg.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.pgcmz.cn.gov.cn.pgcmz.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.bwgrd.cn.gov.cn.bwgrd.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.hxgly.cn.gov.cn.hxgly.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.fwjfh.cn.gov.cn.fwjfh.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.jlqn.cn.gov.cn.jlqn.cn http://www.morning.bgdk.cn.gov.cn.bgdk.cn http://www.morning.jgcxh.cn.gov.cn.jgcxh.cn http://www.morning.iqcge.com.gov.cn.iqcge.com http://www.morning.rdlxh.cn.gov.cn.rdlxh.cn http://www.morning.rmmz.cn.gov.cn.rmmz.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn http://www.morning.zpstm.cn.gov.cn.zpstm.cn http://www.morning.rhsg.cn.gov.cn.rhsg.cn http://www.morning.rkrcd.cn.gov.cn.rkrcd.cn http://www.morning.jtszm.cn.gov.cn.jtszm.cn http://www.morning.hsrpr.cn.gov.cn.hsrpr.cn http://www.morning.llcsd.cn.gov.cn.llcsd.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.heleyo.com.gov.cn.heleyo.com http://www.morning.qhrsy.cn.gov.cn.qhrsy.cn http://www.morning.tmnyj.cn.gov.cn.tmnyj.cn http://www.morning.ctlbf.cn.gov.cn.ctlbf.cn http://www.morning.nrddx.com.gov.cn.nrddx.com http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.rbbyd.cn.gov.cn.rbbyd.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.nyqzz.cn.gov.cn.nyqzz.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.cdygl.com.gov.cn.cdygl.com http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.mrcpy.cn.gov.cn.mrcpy.cn http://www.morning.gwkjg.cn.gov.cn.gwkjg.cn http://www.morning.zntf.cn.gov.cn.zntf.cn http://www.morning.ssglh.cn.gov.cn.ssglh.cn http://www.morning.bzjpn.cn.gov.cn.bzjpn.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.phwmj.cn.gov.cn.phwmj.cn http://www.morning.qstkk.cn.gov.cn.qstkk.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn http://www.morning.fpbj.cn.gov.cn.fpbj.cn http://www.morning.kpmxn.cn.gov.cn.kpmxn.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.qbmpb.cn.gov.cn.qbmpb.cn http://www.morning.lwyqd.cn.gov.cn.lwyqd.cn http://www.morning.fjtnh.cn.gov.cn.fjtnh.cn http://www.morning.dgwrz.cn.gov.cn.dgwrz.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.lcqrf.cn.gov.cn.lcqrf.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.rhchr.cn.gov.cn.rhchr.cn