Skip to content

Commit

Permalink
WIP: format usage experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
vyncent-t committed Aug 5, 2024
1 parent 7bc906b commit 5152204
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 58 deletions.
87 changes: 29 additions & 58 deletions app/app-e2e-tests/e2e/example.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test } from '@playwright/test';
import { expect, Page, test } from '@playwright/test';
import { _electron } from '@playwright/test';
import { spawn } from 'child_process';

Expand Down Expand Up @@ -45,40 +45,15 @@ async function runCommand(command: string, args: string[] = [], options = {}): P
});
}

// Function to run a shell command and return a promise
async function runCommand(command: string, args: string[] = [], options = {}): Promise<void> {
return new Promise((resolve, reject) => {
const process = spawn(command, args, { ...options, detached: true, stdio: 'ignore' });

process.on('error', error => {
console.error(`Error executing command: ${command}\n${error}`);
reject(error);
});

process.on('close', code => {
if (code !== 0) {
console.error(`Command failed with exit code ${code}`);
reject(new Error(`Command failed with exit code ${code}`));
} else {
resolve();
}
});

// Detach the process and resolve immediately
process.unref();
resolve();
});
}

test('launch app', async () => {
// Launch Electron app.
const electronApp = await electron.launch({
cwd: '/home/vtaylor/headlamp/app/electron',
timeout: 0,
args: ['main.js'],
});
electronApp.process().stdout.on('data', data => console.log(`stdoutX: ${data}`));
electronApp.process().stderr.on('data', error => console.log(`stderrX: ${error}`));
// electronApp.process().stdout.on('data', data => console.log(`stdoutX: ${data}`));
// electronApp.process().stderr.on('data', error => console.log(`stderrX: ${error}`));
// Evaluation expression in the Electron context.
// const appPath = await electronApp.evaluate(async (electron) => {
// // This runs in the main Electron process, |electron| parameter
Expand Down Expand Up @@ -106,57 +81,53 @@ test('launch app', async () => {

// await headlampPage.authenticate();
// await headlampPage.hasNetworkTab();

// Direct Electron console to Node terminal.
window.on('console', console.log);
// Click button.
// await window.click('text=Click me');
});

test('launch app with integrated shell commands', async () => {
// Start the frontend server
await runCommand('npm', ['start'], { cwd: '/home/skoeva/headlamp/frontend' });
await runCommand('make', ['run-backend'], { cwd: '/home/skoeva/headlamp' });
class HeadlampPage {
constructor(private page: Page) {}

// Launch Electron app
async homepage() {
await this.page.goto('http://localhost:3000/');
await this.page.waitForLoadState('domcontentloaded');
await this.page.waitForSelector('button');
await this.page.click('button');
}
}

test('launch app w/page', async () => {
// Launch Electron app.
const electronApp = await electron.launch({
cwd: '/home/skoeva/headlamp/app/electron',
cwd: '/home/vtaylor/headlamp/app/electron',
timeout: 0,
args: ['main.js'],
});
electronApp.process().stdout.on('data', data => console.log(`stdoutX: ${data}`));
electronApp.process().stderr.on('data', error => console.log(`stderrX: ${error}`));

// Get the first window that the app opens, wait if necessary.
const window = await electronApp.firstWindow();
await window.waitForLoadState('domcontentloaded');

// Navigate to the start URL and click on a button
await window.goto('http://localhost:3000/');
await window.getByRole('button', { name: 'Home' }).click();

// Ensure that we are in the home context
const title = await window.title();
expect(title).not.toBe(null);
expect(title).toBe('Choose a cluster');
// const context = await electronApp.context();
const page = await electronApp.firstWindow();
const headlampPage = new HeadlampPage(page);

// Direct Electron console to Node terminal.
window.on('console', console.log);

// Close the Electron app
await electronApp.close();
electronApp.on('window', async () => {
await headlampPage.homepage();
});
});

test('launch app with integrated shell commands', async () => {
// Start the frontend server
await runCommand('npm', ['start'], { cwd: '/home/skoeva/headlamp/frontend' });
await runCommand('make', ['run-backend'], { cwd: '/home/skoeva/headlamp' });
await runCommand('npm', ['start'], { cwd: '/home/vtaylor/headlamp/frontend' });
await runCommand('make', ['run-backend'], { cwd: '/home/vtaylor/headlamp' });

// Launch Electron app
const electronApp = await electron.launch({
cwd: '/home/skoeva/headlamp/app/electron',
cwd: '/home/vtaylor/headlamp/app/electron',
args: ['main.js'],
});
electronApp.process().stdout.on('data', data => console.log(`stdoutX: ${data}`));
electronApp.process().stderr.on('data', error => console.log(`stderrX: ${error}`));
// electronApp.process().stdout.on('data', data => console.log(`stdoutX: ${data}`));
// electronApp.process().stderr.on('data', error => console.log(`stderrX: ${error}`));

// Get the first window that the app opens, wait if necessary.
const window = await electronApp.firstWindow();
Expand Down
133 changes: 133 additions & 0 deletions app/app-e2e-tests/e2e/examplePage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { Page } from '@playwright/test';

export class HeadlampPage {
constructor(private page: Page) {}

async loadHomepage() {
await this.page.goto('http://localhost:3000/');

await this.page.waitForLoadState('domcontentloaded');

await this.page.goto('http://localhost:3000/');
await this.page.getByRole('button', { name: 'Home' }).click();
}

// async authenticate() {
// await this.page.goto('/');
// await this.page.waitForSelector('h1:has-text("Authentication")');

// // Expects the URL to contain c/main/token
// this.hasURLContaining(/.*token/);

// const token = process.env.HEADLAMP_TOKEN || '';
// this.hasToken(token);

// // Fill in the token
// await this.page.locator('#token').fill(token);

// // Click on the "Authenticate" button and wait for navigation
// await Promise.all([
// this.page.waitForNavigation(),
// this.page.click('button:has-text("Authenticate")'),
// ]);
// }

// async hasURLContaining(pattern: RegExp) {
// await expect(this.page).toHaveURL(pattern);
// }

// async hasTitleContaining(pattern: RegExp) {
// await expect(this.page).toHaveTitle(pattern);
// }

// async hasToken(token: string) {
// expect(token).not.toBe('');
// }

// async hasNetworkTab() {
// const networkTab = this.page.locator('span:has-text("Network")').first();
// expect(await networkTab.textContent()).toBe('Network');
// }

// async hasSecurityTab() {
// const networkTab = this.page.locator('span:has-text("Security")').first();
// expect(await networkTab.textContent()).toBe('Security');
// }

// async checkPageContent(text: string) {
// await this.page.waitForSelector(`:has-text("${text}")`);
// const pageContent = await this.page.content();
// expect(pageContent).toContain(text);
// }

// async pageLocatorContent(locator: string, text: string) {
// const pageContent = this.page.locator(locator).textContent();
// expect(await pageContent).toContain(text);
// }

// async navigateTopage(page: string, title: RegExp) {
// await this.page.goto(page);
// await this.page.waitForLoadState('load');
// await this.hasTitleContaining(title);
// }

// async logout() {
// // Click on the account button to open the user menu
// await this.page.click('button[aria-label="Account of current user"]');

// // Wait for the logout option to be visible and click on it
// await this.page.waitForSelector('a.MuiMenuItem-root:has-text("Log out")');
// await this.page.click('a.MuiMenuItem-root:has-text("Log out")');
// await this.page.waitForLoadState('load');

// // Expects the URL to contain c/main/token
// await this.hasURLContaining(/.*token/);
// }

// async tableHasHeaders(tableSelector: string, expectedHeaders: string[]) {
// // Get all table headers
// const headers = await this.page.$$eval(`${tableSelector} th`, ths =>
// ths.map(th => {
// if (th && th.textContent) {
// // Table header also contains a number, displayed during multi-sorting, so we remove it
// return th.textContent.trim().replace('0', '');
// }
// })
// );

// // Check if all expected headers are present in the table
// for (const header of expectedHeaders) {
// if (!headers.includes(header)) {
// throw new Error(`Table does not contain header: ${header}`);
// }
// }
// }

// async clickOnPlugin(pluginName: string) {
// await this.page.click(`a:has-text("${pluginName}")`);
// await this.page.waitForLoadState('load');
// }

// async checkRows() {
// // Get value of rows per page
// const rowsDisplayed1 = await this.getRowsDisplayed();

// // Click on the next page button
// const nextPageButton = this.page.getByRole('button', {
// name: 'Go to next page',
// });
// await nextPageButton.click();

// // Get value of rows per page after clicking next page button
// const rowsDisplayed2 = await this.getRowsDisplayed();

// // Check if the rows displayed are different
// expect(rowsDisplayed1).not.toBe(rowsDisplayed2);
// }

// async getRowsDisplayed() {
// const paginationCaption = this.page.locator("span:has-text(' of ')");
// const captionText = await paginationCaption.textContent();
// return captionText;
// }
}

0 comments on commit 5152204

Please sign in to comment.