From dcb6337f1bc750e274fa463a1f54763a9795a3aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20R=C3=B8sdal?= Date: Fri, 24 Apr 2015 20:55:39 +0200 Subject: [PATCH] Add automatic Javascript testing of Freeciv-web using CasperJS and SlimerJS. --- scripts/freeciv-web-travis.sh | 22 ++++- scripts/freeciv-web-vagrant.sh | 22 ++++- tests/freeciv-web-tests.js | 166 +++++++++++++++++++++++++++++++++ 3 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 tests/freeciv-web-tests.js diff --git a/scripts/freeciv-web-travis.sh b/scripts/freeciv-web-travis.sh index 0b9118f61..eef491ccf 100644 --- a/scripts/freeciv-web-travis.sh +++ b/scripts/freeciv-web-travis.sh @@ -31,9 +31,12 @@ resin_version="4.0.40" resin_url="http://www.caucho.com/download/resin-${resin_version}.tar.gz" tornado_url="https://pypi.python.org/packages/source/t/tornado/tornado-4.1.tar.gz" jansson_url="http://www.digip.org/jansson/releases/jansson-2.7.tar.bz2" +slimerjs_url="https://github.com/laurentj/slimerjs/archive/master.zip" +casperjs_url="https://github.com/n1k0/casperjs/archive/1.1-beta3.zip" + # Based on fresh install of Ubuntu 12.04 -dependencies="maven mysql-server-5.5 openjdk-7-jdk libcurl4-openssl-dev nginx subversion pngcrush libtool automake autoconf autotools-dev language-pack-en python3-setuptools libglib2.0-dev python3.2 python3.2-dev imagemagick liblzma-dev" +dependencies="maven mysql-server-5.5 openjdk-7-jdk libcurl4-openssl-dev nginx subversion pngcrush libtool automake autoconf autotools-dev language-pack-en python3-setuptools libglib2.0-dev python3.2 python3.2-dev imagemagick liblzma-dev firefox xvfb" ## dependencies echo "==== Installing Updates and Dependencies ====" @@ -99,9 +102,26 @@ cd ${basedir}/scripts && ./sync-js-hand.sh cd ${basedir}/freeciv && rm -rf freeciv cd ${basedir}/freeciv-web && ./build.sh +echo "============================================" +echo "Installing SlimerJS and CasperJS for testing" +export SLIMERJSLAUNCHER=/usr/bin/firefox +export SLIMERJS_EXECUTABLE=/vagrant/tests/slimerjs-master/src/slimerjs +cd ${basedir}/tests +wget ${slimerjs_url} +unzip master.zip +wget ${casperjs_url} +unzip 1.1-beta3.zip + + echo "Starting Freeciv-web..." service nginx start cd ${basedir}/scripts/ && sudo -u travis ./start-freeciv-web.sh + + +echo "Start testing of Freeciv-web using CasperJS:" +cd ${basedir}/tests/casperjs-1.1-beta3/bin +xvfb-run ./casperjs --engine=slimerjs test /vagrant/tests/freeciv-web-tests.js || (>&2 echo "Freeciv-web CasperJS tests failed!" && exit 1) + echo "==============================" echo "Freeciv-web built and started correctly: Build successful!" diff --git a/scripts/freeciv-web-vagrant.sh b/scripts/freeciv-web-vagrant.sh index 31fae8c22..0ce0c3f87 100755 --- a/scripts/freeciv-web-vagrant.sh +++ b/scripts/freeciv-web-vagrant.sh @@ -39,9 +39,11 @@ mysql_pass="vagrant" resin_version="4.0.41" resin_url="http://www.caucho.com/download/resin-${resin_version}.tar.gz" tornado_url="https://pypi.python.org/packages/source/t/tornado/tornado-4.1.tar.gz" +slimerjs_url="https://github.com/laurentj/slimerjs/archive/master.zip" +casperjs_url="https://github.com/n1k0/casperjs/archive/1.1-beta3.zip" # Based on fresh install of Ubuntu 12.04 -dependencies="maven mysql-server openjdk-7-jdk libcurl4-openssl-dev nginx libjansson-dev subversion pngcrush python3-pillow libtool automake autoconf autotools-dev language-pack-en python3.4-dev python3-setuptools libglib2.0-dev libbz2-dev imagemagick python3-pip dos2unix liblzma-dev" +dependencies="maven mysql-server openjdk-7-jdk libcurl4-openssl-dev nginx libjansson-dev subversion pngcrush python3-pillow libtool automake autoconf autotools-dev language-pack-en python3.4-dev python3-setuptools libglib2.0-dev libbz2-dev imagemagick python3-pip dos2unix liblzma-dev firefox xvfb" ## Setup mkdir -p ${basedir} @@ -96,6 +98,16 @@ cd ${basedir}/scripts/freeciv-img-extract/ && ./setup_links.sh && ./sync.sh cd ${basedir}/scripts && ./sync-js-hand.sh cd ${basedir}/freeciv-web && sudo -u vagrant ./setup.sh +echo "============================================" +echo "Installing SlimerJS and CasperJS for testing" +export SLIMERJSLAUNCHER=/usr/bin/firefox +export SLIMERJS_EXECUTABLE=/vagrant/tests/slimerjs-master/src/slimerjs +cd ${basedir}/tests +wget ${slimerjs_url} +unzip master.zip +wget ${casperjs_url} +unzip 1.1-beta3.zip + echo "==============================" service nginx stop @@ -110,7 +122,13 @@ if [ -d "/vagrant/" ]; then echo "Starting Freeciv-web..." service nginx start cd ${basedir}/scripts/ && sudo -u vagrant ./start-freeciv-web.sh - echo "Freeciv-web started! Now try http://localhost/ on your host operating system." else echo "Freeciv-web installed. Please start it manually." fi + +echo "Start testing of Freeciv-web using CasperJS:" +cd ${basedir}/tests/casperjs-1.1-beta3/bin +xvfb-run ./casperjs --engine=slimerjs test /vagrant/tests/freeciv-web-tests.js || (>&2 echo "Freeciv-web CasperJS tests failed!" && exit 1) + +echo "Freeciv-web started! Now try http://localhost/ on your host operating system." + diff --git a/tests/freeciv-web-tests.js b/tests/freeciv-web-tests.js new file mode 100644 index 000000000..8352a726d --- /dev/null +++ b/tests/freeciv-web-tests.js @@ -0,0 +1,166 @@ +/********************************************************************** + CasperJS tests for Freeciv-web. +***********************************************************************/ + + +casper.test.begin('Test of Resin running on localhost port 8080.', 2, function suite(test) { + casper.start("http://localhost:8080/", function() { + test.assertHttpStatus(200); + test.assertTitleMatch(/Freeciv-web/, 'Freeciv-web title is present'); + }); + + casper.run(function() { + test.done(); + }); +}); + + +casper.test.begin('Test of Freeciv-proxy on port 7001.', 2, function suite(test) { + casper.start("http://localhost:7001/status", function() { + test.assertHttpStatus(200); + test.assertTextExists('Freeciv WebSocket Proxy Status', + 'Test that Freeciv-proxy contains expected text.'); + }); + + casper.run(function() { + test.done(); + }); +}); + + +casper.test.begin('Test of Freeciv-web frontpage on localhost port 80 (nginx).', 3, function suite(test) { + casper.start("http://localhost", function() { + test.assertHttpStatus(200); + test.assertTitleMatch(/Freeciv-web/, 'Freeciv-web title is present'); + test.assertExists('#single-button'); + }); + + casper.run(function() { + test.done(); + }); +}); + +casper.test.begin('Test that Metaserver is responding.', 2, function suite(test) { + casper.start("http://localhost/meta/metaserver.php", function() { + test.assertHttpStatus(200); + test.assertTextExists('Freeciv-web Single-player games', + 'Test that Metaserver contains expected text.'); + }); + + casper.run(function() { + test.done(); + }); +}); + + +casper.test.begin('Test of Freeciv-web frontpage', 3, function suite(test) { + casper.start("http://localhost", function() { + test.assertHttpStatus(200); + test.assertTitleMatch(/Freeciv-web/, 'Freeciv-web title is present'); + test.assertExists('#single-button'); + }); + + casper.run(function() { + test.done(); + }); +}); + +casper.test.begin('Test starting new Freeciv-web game', 10, function suite(test) { + casper.start("http://localhost/webclient/?action=new", function() { + test.assertHttpStatus(200); + test.assertTitleMatch(/Freeciv-web/, 'Freeciv-web title is present'); + test.assertExists('#username_req'); + this.echo("Captured screenshot to be saved as screenshot-1.png"); + this.capture('../../screenshot-1.png', undefined, { + format: 'png', + quality: 100 + }); + + }); + + casper.then(function() { + this.echo("Filling in username in new game dialog."); + this.sendKeys('#username_req', 'CasperJS'); + }); + + casper.thenEvaluate(function() { + /* Starting new game automatically from Javascript.*/ + if (validate_username()) { + $("#dialog").dialog('close'); + setTimeout("pregame_start_game();", 700); + } + }); + + casper.waitForText("Ok", function() { + this.echo("Clicking Ok in Intro dialog."); + this.clickLabel('Ok'); + this.echo("Captured screenshot to be saved as screenshot-2.png"); + this.capture('../../screenshot-2.png', undefined, { + format: 'png', + quality: 100 + }); + }); + + casper.then(function() { + this.echo("Checking that JavaScript objects in browser memory are as expected."); + + test.assertEval(function() { + return tileset['u.settlers'].length == 5 && tileset['f.shield.england'].length == 5; + }, "Checks that tileset contains settlers and english flag."); + + test.assertEval(function() { + return map['xsize'] > 0 + && map['ysize'] > 0 + && tiles[5]['x'] >= 0 + && tiles[5]['y'] >= 0 + && tiles[5]['terrain'] != null; + }, "Checks properties of map and tiles."); + + + test.assertEval(function() { + return techs[1] != null + && techs[1]['name'] != null + && techs[1]['name'].length > 0 + && techs[1]['num_reqs'] > 0 + && techs[1]['req'].length == 2 + && techs[1]['req'][0] > 0 + && techs[1]['req'][1] > 0; + }, "Checks some properties of the tech object."); + + test.assertEval(function() { + return players[0] != null && players[0]['name'].length > 0 + && players[0]['username'].length > 0 + && players[0]['playerno'] >= 0 + && nations[players[0]['nation']] + && players[0]['love'].length > 0 + && players[0]['is_ready'] == true + }, "Checks some properties of the player object."); + + test.assertEval(function() { + return game_info['turn'] == 0 + && game_info['year'] == -4000 + && game_info['timeout'] == 0 + && game_info['gold'] > 0 + && game_info['aifill'] > 0 + && game_info['mapsize'] > 0; + }, "Checks some properties of the game_info object."); + + test.assertEval(function() { + return unit_types[0]['name'].length > 0 + && unit_types[0]['helptext'].length > 0 + && unit_types[0]['graphic_str'].length > 0 + }, "Checks some properties of the unit_types object."); + + + test.assertEval(function() { + return nations[0]['adjective'].length > 0 + && nations[0]['graphic_str'].length > 0; + }, "Checks some properties of the nations object."); + + }); + + casper.run(function() { + this.echo("Tests are run successfully!"); + test.done(); + }); +});