diff --git a/test/test.js b/test/test.js index bce3f9f..8f9a2ff 100755 --- a/test/test.js +++ b/test/test.js @@ -1,24 +1,20 @@ #!/usr/bin/env node -/* jslint node:true */ -/* global it:false */ -/* global xit:false */ -/* global describe:false */ -/* global before:false */ -/* global after:false */ +/* jshint esversion: 8 */ +/* global describe */ +/* global before */ +/* global after */ +/* global it */ 'use strict'; require('chromedriver'); -const execSync = require('child_process').execSync, +var execSync = require('child_process').execSync, expect = require('expect.js'), - path = require('path'); - -var By = require('selenium-webdriver').By, - until = require('selenium-webdriver').until, - Key = require('selenium-webdriver').Key, - Builder = require('selenium-webdriver').Builder; + path = require('path'), + { Builder, By, Key, until } = require('selenium-webdriver'), + { Options } = require('selenium-webdriver/chrome'); if (!process.env.USERNAME || !process.env.PASSWORD) { console.log('USERNAME and PASSWORD env vars need to be set'); @@ -28,65 +24,53 @@ if (!process.env.USERNAME || !process.env.PASSWORD) { describe('Application life cycle test', function () { this.timeout(0); - var server, browser = new Builder().forBrowser('chrome').build(); + const LOCATION = 'test'; + const EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }; + const TEST_TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 50000; + + var browser; var username = process.env.USERNAME; var password = process.env.PASSWORD; - var LOCATION = 'test'; - var EXEC_ARGS = { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }; - var TIMEOUT = parseInt(process.env.TIMEOUT, 10) || 50000; - var app; + before(function () { + const options = new Options().windowSize({ width: 1280, height: 1024 }); + if (process.env.HEADLESS) options.addArguments('headless'); + + browser = new Builder().forBrowser('chrome').setChromeOptions(options).build(); + }); + + after(function () { + browser.quit(); + }); + + async function waitForElement(elem) { + await browser.wait(until.elementLocated(elem), TEST_TIMEOUT); + await browser.wait(until.elementIsVisible(browser.findElement(elem)), TEST_TIMEOUT); + } + function getAppInfo() { var inspect = JSON.parse(execSync('cloudron inspect')); - app = inspect.apps.filter(function (a) { return a.location === LOCATION || a.location === LOCATION + '2'; })[0]; + app = inspect.apps.filter(function (a) { return a.location.indexOf(LOCATION) === 0; })[0]; expect(app).to.be.an('object'); } - function login(done) { - browser.get(`https://${app.fqdn}/login`).then(function () { - return browser.wait(until.elementLocated(By.xpath('//input[@name="username"]')), TIMEOUT); - }).then(function () { - return browser.findElement(By.xpath('//input[@name="username"]')).sendKeys(username); - }).then(function () { - return browser.findElement(By.xpath('//input[@name="password"]')).sendKeys(password); - }).then(function () { - return browser.findElement(By.tagName('form')).submit(); - }).then(function () { - return browser.wait(until.elementLocated(By.xpath('//a[text()="Alerts"]')), TIMEOUT); - }).then(function () { - done(); - }); + async function login() { + await browser.get(`https://${app.fqdn}/login`); + await waitForElement(By.xpath('//input[@name="username"]')); + await browser.findElement(By.xpath('//input[@name="username"]')).sendKeys(username); + await browser.findElement(By.xpath('//input[@name="password"]')).sendKeys(password); + await browser.findElement(By.id('login')).click(); + await waitForElement(By.xpath('//a[text()="Alerts"]')); } - function logout(done) { - browser.get(`https://${app.fqdn}/logout`).then(function () { - return browser.wait(until.elementLocated(By.xpath('//input[@name="username"]')), TIMEOUT); - }).then(function () { - done(); - }); + async function logout() { + await browser.get(`https://${app.fqdn}/logout`); + await waitForElement(By.xpath('//input[@name="username"]')); } - before(function (done) { - if (!process.env.PASSWORD) return done(new Error('PASSWORD env var not set')); - - var seleniumJar= require('selenium-server-standalone-jar'); - var SeleniumServer = require('selenium-webdriver/remote').SeleniumServer; - server = new SeleniumServer(seleniumJar.path, { port: 4444 }); - server.start(); - - done(); - }); - - after(function (done) { - browser.quit(); - server.stop(); - done(); - }); - xit('build app', function () { execSync('cloudron build', EXEC_ARGS); }); - it('install app', function () { execSync(`cloudron install --location ${LOCATION}`, EXEC_ARGS); }); it('can get app information', getAppInfo); @@ -100,12 +84,11 @@ describe('Application life cycle test', function () { it('backup app', function () { execSync(`cloudron backup create --app ${app.id}`, EXEC_ARGS); }); it('restore app', function () { - const backups = JSON.parse(execSync('cloudron backup list --raw')); - execSync('cloudron uninstall --app ' + app.id, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); - execSync('cloudron install --location ' + LOCATION, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); - var inspect = JSON.parse(execSync('cloudron inspect')); - app = inspect.apps.filter(function (a) { return a.location === LOCATION; })[0]; - execSync(`cloudron restore --backup ${backups[0].id} --app ${app.id}`, { cwd: path.resolve(__dirname, '..'), stdio: 'inherit' }); + const backups = JSON.parse(execSync(`cloudron backup list --raw --app ${app.id}`)); + execSync('cloudron uninstall --app ' + app.id, EXEC_ARGS); + execSync('cloudron install --location ' + LOCATION, EXEC_ARGS); + getAppInfo(); + execSync(`cloudron restore --backup ${backups[0].id} --app ${app.id}`, EXEC_ARGS); }); it('can login', login); @@ -113,6 +96,7 @@ describe('Application life cycle test', function () { it('move to different location', function () { execSync(`cloudron configure --location ${LOCATION}2 --app ${app.id}`, EXEC_ARGS); }); it('can get app information', getAppInfo); + it('can login', login); it('can logout', logout);