function ExampleTestCase() { /* an example unit test */ this.name = 'ExampleTestCase'; // this method will be executed before *every* test // method, there's also a tearDown counterpart that // is not used in this example this.setUp = function() { function Foo() { this.returnfoo = function() { return 'foo'; }; this.throwfoo = function() { throw('foo'); }; }; this.foo = new Foo(); }; // an example of a successful assertEquals call this.testAssertEquals = function() { this.assertEquals(this.foo.returnfoo(), 'foo'); }; // an example of a successful assertThrows call (to // test whether some call results in an exception) this.testAssertThrows = function() { this.assertThrows(this.foo.throwfoo, 'foo'); }; }; // each test case has to subclass from TestCase, which provides // the .assert* methods and some stubs ExampleTestCase.prototype = new TestCase;
// a simple test function, assumes there's a cgi script called 'square.cgi' in // the root of the proxy which returns 4 if a POST body 'int=16' is inputted, // if this fails, an error is reported and the next test is called function test_square_root(testcase, status, headers, body) { testcase.assertEquals(status, '200'); testcase.assertEquals(headers['content-type'].substr(0, 10), 'text/plain'); // note that string.strip() is a function of the 'jsbase' library testcase.assertEquals(string.strip(body), '4'); }; // register the function so it can be ran later window.httpunit_test_registry.push([ test_square_root, // test function '/square_root.cgi', // url to test 'POST', // method {'Content-Type': 'application/x-www-form-urlencoded'}, // headers 'int=16' // body ]);
function test_sync(at, browser) { /* simple test of a plain HTML document */ // see whether the title is what we expect; if this fails (or // any other at.assert* call), execution of the test chain // stops entirely at.assertEquals(browser.title, 'Test document 1'); // also test the content of the 'body' element at.assertEquals( string.strip( browser.document.getElementsByTagName( 'body' )[0].childNodes[0].nodeValue ), 'Test document content' ); // in the end we have to make sure we send the browser to the // next location, either by clicking a button or by setting a new // location (unless this is the last test, in that case nothing // special needs to be done) browser.navigateTo('apptestdata/test_async.html'); }; window.app_test_registry.push(['apptestdata/test_sync.html', test_sync]);
// simple polling loop that continues testing var poll_and_continue = function(at, browser, callback, orgtext) { var newtext = browser.getText( browser.document.getElementById('container') ); // check if the text has been updated yet if (newtext == orgtext) { // not yet updated, poll misclib.schedule(this, poll_and_continue, 100, at, browser, callback, orgtext); return; }; at.assertEquals(string.strip(newtext), 'Text loaded'); callback(); }; function test_async(at, browser, callback) { /* we have a document with a div that is filled with data, using XMLHttpRequest, after pressing some button... */ // first we store the original text var orgtext = browser.getText( browser.document.getElementById('container') ); // see whether it's the text we expect at.assertEquals( string.strip(orgtext), 'Nothing to see here, please move along.' ); // click the button that starts the XHR text loading browser.getElementsByText('load')[0].click(); // start the polling and continue testing there poll_and_continue(at, browser, callback, orgtext); // tell the framework to wait until the callback is called return at.CONTINUE_ASYNC; }; window.app_test_registry.push(['apptestdata/test_async.html', test_async]);
johnny@medusa:~/hypertest$ ./run_browsertest
Going to test browser: epiphany
TestEcmaUnitTestCase (testecmaunit.js, js test)
TestTestCase - testAssert: OK
TestTestCase - testAssertEquals: OK
TestTestCase - testAssertNotEquals: OK
TestTestCase - testAssertTrue: OK
TestTestCase - testAssertFalse: OK
TestTestCase - testAssertThrows: OK
TestTestCase2 - testAssert: OK
TestTestCase2 - testAssertEquals: OK
TestTestCase2 - testAssertNotEquals: OK
TestTestCase2 - testAssertTrue: OK
TestTestCase2 - testAssertFalse: OK
TestTestCase2 - testAssertThrows: OK
time spent: 8 msecs
TestHTTPUnitTestCase (testhttpunittests.js, http test)
TestHTTPUnitTestCase - test_square_root: OK
time spent: 75 msecs
TestAppTestTestCase (testapptests.js, app test)
TestAppTestTestCase - apptestdata/test_sync.html: OK
TestAppTestTestCase - apptestdata/test_async.html: OK
time spent: 439 msecs
johnny@medusa:~/hypertest$