@gravitywiz/playwright-plugin-gravity-wiz
Version:
Playwright plugin for testing WordPress and Gravity Wiz plugins
139 lines • 4.6 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tasks = void 0;
exports.globalSetup = globalSetup;
exports.globalTeardown = globalTeardown;
const execa_1 = require("./utils/execa");
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const outdent_1 = require("outdent");
/**
* Global setup function for Gravity Wiz tests
*/
async function globalSetup(options = {}) {
// Set up emails folder
const emailsFolder = options.emailsFolder ||
path_1.default.resolve(os_1.default.tmpdir(), './gwiz-playwright-emails');
await setupEmailCapture(emailsFolder);
return {
emailsFolder,
};
}
/**
* Global teardown function for Gravity Wiz tests
*/
async function globalTeardown() {
console.info('Cleaning up test environment...');
// Check for xdebug errors and database errors
// This would typically be done in individual tests
// Clean up any temporary files if needed
}
/**
* Set up email capture functionality
*/
async function setupEmailCapture(emailsFolder) {
if (!fs_1.default.existsSync(emailsFolder)) {
fs_1.default.mkdirSync(emailsFolder, { recursive: true });
}
// Clear out existing emails
const files = fs_1.default.readdirSync(emailsFolder);
for (const file of files) {
fs_1.default.unlinkSync(path_1.default.join(emailsFolder, file));
}
fs_1.default.chmodSync(emailsFolder, 0o777);
// Set the email output path in WordPress
await (0, execa_1.execaCommand)('wp', [
'option',
'update',
'gwiz_playwright_mail_output_path',
emailsFolder,
]);
}
/**
* Task functions that can be used in tests
*/
exports.tasks = {
/**
* Add a plugin file dynamically
*/
async addPlugin({ filename, contents, }) {
let pluginPath = path_1.default.resolve(__dirname, '..');
const { stdout } = await (0, execa_1.execaCommand)('wp', ['plugin', 'path']);
if (stdout) {
pluginPath = stdout.trim();
}
const cleanedContents = (0, outdent_1.outdent)({ trimTrailingNewline: false }).string(contents);
const filePath = path_1.default.resolve(pluginPath, filename);
fs_1.default.writeFileSync(filePath, cleanedContents);
fs_1.default.chmodSync(filePath, 0o755);
return null;
},
/**
* Clear all emails from the capture folder
*/
async clearEmails(emailsFolder) {
if (!fs_1.default.existsSync(emailsFolder)) {
fs_1.default.mkdirSync(emailsFolder, { recursive: true });
}
// Clear out emails
const files = fs_1.default.readdirSync(emailsFolder);
for (const file of files) {
fs_1.default.unlinkSync(path_1.default.join(emailsFolder, file));
}
fs_1.default.chmodSync(emailsFolder, 0o777);
return null;
},
/**
* List all captured emails
*/
listEmails(emailsFolder) {
return fs_1.default
.readdirSync(emailsFolder)
.filter((name) => name.indexOf('.json') !== -1)
.map((fileName) => ({
name: fileName,
time: fs_1.default
.statSync(`${emailsFolder}/${fileName}`)
.mtime.getTime(),
}))
.sort((a, b) => a.time - b.time)
.map((file) => file.name);
},
/**
* Read a specific email file
*/
readEmail(emailsFolder, emailFilename) {
return JSON.parse(fs_1.default.readFileSync(path_1.default.join(emailsFolder, emailFilename)).toString());
},
/**
* Clear the WordPress debug log
*/
async clearDebugLog() {
const { stdout: logPath } = await (0, execa_1.execaCommand)('wp', [
'eval',
`echo WP_CONTENT_DIR . '/debug.log';`,
]);
if (fs_1.default.existsSync(logPath)) {
fs_1.default.rmSync(logPath);
}
return null;
},
/**
* Get the WordPress debug log contents
*/
async getDebugLog() {
const { stdout: logPath } = await (0, execa_1.execaCommand)('wp', [
'eval',
`echo WP_CONTENT_DIR . '/debug.log';`,
]);
if (!fs_1.default.existsSync(logPath)) {
return '';
}
return fs_1.default.readFileSync(logPath).toString();
},
};
//# sourceMappingURL=setup.js.map