@dfinity/internet-identity-playwright
Version:
A Playwright library to simplify the integration of Internet Identity authentication in E2E tests.
2 lines (1 loc) • 2.04 kB
JavaScript
import{test as I}from"@playwright/test";import{expect as a}from"@playwright/test";var n=class{page;browser;context;constructor({page:e,context:i,browser:t}){this.page=e,this.browser=t,this.context=i}waitReady=async({url:e,canisterId:i,timeout:t=6e4})=>{let{host:o,protocol:c}=new URL(e),d=i!==void 0?this.browser.browserType().name()==="webkit"?`${c}//${o}?canisterId=${i}`:`${c}//${i}.${o.replace("127.0.0.1","localhost")}`:e,y=async()=>{try{return(await this.page.goto(d,{waitUntil:"domcontentloaded"}))?.ok()??!1}catch{return!1}},l=500,u=async({count:r})=>{if(await y())return"ready";let w=r-1;return w===0?"timeout":(await new Promise(m=>setTimeout(m,l)),await u({count:w}))},p=await u({count:t/l});a(p).toEqual("ready"),await a(this.page).toHaveTitle("Internet Identity");let g=this.page.locator("#registerButton");a(g).not.toBeNull()};signInWithNewIdentity=async e=>{let i=this.context.waitForEvent("page");await this.page.locator(e?.selector??"[data-tid=login-button]").click();let t=await i;await a(t).toHaveTitle("Internet Identity"),await t.locator("#registerButton").click(),e?.createPasskey===!0&&await t.locator("[data-action=construct-identity]").click(),e?.captcha===!0&&(await t.locator("input#captchaInput").fill("a",{timeout:1e4}),await t.locator("#confirmRegisterButton").click());let o=await t.locator("#userNumber").textContent();return a(o).not.toBeNull(),await t.locator("#displayUserContinue").click(),await t.waitForEvent("close"),a(t.isClosed()).toBe(!0),parseInt(o)};signInWithIdentity=async({selector:e,identity:i})=>{let t=this.context.waitForEvent("page");await this.page.locator(e??"[data-tid=login-button]").click();let o=await t;await a(o).toHaveTitle("Internet Identity"),await o.locator(`[data-anchor-id='${i}']`).click(),await o.waitForEvent("close"),a(o.isClosed()).toBe(!0)}};import{expect as $}from"@playwright/test";var k=I.extend({iiPage:async({page:s,browser:e,context:i},t)=>{let o=new n({page:s,context:i,browser:e});await t(o)}});export{n as InternetIdentityPage,$ as expect,k as testWithII};