Skip to content

Instantly share code, notes, and snippets.

@nottyo
Last active April 14, 2022 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nottyo/d65a654ff288a148ace82ae1a4c24c8e to your computer and use it in GitHub Desktop.
Save nottyo/d65a654ff288a148ace82ae1a4c24c8e to your computer and use it in GitHub Desktop.
cypress - qrcode reader
import { BrowserMultiFormatReader } from '@zxing/browser';
const reader = new BrowserMultiFormatReader();
Cypress.Commands.add('readCode', { prevSubject: true }, (subject) => {
const img = subject[0];
const image = new Image();
image.width = img.width;
image.height = img.height;
image.src = img.src;
image.crossOrigin = 'Anonymous';
return reader.decodeFromImageElement(image);
});
describe('QR Code', () => {
it('can read barcode with zxing-js', () => {
cy.visit('./qrcode.html');
cy.get('[data-testid="barcode"]')
.readCode()
.should('have.property', 'text', 'https://medium.com/@nottyo');
});
it('can read qrcode with zxing-js', () => {
cy.visit('./qrcode.html');
cy.get('[data-testid="qr-code-img"]')
.readCode()
.should('have.property', 'text', 'https://www.cypress.io');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment