Install playwright

This commit is contained in:
Aarni Halinen
2023-10-03 20:59:42 +03:00
parent d62ce26759
commit 8abbca1b41
20 changed files with 262 additions and 16 deletions
+170
View File
@@ -0,0 +1,170 @@
/* eslint-disable no-console */
import { Selector, ClientFunction, RequestLogger } from "testcafe";
import axios from "axios";
const API_URL = "https://api.dev.sahkoinsinoorikilta.fi/api";
export const getSiteRoot = (): string => process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3000";
export const getPageUrl = ClientFunction(() => window.location.pathname);
export const getPostRequestLogger = (url: string) => RequestLogger({ url: `${API_URL}/${url}`, method: "POST" }, {
// logResponseHeaders: true,
logResponseBody: true,
stringifyResponseBody: true,
});
const openTime = new Date("1994-01-14T22:51:00+02:00");
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const USERNAME = "admin";
const PASSWORD = "password123";
export const doLogin = async (t: TestController) => {
await t.typeText(Selector("#login-username"), USERNAME);
await t.typeText(Selector("#login-password"), PASSWORD);
await t.click(Selector("#login-submit"));
};
export async function generateAccessToken(): Promise<string> {
const tokenUrl = `${API_URL}/token/`;
try {
const resp = await axios.post(tokenUrl, {
username: USERNAME,
password: PASSWORD,
});
return resp.data.access;
} catch (err) {
console.error(err);
throw err;
}
}
const eventURL = `${API_URL}/events/`;
export async function createEvent(data, jwt_access: string) {
try {
const resp = await axios.post(eventURL, data, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
export async function deleteEvent(id: string, jwt_access: string) {
try {
const resp = await axios.delete(`${eventURL}${id}/`, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
const formURL = `${API_URL}/signupForm/`;
export async function createForm(data, jwt_access: string) {
try {
const resp = await axios.post(formURL, data, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
export async function deleteForm(id: string, jwt_access: string) {
try {
const resp = await axios.delete(`${formURL}${id}/`, {
headers: {
Authorization: `Bearer ${jwt_access}`,
},
});
return resp.data;
} catch (err) {
console.error(err);
throw err;
}
}
export const generateTestForm = async (jwt_access: string) => (
createForm({
title_fi: "Testi Ilmo",
title_en: "Test Signup",
visible: true,
quota: 0,
start_time: openTime,
end_time: tomorrow,
email_content: "E2E Test",
questions: [{
id: "Kv0IRYUWE", type: "name", options: { enum: [], enumNames_en: [], enumNames_fi: [] }, required: true, title_en: "Name", title_fi: "Nimi",
}, {
id: "_9o78DbdZ", type: "email", options: { enum: [], enumNames_en: [], enumNames_fi: [] }, required: true, title_en: "Email", title_fi: "S-Posti",
}, {
id: "-Zk6tCy7U", type: "radiobutton", options: { enum: ["Nuori", "Vanha", "Testaaja"], enumNames_en: ["Yung", "Old", "Tester"], enumNames_fi: ["Nuori", "Vanha", "Testaaja"] }, title_en: "I am", title_fi: "Olen",
}],
id: 14,
isOpen: true,
schema: {
type: "object",
required: ["Kv0IRYUWE", "_9o78DbdZ"],
properties: {
Kv0IRYUWE: { type: "string", title: "Nimi" },
_9o78DbdZ: {
type: ["string"], title: "S-Posti", format: "email", pattern: "^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*$", default: null,
},
"-Zk6tCy7U": {
type: "string", title: "Olen", pattern: "^Nuori$|^Vanha$|^Testaaja$", enum: ["Nuori", "Vanha", "Testaaja"],
},
},
},
}, jwt_access)
);
export const generateTestEvent = async (formIds = [], jwt_access: string) => (
createEvent({
tags: [1],
visible: true,
start_time: openTime,
end_time: tomorrow,
title_fi: "title_fi",
description_fi: "desc_fi",
content_fi: "content_fi",
location_fi: "location_fi",
title_en: "title_en",
description_en: "desc_en",
content_en: "content_en",
location_en: "location_en",
image: null,
signupForm: formIds,
signup_id: formIds,
tag_id: [1],
}, jwt_access)
);
export const sleep = async (ms: number) => new Promise((resolve) => { setTimeout(resolve, ms); });
export const waitForLogger = async (logger: RequestLogger) => {
let i = 0;
while (i < 50) {
await sleep(100); // eslint-disable-line no-await-in-loop
if (logger.requests.length > 0) {
return;
}
i += 1;
}
};