Skip to content

Instantly share code, notes, and snippets.

@hexylena
Created April 21, 2022 11:11
Show Gist options
  • Save hexylena/c6a97e99d29adc350b5d907aaf9b621b to your computer and use it in GitHub Desktop.
Save hexylena/c6a97e99d29adc350b5d907aaf9b621b to your computer and use it in GitHub Desktop.
Testing logins with playwright to automate Galaxy things
const { chromium } = require('playwright-chromium');
const fs = require('fs');
(async () => {
const browser = await chromium.launch();
var contextOptions = {
ignoreHTTPSErrors: true,
}
fs.stat("state.json", (err, stats) => {
if(err === null){
contextOptions['storageState'] = 'state.json'
}
})
const context = await browser.newContext(contextOptions);
const page = await context.newPage();
fs.stat("session.json", (err, stats) => {
if(err === null){
const sessionStorage = JSON.parse(fs.readFileSync("state.json").toString("utf-8"))
context.addInitScript(storage => {
if (window.location.hostname === 'usegalaxy.eu') {
const entries = JSON.parse(storage);
for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, value);
}
}
}, sessionStorage);
}
})
await page.goto('https://usegalaxy.eu/');
})();
const { chromium } = require('playwright-chromium');
const fs = require('fs');
(async () => {
const browser = await chromium.launch();
var contextOptions = {
ignoreHTTPSErrors: true,
}
fs.stat("state.json", (err, stats) => {
if(err === null){
contextOptions['storageState'] = 'state.json'
}
})
const context = await browser.newContext(contextOptions);
const page = await context.newPage();
fs.stat("session.json", (err, stats) => {
if(err === null){
const sessionStorage = JSON.parse(fs.readFileSync("state.json").toString("utf-8"))
context.addInitScript(storage => {
if (window.location.hostname === 'usegalaxy.eu') {
const entries = JSON.parse(storage);
for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, value);
}
}
}, sessionStorage);
}
})
await page.goto('https://usegalaxy.eu/login');
//await page.locator('#register-toggle').click();
await page.locator('input[name="login"]').fill('email');
await page.locator('input[name="password"]').fill('password');
await page.locator('button[name="login"]').click();
await page.waitForLoadState('networkidle');
// Save storage state into the file.
await context.storageState({ path: 'state.json' });
// Get session storage and store as file
const sessionStorage = await page.evaluate(() => JSON.stringify(sessionStorage));
fs.writeFile('session.json', sessionStorage, 'utf8', () => {})
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment