Make tests possible without human interaction

This commit is contained in:
Johannes Zellner 2024-10-08 12:15:54 +02:00
parent 9eea2f2e29
commit ddb5032db9
3 changed files with 45 additions and 17 deletions

14
test/package-lock.json generated
View file

@ -9,7 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"chromedriver": "^129.0.0",
"chromedriver": "^129.0.2",
"safetydance": "^2.4.0",
"selenium-webdriver": "^4.25.0"
},
@ -263,9 +263,9 @@
}
},
"node_modules/chromedriver": {
"version": "129.0.0",
"resolved": "https://registry.npmjs.org/chromedriver/-/chromedriver-129.0.0.tgz",
"integrity": "sha512-B1ccqD6hDjNrw94FeqdynIotn1ZV/TnFrkRz2Rync2kzSnq6D6IrSkN1w5Pnuvnc98QhN2xujxDXxkqEqy/PWg==",
"version": "129.0.2",
"resolved": "https://registry.npmjs.org/chromedriver/-/chromedriver-129.0.2.tgz",
"integrity": "sha512-rUEFCJAmAwOdFfaDFtveT97fFeA7NOxlkgyPyN+G09Ws4qGW39aLDxMQBbS9cxQQHhTihqZZobgF5CLVYXnmGA==",
"hasInstallScript": true,
"dependencies": {
"@testim/chrome-version": "^1.1.4",
@ -1781,9 +1781,9 @@
}
},
"chromedriver": {
"version": "129.0.0",
"resolved": "https://registry.npmjs.org/chromedriver/-/chromedriver-129.0.0.tgz",
"integrity": "sha512-B1ccqD6hDjNrw94FeqdynIotn1ZV/TnFrkRz2Rync2kzSnq6D6IrSkN1w5Pnuvnc98QhN2xujxDXxkqEqy/PWg==",
"version": "129.0.2",
"resolved": "https://registry.npmjs.org/chromedriver/-/chromedriver-129.0.2.tgz",
"integrity": "sha512-rUEFCJAmAwOdFfaDFtveT97fFeA7NOxlkgyPyN+G09Ws4qGW39aLDxMQBbS9cxQQHhTihqZZobgF5CLVYXnmGA==",
"requires": {
"@testim/chrome-version": "^1.1.4",
"axios": "^1.7.4",

View file

@ -13,7 +13,7 @@
"mocha": "^10.7.3"
},
"dependencies": {
"chromedriver": "^129.0.0",
"chromedriver": "^129.0.2",
"safetydance": "^2.4.0",
"selenium-webdriver": "^4.25.0"
}

View file

@ -1,9 +1,9 @@
#!/usr/bin/env node
/* jshint esversion: 8 */
/* global describe */
/* global before */
/* global after */
/* global afterEach */
/* global it */
'use strict';
@ -12,7 +12,7 @@ require('chromedriver');
const execSync = require('child_process').execSync,
expect = require('expect.js'),
readlinePromises = require('readline/promises'),
fs = require('fs'),
path = require('path'),
safe = require('safetydance'),
util = require('util'),
@ -27,7 +27,7 @@ if (!process.env.USERNAME || !process.env.PASSWORD || !process.env.EMAIL) {
describe('Application life cycle test', function () {
this.timeout(0);
const LOCATION = 'test';
const LOCATION = process.env.LOCATION || 'test';
const TEST_TIMEOUT = parseInt(process.env.TEST_TIMEOUT, 10) || 30000;
const EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' };
@ -40,13 +40,37 @@ describe('Application life cycle test', function () {
let athenticated_by_oidc = false;
before(function () {
browser = new Builder().forBrowser('chrome').setChromeOptions(new Options().windowSize({ width: 1280, height: 1024 })).build();
const chromeOptions = new Options().windowSize({ width: 1280, height: 1024 });
if (process.env.CI) chromeOptions.addArguments('no-sandbox', 'disable-dev-shm-usage', 'headless');
browser = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
if (!fs.existsSync('./screenshots')) fs.mkdirSync('./screenshots');
});
after(function () {
browser.quit();
});
afterEach(async function () {
if (!process.env.CI || !app) return;
const currentUrl = await browser.getCurrentUrl();
if (!currentUrl.includes(app.domain)) return;
expect(this.currentTest.title).to.be.a('string');
const screenshotData = await browser.takeScreenshot();
fs.writeFileSync(`./screenshots/${new Date().getTime()}-${this.currentTest.title.replaceAll(' ', '_')}.png`, screenshotData, 'base64');
});
async function clearCache() {
await browser.manage().deleteAllCookies();
await browser.quit();
browser = null;
const chromeOptions = new Options().windowSize({ width: 1280, height: 1024 });
if (process.env.CI) chromeOptions.addArguments('no-sandbox', 'disable-dev-shm-usage', 'headless');
chromeOptions.addArguments(`--user-data-dir=${await fs.promises.mkdtemp('/tmp/test-')}`); // --profile-directory=Default
browser = new Builder().forBrowser('chrome').setChromeOptions(chromeOptions).build();
}
function getAppInfo() {
let inspect = JSON.parse(execSync('cloudron inspect'));
app = inspect.apps.filter(function (a) { return a.location.indexOf(LOCATION) === 0; })[0];
@ -128,11 +152,8 @@ describe('Application life cycle test', function () {
if (error) return; // sometimes it doesn't appear, maybe it's cached in local storage
await browser.findElement(By.xpath('//a[contains(text(), "Configure my instance")]')).click(); // this opens a new window
const say_cmd = await getOS() == "Darwin" ? "say" : "spd-say";
execSync(`${say_cmd} "Close the newly opened configuration tab."`);
const rl = readlinePromises.createInterface({ input: process.stdin, output: process.stdout });
await rl.question('Is tab closed? ');
await browser.sleep(2000);
await closeTab();
}
async function uploadVideo() {
@ -156,6 +177,13 @@ describe('Application life cycle test', function () {
await waitForElement(By.xpath('//a[contains(@title, "Cloudron Test Video")]'));
}
async function closeTab() {
const handles = await browser.getAllWindowHandles();
await browser.switchTo().window(handles[1]);
await browser.close();
await browser.switchTo().window(handles[0]);
}
xit('build app', function () { execSync('cloudron build', EXEC_ARGS); });
it('install app', async function () {
execSync('cloudron install --location ' + LOCATION, EXEC_ARGS);