// Run against a loopback visual host rendering the real Account/TwoFactor view // with a synthetic pending challenge. No real user, TOTP or database is needed. const {chromium}=require('playwright'); const assert=require('node:assert/strict'); const fs=require('node:fs'); const base=process.env.OTP_VISUAL_URL||'http://127.0.0.1:5282'; const output=process.env.OTP_SCREENSHOTS; if(!/^http:\/\/127\.0\.0\.1:\d+$/.test(base))throw Error('Loopback visual fixture required'); (async()=>{const browser=await chromium.launch();try{ for(const width of [1440,390,320]){ const page=await browser.newPage({viewport:{width,height:950}}); await page.goto(base+'/Account/TwoFactor'); const digits=page.locator('[data-two-factor-digit]');assert.equal(await digits.count(),6); const geometry=await digits.evaluateAll(xs=>xs.map(x=>{const r=x.getBoundingClientRect();return{x:r.x,y:r.y,w:r.width,h:r.height,align:getComputedStyle(x).textAlign}})); assert(geometry.every(x=>Math.abs(x.y-geometry[0].y)<.1&&Math.abs(x.w-geometry[0].w)<.1&&Math.abs(x.w-x.h)<.1&&x.align==='center')); assert(geometry[0].w<=56&&geometry[0].w>=24); const gaps=geometry.slice(1).map((x,i)=>x.x-geometry[i].x-geometry[i].w);assert(gaps.every(x=>x>0&&Math.abs(x-gaps[0])<.1)); assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false); const action=await page.locator('.two-factor-actions').boundingBox();assert(action.y-geometry[0].y-geometry[0].h>=20); for(let i=0;i<6;i++){ assert.equal(await digits.nth(i).getAttribute('aria-label'),`Digit ${i+1} of 6`); assert.equal(await digits.nth(i).getAttribute('inputmode'),'numeric');assert.equal(await digits.nth(i).getAttribute('maxlength'),'1'); assert.equal(await digits.nth(i).getAttribute('autocomplete'),'one-time-code');assert.equal(await digits.nth(i).getAttribute('pattern'),'[0-9]*'); } const focus=await digits.first().evaluate(x=>{const s=getComputedStyle(x);return s.outlineStyle!=='none'||s.boxShadow!=='none'});assert(focus); await page.evaluate(()=>{window.otpSubmissions=[];document.querySelector('form[data-two-factor-form]').addEventListener('submit',e=>{e.preventDefault();window.otpSubmissions.push(document.querySelector('[data-two-factor-code]').value)});}); await digits.first().press('a');assert.equal(await digits.first().inputValue(),'');assert(await digits.first().evaluate(x=>x===document.activeElement)); for(let i=0;i<6;i++){await digits.nth(i).press(String(i+1));if(i<5)assert(await digits.nth(i+1).evaluate(x=>x===document.activeElement));} assert.equal(await page.locator('[data-two-factor-code]').inputValue(),'123456');assert.deepEqual(await page.evaluate(()=>window.otpSubmissions),['123456']); if(output)await page.screenshot({path:`${output}/after-${width}.png`,fullPage:true}); assert.equal(await page.locator('form[data-two-factor-form]').getAttribute('method'),'post');assert.equal(await page.locator('input[name=__RequestVerificationToken]').count(),1); await page.reload();await page.evaluate(()=>{window.otpSubmissions=[];document.querySelector('[data-two-factor-form]').addEventListener('submit',e=>{e.preventDefault();window.otpSubmissions.push(document.querySelector('[data-two-factor-code]').value)});}); await digits.first().evaluate(x=>{const d=new DataTransfer();d.setData('text','12 34-56');x.dispatchEvent(new ClipboardEvent('paste',{bubbles:true,cancelable:true,clipboardData:d}));}); assert.deepEqual(await digits.evaluateAll(xs=>xs.map(x=>x.value)),['1','2','3','4','5','6']);assert.deepEqual(await page.evaluate(()=>window.otpSubmissions),['123456']); await page.reload();await digits.first().press('1');await digits.nth(1).press('2');await digits.nth(2).press('Backspace'); assert(await digits.nth(1).evaluate(x=>x===document.activeElement));assert.deepEqual((await digits.evaluateAll(xs=>xs.map(x=>x.value))).slice(0,3),['1','','']); await digits.nth(1).press('Tab');assert(await digits.nth(2).evaluate(x=>x===document.activeElement));await digits.nth(2).press('Shift+Tab');assert(await digits.nth(1).evaluate(x=>x===document.activeElement)); await digits.nth(1).press('ArrowLeft');await digits.nth(1).press('ArrowRight');assert.deepEqual((await digits.evaluateAll(xs=>xs.map(x=>x.value))).slice(0,3),['1','','']); assert.equal(await page.getByRole('link',{name:'Use a recovery code.'}).getAttribute('href'),'/Account/TwoFactorRecovery?returnUrl=%2F'); // Empty Code uses the existing server-side required validation, never TOTP. await page.reload();const response=page.waitForResponse(r=>r.url().endsWith('/Account/TwoFactor')&&r.request().method()==='POST'); await page.getByRole('button',{name:'Continue',exact:true}).click();assert.equal((await response).status(),200);await page.waitForLoadState(); assert(await page.getByText('Enter the code from your authenticator app.',{exact:true}).isVisible());assert.equal(await page.locator('[data-two-factor-digit]').count(),6); await page.close(); } console.log('PASS Chromium OTP: desktop/390/320 single row, equal squares/gaps, centred text, focus, no overflow, spacing, numeric input, advancement, paste, backspace, tab/arrows, accessibility, auto-POST payload/antiforgery and real required validation.'); }finally{await browser.close();}})().catch(e=>{console.error(e);process.exit(1)});