|
| 1 | +import { browser, expect } from '@wdio/globals' |
| 2 | +import { unlink } from 'node:fs/promises' |
| 3 | +import type { ImageCompareResult } from '@wdio/image-comparison-core' |
| 4 | + |
| 5 | +describe('@wdio/visual-service desktop performance', () => { |
| 6 | + // @TODO |
| 7 | + // @ts-ignore |
| 8 | + const browserName = `${browser.capabilities.browserName}-${browser.capabilities.browserVersion}` |
| 9 | + const iterations = 20 |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + await browser.url('') |
| 13 | + await $('.hero__title-logo').waitForDisplayed() |
| 14 | + }) |
| 15 | + |
| 16 | + // Chrome remembers the last position when the url is loaded again, this will reset it. |
| 17 | + afterEach(async () => await browser.execute('window.scrollTo(0, 0);', [])) |
| 18 | + |
| 19 | + it(`should compare an element successful with a baseline for '${browserName}' (performance test)`, async function() { |
| 20 | + const executionTimes: number[] = [] |
| 21 | + |
| 22 | + for (let i = 0; i < iterations; i++) { |
| 23 | + const startTime = Date.now() |
| 24 | + |
| 25 | + const result = await browser.checkElement(await $('.hero__title-logo'), 'wdioLogo', { |
| 26 | + removeElements: [await $('nav.navbar')], |
| 27 | + returnAllCompareData: true |
| 28 | + }) as ImageCompareResult |
| 29 | + |
| 30 | + // Delete the actual image after each iteration |
| 31 | + try { |
| 32 | + await unlink(result.folders.actual) |
| 33 | + } catch { |
| 34 | + // Ignore errors if file doesn't exist |
| 35 | + } |
| 36 | + |
| 37 | + // Assert the result |
| 38 | + expect(result.misMatchPercentage).toBeLessThanOrEqual(0) |
| 39 | + |
| 40 | + const endTime = Date.now() |
| 41 | + const executionTime = endTime - startTime |
| 42 | + executionTimes.push(executionTime) |
| 43 | + |
| 44 | + if (i === iterations - 1) { |
| 45 | + const averageTime = executionTimes.reduce((sum, time) => sum + time, 0) / executionTimes.length |
| 46 | + console.log(`[Element Snapshot] Average execution time over ${iterations} iterations: ${averageTime.toFixed(2)}ms`) |
| 47 | + } |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + it(`should compare a viewport screenshot successful with a baseline for '${browserName}' (performance test)`, async function() { |
| 52 | + const executionTimes: number[] = [] |
| 53 | + |
| 54 | + for (let i = 0; i < iterations; i++) { |
| 55 | + const startTime = Date.now() |
| 56 | + |
| 57 | + const result = await browser.checkScreen('viewportScreenshot', { |
| 58 | + returnAllCompareData: true |
| 59 | + }) as ImageCompareResult |
| 60 | + |
| 61 | + // Delete the actual image after each iteration |
| 62 | + try { |
| 63 | + await unlink(result.folders.actual) |
| 64 | + } catch { |
| 65 | + // Ignore errors if file doesn't exist |
| 66 | + } |
| 67 | + |
| 68 | + // Assert the result |
| 69 | + expect(result.misMatchPercentage).toBeLessThanOrEqual(0) |
| 70 | + |
| 71 | + const endTime = Date.now() |
| 72 | + const executionTime = endTime - startTime |
| 73 | + executionTimes.push(executionTime) |
| 74 | + |
| 75 | + if (i === iterations - 1) { |
| 76 | + const averageTime = executionTimes.reduce((sum, time) => sum + time, 0) / executionTimes.length |
| 77 | + console.log(`[Viewport Screenshot] Average execution time over ${iterations} iterations: ${averageTime.toFixed(2)}ms`) |
| 78 | + } |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + it(`should compare a full page screenshot successful with a baseline for '${browserName}' (performance test)`, async function () { |
| 83 | + const executionTimes: number[] = [] |
| 84 | + |
| 85 | + for (let i = 0; i < iterations; i++) { |
| 86 | + const startTime = Date.now() |
| 87 | + |
| 88 | + const result = await browser.checkFullPageScreen('fullPage', { |
| 89 | + fullPageScrollTimeout: 1500, |
| 90 | + hideAfterFirstScroll: [ |
| 91 | + await $('nav.navbar'), |
| 92 | + ], |
| 93 | + returnAllCompareData: true |
| 94 | + }) as ImageCompareResult |
| 95 | + |
| 96 | + // Delete the actual image after each iteration |
| 97 | + try { |
| 98 | + await unlink(result.folders.actual) |
| 99 | + } catch { |
| 100 | + // Ignore errors if file doesn't exist |
| 101 | + } |
| 102 | + |
| 103 | + // Assert the result |
| 104 | + expect(result.misMatchPercentage).toBeLessThanOrEqual(0) |
| 105 | + |
| 106 | + const endTime = Date.now() |
| 107 | + const executionTime = endTime - startTime |
| 108 | + executionTimes.push(executionTime) |
| 109 | + |
| 110 | + if (i === iterations - 1) { |
| 111 | + const averageTime = executionTimes.reduce((sum, time) => sum + time, 0) / executionTimes.length |
| 112 | + console.log(`[Full Page Screenshot] Average execution time over ${iterations} iterations: ${averageTime.toFixed(2)}ms`) |
| 113 | + } |
| 114 | + } |
| 115 | + }) |
| 116 | + |
| 117 | + it(`should compare a tabbable screenshot successful with a baseline for '${browserName}' (performance test)`, async function() { |
| 118 | + const executionTimes: number[] = [] |
| 119 | + |
| 120 | + for (let i = 0; i < iterations; i++) { |
| 121 | + const startTime = Date.now() |
| 122 | + |
| 123 | + const result = await browser.checkTabbablePage('tabbable', { |
| 124 | + hideAfterFirstScroll: [ |
| 125 | + await $('nav.navbar'), |
| 126 | + ], |
| 127 | + returnAllCompareData: true |
| 128 | + }) as ImageCompareResult |
| 129 | + |
| 130 | + // Delete the actual image after each iteration |
| 131 | + try { |
| 132 | + await unlink(result.folders.actual) |
| 133 | + } catch { |
| 134 | + // Ignore errors if file doesn't exist |
| 135 | + } |
| 136 | + |
| 137 | + // Assert the result |
| 138 | + expect(result.misMatchPercentage).toBeLessThanOrEqual(0) |
| 139 | + |
| 140 | + const endTime = Date.now() |
| 141 | + const executionTime = endTime - startTime |
| 142 | + executionTimes.push(executionTime) |
| 143 | + |
| 144 | + if (i === iterations - 1) { |
| 145 | + const averageTime = executionTimes.reduce((sum, time) => sum + time, 0) / executionTimes.length |
| 146 | + console.log(`[Tabbable Screenshot] Average execution time over ${iterations} iterations: ${averageTime.toFixed(2)}ms`) |
| 147 | + } |
| 148 | + } |
| 149 | + }) |
| 150 | +}) |
0 commit comments