import { BrowserContext, Locator, Page } from "playwright-core";
import { FormInputItem, extendPlaywrightPage } from "@intuned/sdk/playwright";
export interface Input {
firstName: string;
lastName: string;
address1: string;
address2: string;
city: string;
state: string;
zip: string;
country: string;
nameOnCard: string;
cardNumber: string;
expiration: string;
cvv: string;
saveAddress: boolean;
}
export default async function handler(
params: Input,
_playwrightPage: Page,
context: BrowserContext
) {
const page = extendPlaywrightPage(_playwrightPage);
await page.goto("https://demo-site-eta.vercel.app/steps-form/ShippingAddress");
const fields: FormInputItem[] = [
{
fieldSelector: {
selector: "[name='firstName']",
type: "css",
},
value: { type: "static", value: params.firstName },
fieldType: "text-input",
},
{
fieldSelector: {
selector: "[name='lastName']",
type: "css",
},
value: { type: "static", value: params.lastName },
fieldType: "text-input",
},
{
fieldSelector: {
selector: "[name='addressLine1']",
type: "css",
},
value: { type: "static", value: params.address1 },
fieldType: "text-input",
},
{
fieldSelector: {
selector: "[name='addressLine2']",
type: "css",
},
value: { type: "static", value: params.address2 },
fieldType: "text-input",
},
{
fieldSelector: {
selector: "[name='city']",
type: "css",
},
value: { type: "static", value: params.city },
fieldType: "text-input",
},
{
fieldSelector: {
selector: "[name='state']",
type: "css",
},
value: { type: "static", value: params.state },
fieldType: "text-input",
},
{
fieldSelector: {
selector: "[name='zipCode']",
type: "css",
},
value: { type: "static", value: params.zip },
fieldType: "text-input",
},
{
fieldSelector: {
selector: "[name='country']",
type: "css",
},
value: { type: "dynamic", source: { country: params.country } },
fieldType: "select",
},
{
fieldSelector: {
selector: "[name='futurePurchase']",
type: "css",
},
fieldType: "checkbox",
value: { type: "static", value: true },
},
];
const didFormSucceed = async (locator: Locator): Promise<boolean> => {
return (await locator.page().locator(".error-message").count()) === 0
};
async function formSubmit(locator: Locator) {
const nextButtonLocator = locator.page().getByRole("button", { name: "Next" });
await nextButtonLocator.waitFor({ state: "visible" });
await nextButtonLocator.click();
}
await page.fillForm({
formLocator: page.locator("main"),
formInput: fields,
isSubmitSuccessful: didFormSucceed,
submitForm: formSubmit,
autoRecoveryOptions: {
enabled: true,
recoveryData: params
}
});
return {};
}