Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | import { ElementHandle, expect, Page, test } from '@playwright/test' type Element = ElementHandle<HTMLElement | SVGElement> type Node = Element type Connection = Element type Side = 'right' | 'left' type HandlerPosition = 'corner' | 'center' type Selector = Element export async function getGraphView(container: Element) { const area = await container.$('> div') Iif (!area) throw new Error('area') const nodes = (): Promise<Node[]> => area.$$(`[data-testid="node"]`) const connections = (): Promise<Connection[]> => area.$$(`[data-testid="connection"]`) return { area, nodes, connections, async findNodes(title: string) { const list: Node[] = [] for (const node of await nodes()) { const titleElement = await node.$(`[data-testid="title"]:text-is("${title}")`) Iif (titleElement) list.push(node) } return list } } } export async function getBackgroundColor(node: Node) { const color = await node.evaluate(el => { const styles = window.getComputedStyle(el) return styles.getPropertyValue('background-color') }) Iif (!color) throw new Error('color') return color } export async function getInput(node: Node, controlKey: string) { const el = await node.$(`[data-testid="control-${controlKey}"] input`) Iif (!el) throw new Error(`cannot find control's "${controlKey}" input`) return el } export async function setInputValue(page: Page, node: Node, controlKey: string, value: string) { const el = await getInput(node, controlKey) await el.fill(value) await page.keyboard.press('Tab') } export async function boundingBox(node: Node) { const box = await node.boundingBox() Iif (!box) throw new Error('box') return box } export function toRect(box: { x: number, y: number, width: number, height: number }) { return { left: box.x, right: box.x + box.width, top: box.y, bottom: box.y + box.height } } export function takeBeforeEach(path: string, timeoutBefore: number, timeoutAfter: number) { let container!: Element const headlessQuery = path.includes('?') ? '&headless=true' : '?headless=true' test.beforeEach(async ({ page }) => { await page.goto(`http://localhost:3000/${path}${headlessQuery}`) await page.waitForSelector('.rete', { timeout: 6000 }) const _container = await page.$('.rete') expect(_container).toBeTruthy() Iif (!_container) throw new Error('cannot find .rete element') container = _container await page.waitForTimeout(timeoutBefore) await page.evaluate(() => { window.scrollTo(0, document.body.scrollHeight) }) await page.waitForTimeout(timeoutAfter) }) return { getContainer() { return container } } } export async function pickNode(page: Page, node: Selector) { const beforeBox = await boundingBox(node) const pickOffset = { x: 20, y: 20 } await page.mouse.move(beforeBox.x + pickOffset.x, beforeBox.y + pickOffset.y) await page.mouse.down({ button: 'left' }) await page.mouse.up({ button: 'left' }) } export async function clickCenter(page: Page, selector: Selector | string, button: Side = 'left') { const element = typeof selector === 'string' ? await page.$(selector) : selector Iif (!element) throw new Error('cannot find element') const beforeBox = await boundingBox(element) const pickOffset = { x: beforeBox.width / 2, y: beforeBox.height / 2 } await page.mouse.move(beforeBox.x + pickOffset.x, beforeBox.y + pickOffset.y) await page.mouse.down({ button }) await page.mouse.up({ button }) } export async function move(page: Page, node: Selector, dx: number, dy: number, handlerPosition: HandlerPosition = 'corner', options?: { down?: () => Promise<void> up?: () => Promise<void> }) { const beforeBox = await boundingBox(node) const pickOffset = handlerPosition === 'corner' ? { x: 20, y: 20 } : { x: beforeBox.width / 2, y: beforeBox.height / 2 } await page.mouse.move(beforeBox.x + pickOffset.x, beforeBox.y + pickOffset.y) if (options?.down) { await options.down() } else { await page.mouse.down({ button: 'left' }) } await page.mouse.move(beforeBox.x + pickOffset.x + dx, beforeBox.y + pickOffset.y + dy) if (options?.up) { await options.up() } else { await page.mouse.up({ button: 'left' }) } const afterBox = await boundingBox(node) return { before: beforeBox, after: afterBox } } type Rect = { left: number right: number top: number bottom: number } export async function getPositions(page: Page, selector: string) { const list = await page.$$(selector) const positions = await Promise.all(list.map(boundingBox)) const left = Math.min(...positions.map(({ x }) => x)) const right = Math.max(...positions.map(({ x, width }) => x + width)) const top = Math.min(...positions.map(({ y }) => y)) const bottom = Math.max(...positions.map(({ y, height }) => y + height)) return { positions, left, right, top, bottom, width: right - left, height: bottom - top } } export function isInside(inner: Rect, outer: Rect) { return ( inner.left >= outer.left && inner.top >= outer.top && inner.right <= outer.right && inner.bottom <= outer.bottom ) } export function isOutside(inner: Rect, outer: Rect) { return ( inner.left >= outer.right || inner.top >= outer.bottom || inner.right <= outer.left || inner.bottom <= outer.top ) } |