|
| 1 | +// E2E tests for example pages |
| 2 | +describe('Example Pages E2E', () => { |
| 3 | + describe('live_w_locator.html', () => { |
| 4 | + beforeEach(() => { |
| 5 | + cy.visit('http://localhost:8080/live_w_locator.html'); |
| 6 | + }); |
| 7 | + |
| 8 | + afterEach(() => { |
| 9 | + cy.window().then((win: any) => { |
| 10 | + if (win.Quagga && win.Quagga.stop) { |
| 11 | + const stopPromise = win.Quagga.stop(); |
| 12 | + if (stopPromise && typeof stopPromise.then === 'function') { |
| 13 | + return cy.wrap(stopPromise); |
| 14 | + } |
| 15 | + } |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + after(() => { |
| 20 | + cy.window().then((win: any) => { |
| 21 | + if (win.Quagga && win.Quagga.stop) { |
| 22 | + const stopPromise = win.Quagga.stop(); |
| 23 | + if (stopPromise && typeof stopPromise.then === 'function') { |
| 24 | + return cy.wrap(stopPromise); |
| 25 | + } |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should load the page successfully', () => { |
| 31 | + cy.contains('h1', 'QuaggaJS').should('be.visible'); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should have a camera dropdown', () => { |
| 35 | + cy.get('select#deviceSelection').should('exist'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should have a locate checkbox', () => { |
| 39 | + cy.get('input[name="locate"]').should('exist'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should have canvas elements', () => { |
| 43 | + // Canvas is created by Quagga inside #interactive div |
| 44 | + cy.get('#interactive').should('exist'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should have Quagga available on window', () => { |
| 48 | + cy.window().its('Quagga').should('exist'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should toggle locate checkbox', () => { |
| 52 | + cy.get('input[name="locate"]').uncheck().should('not.be.checked'); |
| 53 | + cy.get('input[name="locate"]').check().should('be.checked'); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should populate camera dropdown with devices', () => { |
| 57 | + // Wait for devices to be enumerated and dropdown populated |
| 58 | + cy.get('select#deviceSelection option', { timeout: 10000 }) |
| 59 | + .should('have.length.at.least', 1); |
| 60 | + }); |
| 61 | + |
| 62 | + |
| 63 | + it('provides scanning area box when locate=false', () => { |
| 64 | + // Wait for Quagga to be initialized and processing frames before toggling locate |
| 65 | + cy.window().then((win: any) => { |
| 66 | + expect(win.Quagga).to.exist; |
| 67 | + return new Cypress.Promise((resolve) => { |
| 68 | + const timeout = setTimeout(() => resolve(null), 1500); |
| 69 | + win.Quagga.onProcessed(() => { |
| 70 | + clearTimeout(timeout); |
| 71 | + resolve(null); |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + // Now toggle locate off and wait for reinit |
| 77 | + cy.get('input[name="locate"]').uncheck().should('not.be.checked').trigger('change'); |
| 78 | + cy.wait(400); // Allow time for debounced reinit (250ms) + init completion |
| 79 | + |
| 80 | + cy.window().then((win: any) => { |
| 81 | + expect(win.Quagga).to.exist; |
| 82 | + expect(win.Quagga.onProcessed).to.be.a('function'); |
| 83 | + |
| 84 | + const p = new Cypress.Promise((resolve) => { |
| 85 | + const timeout = setTimeout(() => resolve(null), 2000); |
| 86 | + |
| 87 | + win.Quagga.onProcessed(function (result: any) { |
| 88 | + if (result && (result.box || (result.boxes && result.boxes.length > 0))) { |
| 89 | + clearTimeout(timeout); |
| 90 | + resolve(result); |
| 91 | + } |
| 92 | + }); |
| 93 | + }); |
| 94 | + return cy.wrap(p, { timeout: 3000 }); |
| 95 | + }).then((result: any) => { |
| 96 | + expect(result, 'Should receive result with box or boxes').to.exist; |
| 97 | + // When locate=false, should have either result.box or result.boxes |
| 98 | + const hasBox = result.box && Array.isArray(result.box) && result.box.length === 4; |
| 99 | + const hasBoxes = result.boxes && Array.isArray(result.boxes) && result.boxes.length > 0; |
| 100 | + expect(hasBox || hasBoxes, 'Should have either result.box or result.boxes with scanning area').to.be.true; |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it('continues processing frames after resolution change', () => { |
| 105 | + // Select a different resolution option dynamically (choose last option if available) |
| 106 | + cy.contains('label', 'Resolution (width)') |
| 107 | + .find('select[name="input-stream_constraints"]').then($sel => { |
| 108 | + const current = ($sel[0] as HTMLSelectElement).value; |
| 109 | + const options = Array.from(($sel[0] as HTMLSelectElement).options).map(o => o.value); |
| 110 | + const alt = options.reverse().find(v => v !== current) || current; |
| 111 | + cy.wrap($sel).select(alt); |
| 112 | + }); |
| 113 | + // Verify frames continue after resolution change |
| 114 | + cy.window().then((win: any) => { |
| 115 | + expect(win.Quagga).to.exist; |
| 116 | + expect(win.Quagga.onProcessed).to.be.a('function'); |
| 117 | + let count = 0; |
| 118 | + const p = new Cypress.Promise((resolve) => { |
| 119 | + const timeout = setTimeout(() => resolve(null), 3000); |
| 120 | + win.Quagga.onProcessed(() => { |
| 121 | + count++; |
| 122 | + if (count >= 3) { |
| 123 | + clearTimeout(timeout); |
| 124 | + resolve(null); |
| 125 | + } |
| 126 | + }); |
| 127 | + }); |
| 128 | + return cy.wrap(p, { timeout: 3500 }).then(() => { |
| 129 | + expect(count).to.be.greaterThan(0); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('continues processing frames after patch size change', () => { |
| 135 | + cy.contains('label', 'Patch-Size') |
| 136 | + .find('select[name="locator_patch-size"]').select('large'); |
| 137 | + cy.window().then((win: any) => { |
| 138 | + expect(win.Quagga).to.exist; |
| 139 | + expect(win.Quagga.onProcessed).to.be.a('function'); |
| 140 | + let count = 0; |
| 141 | + const p = new Cypress.Promise((resolve) => { |
| 142 | + win.Quagga.onProcessed(() => { count++; if (count >= 3) resolve(null); }); |
| 143 | + }); |
| 144 | + return cy.wrap(p, { timeout: 3000 }).then(() => { |
| 145 | + expect(count).to.be.greaterThan(0); |
| 146 | + }); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('hides zoom and torch controls when unsupported', () => { |
| 151 | + // Labels wrap the inputs and are set to display:none when capability missing |
| 152 | + cy.get('label:has(select[name="settings_zoom"])').should('have.css', 'display', 'none'); |
| 153 | + cy.get('label:has(input[name="settings_torch"])').should('have.css', 'display', 'none'); |
| 154 | + }); |
| 155 | + }); |
| 156 | +}); |
0 commit comments