199 lines
4.8 KiB
JavaScript
199 lines
4.8 KiB
JavaScript
import http from 'apigo.cc/gojs/http'
|
||
import co from 'apigo.cc/gojs/console'
|
||
import u from 'apigo.cc/gojs/util'
|
||
import fs from 'apigo.cc/gojs/file'
|
||
|
||
let t1, t2
|
||
function printOK(...testResult) {
|
||
t2 = u.timestampMS()
|
||
co.info('. >> 成功', '耗时[' + (t2 - t1) + ']', !testResult ? '' : testResult.join(' '))
|
||
t1 = t2
|
||
}
|
||
|
||
co.log("开始测试...")
|
||
t1 = u.timestampMS()
|
||
|
||
co.log("启动Chrome浏览器...")
|
||
let ch = http.startChrome()
|
||
printOK("Chrome启动成功")
|
||
|
||
var page
|
||
|
||
co.log("打开z20首页...")
|
||
page = ch.open("https://z20.cc/")
|
||
printOK()
|
||
|
||
co.log("设置视口大小...")
|
||
page.setViewport(1280, 720)
|
||
printOK("1280×720")
|
||
|
||
co.log("获取当前URL...")
|
||
let url = page.url()
|
||
printOK(url)
|
||
|
||
co.log("获取页面标题...")
|
||
const title = page.title()
|
||
printOK(title.substring(0, 15) + (title.length > 15 ? "..." : ""))
|
||
|
||
co.log("导航到Apigo.cc首页...")
|
||
page.navigate("https://apigo.cc")
|
||
printOK()
|
||
|
||
co.log("获取当前URL...")
|
||
url = page.url()
|
||
printOK(url)
|
||
|
||
co.log("获取资源列表...")
|
||
let res = page.getResList()
|
||
printOK(`资源列表(${res.length})获取成功`)
|
||
|
||
co.log("获取资源列表(按类型script)...")
|
||
res = page.getResListByType("script")
|
||
printOK(`资源列表(${res.length})获取成功`)
|
||
|
||
co.log("获取资源列表(按MimeType)...")
|
||
res = page.getResListByMimeType("text/*")
|
||
printOK(`资源列表(${res.length})获取成功`)
|
||
|
||
co.log("获取资源内容...")
|
||
let content = page.getResContent(res[0].url)
|
||
printOK(`资源(${res[0].url})内容(${content.length}/${res[0].size})获取成功`)
|
||
|
||
co.log("执行后退操作...")
|
||
page.back()
|
||
printOK()
|
||
|
||
co.log("获取当前URL...")
|
||
url = page.url()
|
||
printOK(url)
|
||
|
||
co.log("执行前进操作...")
|
||
page.forward()
|
||
printOK()
|
||
|
||
co.log("获取当前URL...")
|
||
url = page.url()
|
||
printOK(url)
|
||
|
||
co.log("滚动页面...")
|
||
page.scrollTo(0, 500)
|
||
printOK()
|
||
|
||
co.log("查找Logo...")
|
||
const logo = page.find(".logo")
|
||
printOK("已找到")
|
||
|
||
co.log("检查Logo可见性...")
|
||
printOK(logo.isVisible())
|
||
|
||
co.log("获取Logo 尺寸...")
|
||
printOK(logo.getAttribute("width"), logo.getAttribute("height"))
|
||
|
||
co.log("获取Logo alt...")
|
||
printOK(logo.getAttribute("alt"))
|
||
|
||
co.log("获取Logo URL...")
|
||
printOK(logo.getAttribute("src"))
|
||
|
||
co.log("获取Logo位置信息...")
|
||
const rect = logo.getBoundingRect()
|
||
printOK(`X:${rect.x} Y:${rect.y} W:${rect.width} H:${rect.height}`)
|
||
|
||
co.log("导航到Apigo.cc探索页...")
|
||
// page = ch.open("https://apigo.cc/explore/repos")
|
||
page.navigate("https://apigo.cc/explore/repos")
|
||
printOK()
|
||
|
||
co.log("获取当前URL...")
|
||
url = page.url()
|
||
printOK(url)
|
||
|
||
co.log("查找搜索框...")
|
||
var searchInput = page.findN("input[type=search]")
|
||
if (!searchInput) {
|
||
return "搜索框不存在"
|
||
}
|
||
printOK("存在")
|
||
|
||
co.log("查找搜索框父...")
|
||
const parent = searchInput.parent()
|
||
printOK("存在", parent.getAttribute('class'))
|
||
|
||
co.log("查找搜索按钮...")
|
||
const searchBtn = parent.find("button")
|
||
printOK("存在")
|
||
printOK("存在", searchBtn.getAttribute('class') == searchBtn.getProperty('className') ? 'class检查通过' : 'class检查失败')
|
||
|
||
co.log("查找搜索按钮(findChild)...")
|
||
parent.findChild("button")
|
||
printOK("成功")
|
||
|
||
co.log("查找搜索按钮(findX)...")
|
||
parent.findX("./button")
|
||
printOK("成功")
|
||
|
||
co.log("查找表单...")
|
||
const form = searchInput.findParent('form')
|
||
printOK("存在", form.getAttribute('id'))
|
||
|
||
co.log("检查搜索框可交互性...")
|
||
printOK(searchInput.isInteractable())
|
||
|
||
co.log("设置搜索框值...")
|
||
searchInput.setValue("ag")
|
||
printOK("值已设置")
|
||
|
||
co.log("等待跳转搜索结果...")
|
||
page.waitPageLoad()
|
||
searchInput = page.find("input[type=search]")
|
||
printOK("完成", page.url())
|
||
|
||
co.log("获取搜索框值...")
|
||
printOK(searchInput.getValue())
|
||
|
||
co.log("查找所有结果链接...")
|
||
var list = page.findAll(".flex-list > .flex-item")
|
||
printOK(`找到 ${list.length} 个链接`)
|
||
|
||
co.log("查找第二个项目的仓库连接...")
|
||
const link = list[0].find("a:nth-child(2)")
|
||
printOK(link.getAttribute("href"))
|
||
|
||
co.log("计算按钮中心位置...")
|
||
const linkCenter = link.getCenter()
|
||
printOK(`X:${linkCenter.x.toFixed(1)} Y:${linkCenter.y.toFixed(1)}`)
|
||
|
||
co.log("移动鼠标到按钮中心...")
|
||
page.mouseMoveTo(linkCenter.x, linkCenter.y)
|
||
printOK()
|
||
|
||
co.log("按下鼠标左键...")
|
||
page.mouseClick()
|
||
printOK()
|
||
|
||
co.log("等待搜索结果稳定...")
|
||
page.waitPageLoad()
|
||
printOK()
|
||
|
||
co.log("获取当前URL...")
|
||
url = page.url()
|
||
printOK(url)
|
||
|
||
co.log("截取页面截图...")
|
||
const screenshot1 = page.screenshot()
|
||
printOK(`size:${screenshot1.length}`)
|
||
// fs.write('./screenshot1.png', screenshot1)
|
||
|
||
co.log("截取页面截图(全部)...")
|
||
const screenshot2 = page.screenshotFullPage()
|
||
printOK(`size:${screenshot2.length}`)
|
||
// fs.write('./screenshot2.png', screenshot2)
|
||
|
||
co.log("生成PDF...")
|
||
const pdf = page.pDF()
|
||
printOK(`size:${pdf.length}`)
|
||
// fs.write('./page.pdf', pdf)
|
||
|
||
co.log("所有测试步骤完成!")
|
||
|
||
return true |