Add UI tests
This commit is contained in:
29
src/spec/util/expandTopic.ts
Normal file
29
src/spec/util/expandTopic.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { Browser } from 'webdriverio'
|
||||
import { clickOn } from './'
|
||||
|
||||
export async function expandTopic(path: string, browser: Browser<void>) {
|
||||
const originalTopics = path.split('/')
|
||||
let topics = path.split('/')
|
||||
while (topics.length > 0 && !await topicMatches(topics, browser)) {
|
||||
topics = topics.slice(0, topics.length - 1)
|
||||
}
|
||||
if (topics.length === 0) {
|
||||
throw Error('could not expand topics, no match found')
|
||||
}
|
||||
|
||||
while (topics.length <= originalTopics.length) {
|
||||
const match = await browser.$(topicSelector(topics))
|
||||
await clickOn(match, browser)
|
||||
topics.push(originalTopics[topics.length])
|
||||
}
|
||||
}
|
||||
|
||||
async function topicMatches(topics: string[], browser: Browser<void>) {
|
||||
const result = await browser.$(topicSelector(topics))
|
||||
return result.isExisting()
|
||||
}
|
||||
|
||||
function topicSelector(topics: string[]) {
|
||||
const suffix = topics.map(topic => `*[contains(text(), "${topic}")]`).join('/../../..//')
|
||||
return `//${suffix}`
|
||||
}
|
||||
109
src/spec/util/index.ts
Normal file
109
src/spec/util/index.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { Element, Browser } from 'webdriverio'
|
||||
export { expandTopic } from './expandTopic'
|
||||
|
||||
export function sleep(ms: number) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms)
|
||||
})
|
||||
}
|
||||
|
||||
export function writeText(text: string, to: Browser<void>) {
|
||||
text.split('').forEach(async (c) => {
|
||||
await to.keys([c])
|
||||
await sleep(50)
|
||||
})
|
||||
}
|
||||
|
||||
export async function moveToCenterOfElement(element: Element<void>, browser: Browser<void>) {
|
||||
const { x, y } = await element.getLocation()
|
||||
const { width, height } = await element.getSize()
|
||||
|
||||
const js = `{
|
||||
const targetX = ${x + width / 2}
|
||||
const targetY = ${y + height / 2}
|
||||
const duration = 500
|
||||
|
||||
const maxStepSize = 10
|
||||
const e = document.getElementById("bier")
|
||||
const top = parseFloat(e.style.top)
|
||||
const left = parseFloat(e.style.left)
|
||||
const deltaY = targetY - top
|
||||
const deltaX = targetX - left
|
||||
|
||||
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY)
|
||||
const steps = Math.ceil(distance / maxStepSize)
|
||||
|
||||
const stepX = deltaX / steps
|
||||
const stepY = deltaY / steps
|
||||
let currentStep = 0
|
||||
function getCloser() {
|
||||
e.style.left = String(left + (stepX * currentStep)) + 5 + 'px'
|
||||
e.style.top = String(top + (stepY * currentStep)) + 5 + 'px'
|
||||
if (currentStep < steps) {
|
||||
setTimeout(() => {
|
||||
currentStep += 1
|
||||
getCloser()
|
||||
}, duration/steps)
|
||||
}
|
||||
}
|
||||
getCloser()
|
||||
}`
|
||||
await browser.execute(js)
|
||||
await sleep(550)
|
||||
await element.moveTo()
|
||||
}
|
||||
|
||||
export async function clickOn(element: Element<void>, browser: Browser<void>, clicks = 1) {
|
||||
await moveToCenterOfElement(element, browser)
|
||||
for (let i = 0; i < clicks; i += 1) {
|
||||
await element.click()
|
||||
await sleep(50)
|
||||
}
|
||||
}
|
||||
|
||||
export async function createFakeMousePointer(browser: Browser<void>) {
|
||||
const addCursorImage = 'const i=document.createElement("img");'
|
||||
+ 'i.src="../cursor.png";'
|
||||
+ 'i.width="32";'
|
||||
+ 'i.height="32";'
|
||||
+ 'i.id="bier";'
|
||||
+ 'i.style="position: fixed; z-index:10000000; filter: invert(100%);left: 0px; top: 0px;";'
|
||||
+ 'document.body.appendChild(i)'
|
||||
|
||||
await browser.execute(addCursorImage)
|
||||
|
||||
const onMouseMove = `document.onmousemove = (event) => {
|
||||
const e = document.getElementById('bier')
|
||||
e.style.left = (event.pageX+1) + 'px'
|
||||
e.style.top = event.pageY + 'px'
|
||||
}`
|
||||
await browser.execute(onMouseMove)
|
||||
}
|
||||
|
||||
export async function showText(text: string, duration: number = 0, browser: Browser<void>) {
|
||||
const js = `
|
||||
let previousDiv = document.getElementById('tests-text-overlay')
|
||||
previousDiv && previousDiv.remove()
|
||||
let div = document.createElement('div')
|
||||
div.id = "tests-text-overlay"
|
||||
div.style = "background-color: rgba(0, 0, 0, 0.8);position: fixed;left: 5vw;z-index: 1000000;margin: 30vw auto 50vw;border-radius: 16px;right: 5vw;bottom: -65vh;"
|
||||
let div2 = document.createElement('div')
|
||||
div2.style = "text-align: center;font-size: 4em;color: white;"
|
||||
div2.innerHTML = "${text}"
|
||||
div.appendChild(div2)
|
||||
document.body.appendChild(div)
|
||||
if (${duration} > 0) {
|
||||
setTimeout(() => div.remove(), ${duration})
|
||||
}
|
||||
`
|
||||
browser.execute(js)
|
||||
}
|
||||
|
||||
export async function hideText(browser: Browser<void>) {
|
||||
const js = `
|
||||
let previousDiv = document.getElementById('tests-text-overlay')
|
||||
previousDiv && previousDiv.remove()
|
||||
`
|
||||
browser.execute(js)
|
||||
await sleep(600)
|
||||
}
|
||||
Reference in New Issue
Block a user