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