2023-05-24 16:05:45 +03:00
|
|
|
import {E2E_PORT} from '../../playwright.config';
|
|
|
|
|
2023-05-30 12:57:56 +03:00
|
|
|
const MOCKED_SITE_URL = 'http://localhost:1234';
|
|
|
|
|
|
|
|
type LastApiRequest = {
|
|
|
|
body: null | any
|
|
|
|
};
|
|
|
|
|
2023-06-01 05:55:03 +03:00
|
|
|
export async function initialize({page, ...options}: {page: any; title?: string, description?: string, logo?: string, backgroundColor?: string, buttonColor?: string, site?: string, 'label-1'?: string, 'label-2'?: string}) {
|
2023-05-24 16:05:45 +03:00
|
|
|
const url = `http://localhost:${E2E_PORT}/signup-form.min.js`;
|
|
|
|
|
|
|
|
await page.goto('about:blank');
|
|
|
|
await page.setViewportSize({width: 1000, height: 1000});
|
2023-05-30 12:57:56 +03:00
|
|
|
const lastApiRequest = await mockApi({page});
|
|
|
|
|
|
|
|
if (!options.site) {
|
|
|
|
options.site = MOCKED_SITE_URL;
|
|
|
|
}
|
2023-05-24 16:05:45 +03:00
|
|
|
|
|
|
|
await page.evaluate((data) => {
|
|
|
|
const scriptTag = document.createElement('script');
|
|
|
|
scriptTag.src = data.url;
|
|
|
|
|
|
|
|
for (const option of Object.keys(data.options)) {
|
|
|
|
scriptTag.dataset[option] = data.options[option];
|
|
|
|
}
|
|
|
|
document.body.appendChild(scriptTag);
|
|
|
|
}, {url, options});
|
2023-05-30 14:41:38 +03:00
|
|
|
|
2023-05-24 16:05:45 +03:00
|
|
|
await page.waitForSelector('iframe');
|
2023-05-30 12:57:56 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
frame: page.frameLocator('iframe'),
|
|
|
|
lastApiRequest
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function mockApi({page}: {page: any}) {
|
|
|
|
const lastApiRequest: LastApiRequest = {
|
|
|
|
body: null
|
|
|
|
};
|
|
|
|
|
|
|
|
await page.route(`${MOCKED_SITE_URL}/members/api/send-magic-link/`, async (route) => {
|
|
|
|
const requestBody = JSON.parse(route.request().postData());
|
|
|
|
lastApiRequest.body = requestBody;
|
|
|
|
|
|
|
|
await route.fulfill({
|
|
|
|
status: 200,
|
|
|
|
body: 'ok'
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return lastApiRequest;
|
2023-05-24 16:05:45 +03:00
|
|
|
}
|