@clerk/testing
Version:
Utilities to help you create E2E test suites for apps using Clerk
2 lines • 16.9 kB
JavaScript
;var d=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var q=Object.getOwnPropertyNames;var G=Object.prototype.hasOwnProperty;var H=(a,e)=>{for(var r in e)d(a,r,{get:e[r],enumerable:!0})},J=(a,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of q(e))!G.call(a,o)&&o!==r&&d(a,o,{get:()=>e[o],enumerable:!(t=W(e,o))||t.enumerable});return a};var Y=a=>J(d({},"__esModule",{value:!0}),a);var Z={};H(Z,{createAppPageObject:()=>m,createPageObjects:()=>_});module.exports=Y(Z);var s=({page:a})=>{let e={continue:()=>a.getByRole("button",{name:"Continue",exact:!0}).click(),setEmailAddress:r=>e.getEmailAddressInput().fill(r),setPassword:r=>e.getPasswordInput().fill(r),setPasswordConfirmation:r=>a.locator("input[name=confirmPassword]").fill(r),enterOtpCode:async r=>{await a.getByRole("textbox",{name:/digit 1/i}).click(),await a.keyboard.type(r,{delay:100})},enterTestOtpCode:async()=>e.enterOtpCode("424242"),fillTestOtpCode:async r=>a.getByRole("textbox",{name:r}).fill("424242"),getIdentifierInput:()=>a.locator("input[name=identifier]"),getEmailAddressInput:()=>a.locator("input[name=emailAddress]"),getPhoneNumberInput:()=>a.locator("input[name=phoneNumber]"),getUsernameInput:()=>a.locator("input[name=username]"),getPasswordInput:()=>a.locator("input[name=password]"),getLegalAccepted:()=>a.locator("input[name=legalAccepted]"),getFirstNameInput:()=>a.locator("input[name=firstName]"),getLastNameInput:()=>a.locator("input[name=lastName]"),waitForSession:async()=>a.waitForFunction(()=>!!window.Clerk?.session)};return e};var h=a=>{let{page:e}=a,r={never:"Never","1d":"1 Day","7d":"7 Days","30d":"30 Days","60d":"60 Days","90d":"90 Days","180d":"180 Days","1y":"1 Year"};return{...s(a),waitForMounted:()=>e.waitForSelector(".cl-apiKeys-root",{state:"attached"}),clickAddButton:()=>e.getByText(/Add new key/i).click(),waitForFormOpened:()=>e.waitForSelector(".cl-apiKeysCreateForm",{state:"attached"}),waitForFormClosed:()=>e.waitForSelector(".cl-apiKeysCreateForm",{state:"detached"}),waitForRevokeModalOpened:()=>e.waitForSelector(".cl-apiKeysRevokeModal",{state:"attached"}),waitForRevokeModalClosed:()=>e.waitForSelector(".cl-apiKeysRevokeModal",{state:"detached"}),typeName:o=>e.getByLabel(/Secret key name/i).fill(o),typeDescription:o=>e.getByLabel(/Description/i).fill(o),selectExpiration:async o=>(await e.getByRole("button",{name:/Select date/i}).click(),e.getByText(r[o??"never"],{exact:!0}).click()),clickSaveButton:()=>e.getByText(/Create key/i).click(),typeRevokeConfirmation:o=>e.getByLabel(/Type "Revoke" to confirm/i).fill(o),clickConfirmRevokeButton:()=>e.getByText(/Revoke key/i).click()}};var b="__clerk_testing_token";var P="The Clerk Frontend API URL is required to bypass bot protection. Make sure the clerkSetup function is called during your global setup before setupClerkTestingToken is called.";var p=async({context:a,options:e,page:r})=>{let t=a??r?.context();if(!t)throw new Error("Either context or page must be provided to setup testing token");let o=e?.frontendApiUrl||process.env.CLERK_FAPI;if(!o)throw new Error(P);let n=`https://${o}/v1/**/*`;await t.route(n,async i=>{let l=new URL(i.request().url()),c=process.env.CLERK_TESTING_TOKEN;c&&l.searchParams.set(b,c);try{let u=await i.fetch({url:l.toString()}),g=await u.json();g?.response?.captcha_bypass===!1&&(g.response.captcha_bypass=!0),g?.client?.captcha_bypass===!1&&(g.client.captcha_bypass=!0),await i.fulfill({response:u,json:g})}catch{await i.continue({url:l.toString()}).catch(console.error)}})};var m=(a,e)=>{let{page:r,useTestingToken:t=!0}=a,o=Object.create(r);return Object.assign(o,{goToAppHome:async()=>{if(!e.baseURL)throw new Error("Attempted to call method requiring baseURL, but baseURL was not provided to createPageObjects.");try{t&&await p({page:r}),await r.goto(e.baseURL)}catch{}},goToRelative:async(i,l={})=>{if(!e.baseURL)throw new Error("Attempted to call method requiring baseURL, but baseURL was not provided to createPageObjects.");let c;try{r.url().includes("about:blank")?c=new URL(i,e.baseURL):c=new URL(i,r.url())}catch{c=new URL(i,e.baseURL)}return l.searchParams&&(c.search=l.searchParams.toString()),t&&await p({page:r}),r.goto(c.toString(),{timeout:l.timeout??2e4,waitUntil:l.waitUntil})},waitForClerkJsLoaded:async()=>r.waitForFunction(()=>window.Clerk?.loaded),signOut:async()=>r.waitForFunction(()=>window.Clerk?.signOut({})),waitForClerkComponentMounted:async()=>r.waitForSelector(".cl-rootBox",{state:"attached"}),waitForAppUrl:async i=>{if(!e.baseURL)throw new Error("Attempted to call method requiring baseURL, but baseURL was not provided to createPageObjects.");return r.waitForURL(new URL(i,e.baseURL).toString())},cookies:async()=>{let i=await r.context().cookies(),l=i.reduce((c,u)=>(u.name.match(/^(__.*_)(.{8})$/)?c.set(u.name.replace(/^(__.*_)(.{8})$/,"$1*"),u):c.set(u.name,u),c),new Map);return Object.assign(l,{raw:()=>i})}})};var k=a=>{let{page:e}=a,r={...s(a),waitForMounted:(t=".cl-checkout-root")=>e.waitForSelector(t,{state:"attached"}),closeDrawer:()=>e.locator(".cl-drawerClose").click(),fillTestCard:async()=>{await r.fillCard({number:"4242424242424242",expiration:"1234",cvc:"123",country:"United States",zip:"12345"})},fillCard:async t=>{let o=e.frameLocator('iframe[src*="elements-inner-payment"]');await o.getByLabel("Card number").fill(t.number),await o.getByLabel("Expiration date").fill(t.expiration),await o.getByLabel("Security code").fill(t.cvc),await o.getByLabel("Country").selectOption(t.country),await o.getByLabel("ZIP code").fill(t.zip)},waitForStripeElements:async({state:t="visible"}={})=>e.frameLocator('iframe[src*="elements-inner-payment"]').getByLabel("Card number").waitFor({state:t}),clickPayOrSubscribe:async()=>{await r.root.getByRole("button",{name:/subscribe|pay\s\$/i}).click()},waitForSubscribeButton:async()=>{await r.root.getByRole("button",{name:/^subscribe$/i}).waitFor({state:"visible"})},confirmAndContinue:async()=>{await r.root.getByRole("button",{name:/^continue$/i}).click()},clickAddPaymentMethod:async()=>{await r.root.getByRole("radio",{name:"Add payment method"}).click()},clickPaymentMethods:async()=>{await r.root.getByRole("radio",{name:"Payment Methods"}).click()},root:e.locator(".cl-checkout-root")};return r};var F=({page:a})=>({toBeLoaded:async()=>a.waitForFunction(()=>!!window.Clerk?.loaded),getClientSideActor:()=>a.evaluate(()=>window.Clerk?.session?.actor),toBeLoading:async()=>a.waitForFunction(()=>window.Clerk?.status==="loading"),toBeReady:async()=>a.waitForFunction(()=>window.Clerk?.status==="ready"),toBeDegraded:async()=>a.waitForFunction(()=>window.Clerk?.status==="degraded"),getClientSideUser:()=>a.evaluate(()=>window.Clerk?.user)});var w=require("@playwright/test"),S=({page:a})=>({toBeHandshake:async e=>{let r=await e.request().redirectedFrom()?.redirectedFrom()?.response();(0,w.expect)(r?.status()).toBe(307),(0,w.expect)(r?.headers()["x-clerk-auth-status"]).toContain("handshake")},toBeSignedOut:e=>a.waitForFunction(()=>!window.Clerk?.user,null,{timeout:e?.timeOut}),toBeSignedIn:async()=>a.waitForFunction(()=>!!window.Clerk?.user),toBeSignedInAsActor:async()=>a.waitForFunction(()=>!!window.Clerk?.session?.actor),toHaveResolvedTask:async()=>a.waitForFunction(()=>!window.Clerk?.session?.currentTask)});var T=a=>{let{page:e}=a;return{waitForMounted:(t=".cl-impersonationFab")=>e.waitForSelector(t,{state:"attached"}),getSignOutLink:()=>e.locator(".cl-impersonationFab").getByText("Sign out")}};var R=a=>{let{page:e}=a,r="#--clerk-keyless-prompt-button";return{waitForMounted:()=>e.waitForSelector(r,{state:"attached"}),waitForUnmounted:()=>e.waitForSelector(r,{state:"detached"}),isExpanded:()=>e.locator(r).getAttribute("aria-expanded").then(o=>o==="true"),toggle:()=>e.locator(r).click(),promptsToClaim:()=>e.getByRole("link",{name:/^claim application$/i}),promptToUseClaimedKeys:()=>e.getByRole("link",{name:/^get api keys$/i}),promptToDismiss:()=>e.getByRole("button",{name:/^dismiss$/i})}};var y=require("@playwright/test");var C=a=>{let{page:e}=a,r={...s(a),goTo:async(t="/switcher")=>(await e.goToRelative(t),r.waitForMounted()),waitForMounted:()=>e.waitForSelector(".cl-organizationSwitcher-root",{state:"attached"}),expectNoOrganizationSelected:()=>(0,y.expect)(e.getByText(/No organization selected/i)).toBeVisible(),expectPersonalAccount:()=>(0,y.expect)(e.getByText(/personal account/i)).toBeVisible(),toggleTrigger:()=>e.locator(".cl-organizationSwitcherTrigger").click(),waitForAnOrganizationToSelected:()=>e.waitForSelector(".cl-userPreviewMainIdentifier__personalWorkspace",{state:"detached"})};return r};var B=a=>{let{page:e}=a,r={toggle:n=>e.locator(`.cl-pricingTableCard__${n} .cl-pricingTableCardPeriodToggle`),indicator:n=>e.locator(`.cl-pricingTableCard__${n} .cl-switchIndicator`),badge:n=>e.locator(`.cl-pricingTableCard__${n} .cl-badge`),footer:n=>e.locator(`.cl-pricingTableCard__${n} .cl-pricingTableCardFooter`)},t=async(n,i)=>{async function l(u,g,$,D=5e3){return e.waitForFunction(({sel:K,attr:V,val:z})=>document.querySelector(K)?.getAttribute(V)===z,{sel:u,attr:g,val:$},{timeout:D}).then(()=>!0).catch(()=>!1)}let c=await l(`.cl-pricingTableCard__${n} .cl-switchIndicator`,"data-checked","true",500);c&&i==="monthly"&&await r.toggle(n).click(),!c&&i==="annually"&&await r.toggle(n).click()};return{...s(a),waitForMounted:(n=".cl-pricingTable-root")=>e.waitForSelector(n,{state:"attached"}),clickResubscribe:async()=>{await e.getByText("Re-subscribe").click()},waitToBeActive:async({planSlug:n})=>r.badge(n).getByText("Active").waitFor({state:"visible"}),getPlanCardCTA:({planSlug:n})=>r.footer(n).getByRole("button",{name:/get|switch|subscribe/i}),startCheckout:async({planSlug:n,shouldSwitch:i,period:l})=>{let c=i===!0?"Switch to this plan":i===!1?/subscribe/i:/get|switch|subscribe/i;l&&await t(n,l),await r.footer(n).getByRole("button",{name:c}).click()}}};var x=require("@playwright/test");var E=a=>{let{page:e}=a;return{...s(a),resolveForceOrganizationSelectionTask:async t=>{let o=e.getByRole("button",{name:/create new organization/i});await(0,x.expect)(o).toBeVisible(),await e.locator("input[name=name]").fill(t.name),await e.locator("input[name=slug]").fill(t.slug),await o.click()}}};var f=require("@playwright/test");var O=a=>{let{page:e}=a,r={...s(a),goTo:async t=>{let o=await e.goToRelative("/sign-in",t);return typeof t?.headlessSelector<"u"?await r.waitForMounted(t.headlessSelector):await r.waitForMounted(),o},waitForMounted:(t=".cl-signIn-root")=>e.waitForSelector(t,{state:"attached"}),waitForModal:t=>e.waitForSelector(".cl-modalContent:has(.cl-signIn-root)",{state:t==="closed"?"detached":"attached"}),setIdentifier:t=>r.getIdentifierInput().fill(t),setInstantPassword:async t=>{let o=r.getPasswordInput();await(0,f.expect)(o).toBeVisible(),await o.fill(t,{force:!0})},usePhoneNumberIdentifier:()=>e.getByRole("link",{name:/^use phone/i}),useEmailIdentifier:()=>e.getByRole("link",{name:/^use email/i}),useUsernameIdentifier:()=>e.getByRole("link",{name:/^username$/i}),getForgotPassword:()=>e.getByRole("link",{name:/forgot password/i}),getGoToSignUp:()=>e.getByRole("link",{name:/sign up/i}),getResetPassword:()=>e.getByRole("button",{name:/(reset password|reset your password)/i}),getUseAnotherMethodLink:()=>e.getByRole("link",{name:/use another method/i}),getAltMethodsEmailCodeButton:()=>e.getByRole("button",{name:/email code to/i}),getAltMethodsEmailLinkButton:()=>e.getByRole("button",{name:/email link to/i}),signInWithOauth:t=>e.getByRole("button",{name:new RegExp(`continue with ${t}`,"gi")}),signInWithEmailAndInstantPassword:async({email:t,password:o,waitForSession:n=!0})=>{let i=r.getIdentifierInput();await(0,f.expect)(i).toBeVisible(),await i.fill(t),await r.setInstantPassword(o),await r.continue(),n&&await r.waitForSession()}};return r};var U=a=>{let{page:e}=a,r={...s(a),goTo:async t=>(await e.goToRelative("/sign-up",{searchParams:t?.searchParams}),typeof t?.headlessSelector<"u"?r.waitForMounted(t.headlessSelector):r.waitForMounted()),waitForMounted:(t=".cl-signUp-root")=>e.waitForSelector(t,{state:"attached"}),waitForModal:t=>e.waitForSelector(".cl-modalContent:has(.cl-signUp-root)",{state:t==="closed"?"detached":"attached"}),signUpWithOauth:t=>e.getByRole("button",{name:new RegExp(`continue with ${t}`,"gi")}),signUp:async t=>{t.firstName&&await r.getFirstNameInput().fill(t.firstName),t.lastName&&await r.getLastNameInput().fill(t.lastName),t.email&&await r.getEmailAddressInput().fill(t.email),t.username&&await r.getUsernameInput().fill(t.username),t.phoneNumber&&await r.getPhoneNumberInput().fill(t.phoneNumber),t.password&&await r.getPasswordInput().fill(t.password),t.legalAccepted&&await r.getLegalAccepted().check(),await r.continue()},signUpWithEmailAndPassword:async t=>{await r.signUp({email:t.email,password:t.password})},waitForEmailVerificationScreen:async()=>{await e.waitForURL(/verify/),await e.getByRole("heading",{name:/Verify your email/i}).waitFor()}};return r};var I=a=>{let{page:e}=a,r={...s(a),waitForMounted:(t=".cl-subscriptionDetails-root")=>e.waitForSelector(t,{state:"attached"}),waitForUnmounted:()=>r.root.locator(".cl-drawerRoot").waitFor({state:"detached"}),closeDrawer:()=>r.root.locator(".cl-drawerClose").click(),root:e.locator(".cl-subscriptionDetails-root")};return r};var L=({page:a})=>({setup:async()=>p({page:a})});var v=require("@playwright/test"),A=a=>{let{page:e}=a;return{waitForMounted:()=>e.waitForSelector(".cl-userButtonTrigger",{state:"attached"}),toggleTrigger:()=>e.locator(".cl-userButtonTrigger").click(),waitForPopover:()=>e.waitForSelector(".cl-userButtonPopoverCard",{state:"visible"}),waitForPopoverClosed:()=>e.waitForSelector(".cl-userButtonPopoverCard",{state:"detached"}),toHaveVisibleMenuItems:async t=>{(typeof t=="string"||t instanceof RegExp)&&(t=[t]);for(let o of t)await(0,v.expect)(e.getByRole("menuitem",{name:o})).toBeVisible()},triggerSignOut:()=>e.getByRole("menuitem",{name:/Sign out$/i}).click(),triggerManageAccount:()=>e.getByRole("menuitem",{name:/Manage account/i}).click(),switchAccount:t=>e.getByText(t).click()}};var j=a=>{let{page:e}=a,r={...s(a),goTo:async t=>(await e.goToRelative("/user",t),r.waitForMounted()),switchToSecurityTab:async()=>{await e.getByText(/Security/i).click()},switchToBillingTab:async()=>{await e.getByText(/Billing/i).click()},waitForMounted:()=>e.waitForSelector(".cl-userProfile-root",{state:"attached"}),clickSetUsername:()=>e.getByText(/Set username/i).click(),clickToUpdateProfile:()=>e.getByText(/update profile/i).click(),clickUpdateUsername:()=>e.getByText(/update username/i).click(),clickSetPassword:()=>e.getByText(/Set password/i).click(),waitForSectionCard:(t,o)=>e.waitForSelector(`.cl-profileSectionContent__${t} .cl-headerTitle`,{state:o?"visible":"detached"}),waitForSectionCardOpened:t=>r.waitForSectionCard(t,!0),waitForSectionCardClosed:t=>r.waitForSectionCard(t,!1),typeUsername:t=>r.getUsernameInput().fill(t),typeFirstName:t=>r.getFirstNameInput().fill(t),typeLastName:t=>r.getLastNameInput().fill(t),typePhoneNumber:t=>r.getPhoneNumberInput().fill(t),clickAddEmailAddress:()=>e.getByText(/add email address/i).click(),clickAddPhoneNumber:()=>e.getByText(/add phone number/i).click(),typeEmailAddress:t=>e.getByLabel(/Email address/i).fill(t),waitForUserProfileModal:t=>e.waitForSelector(".cl-modalContent:has(.cl-userProfile-root)",{state:t==="closed"?"detached":"attached"})};return r};var M=a=>{let{page:e}=a;return{...s(a),waitForMounted:(t=".cl-userVerification-root")=>e.waitForSelector(t,{state:"attached"}),waitForClosed:(t=".cl-userVerification-root")=>e.waitForSelector(t,{state:"detached"}),closeReverificationModal:()=>e.getByLabel("Close modal").click(),getUseAnotherMethodLink:()=>e.getByRole("link",{name:/use another method/i}),getAltMethodsEmailCodeButton:()=>e.getByRole("button",{name:/email code to/i}),getAltMethodsEmailLinkButton:()=>e.getByRole("button",{name:/email link to/i})}};var N=a=>{let{page:e}=a,r={...s(a),goTo:async t=>(await e.goToRelative("/waitlist",{searchParams:t?.searchParams}),typeof t?.headlessSelector<"u"?r.waitForMounted(t.headlessSelector):r.waitForMounted()),waitForMounted:(t=".cl-waitlist-root")=>e.waitForSelector(t,{state:"attached"}),joinWaitlist:async t=>{await r.getEmailAddressInput().fill(t.email),await r.joinWaitlistContinue()},joinWaitlistContinue:()=>e.getByRole("button",{name:"Join the waitlist",exact:!0}).click()};return r};var _=({page:a,useTestingToken:e=!0,baseURL:r})=>{let t=m({page:a,useTestingToken:e},{baseURL:r}),o={page:t};return{page:t,clerk:F(o),checkout:k(o),expect:S(o),impersonation:T(o),keylessPopover:R(o),organizationSwitcher:C(o),pricingTable:B(o),sessionTask:E(o),signIn:O(o),signUp:U(o),testingToken:L(o),userButton:A(o),userProfile:j(o),userVerification:M(o),waitlist:N(o),apiKeys:h(o),subscriptionDetails:I(o)}};0&&(module.exports={createAppPageObject,createPageObjects});
//# sourceMappingURL=index.js.map