JsUnit An introduction to unit testing in-browser JavaScript with JsUnit Edward Hieatt February, 2005 JsUnit.

Slides:



Advertisements
Similar presentations
Essentials for Design JavaScript Level One Michael Brooks
Advertisements

J-Unit Framework.
Pubman and Selenium tests. What is Selenium Selenium is a suite of Web application test automation tools for any browser on any operating system –Firefox,
The Web Warrior Guide to Web Design Technologies
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Remote Method Invocation
Tutorial 10 Programming with JavaScript
Week 2 IBS 685. Static Page Architecture The user requests the page by typing a URL in a browser The Browser requests the page from the Web Server The.
Introduction to Web Interface Technology (CSE2030)
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
How Clients and Servers Work Together. Objectives Learn about the interaction of clients and servers Explore the features and functions of Web servers.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
Slide 1 of 9 Presenting 24x7 Scheduler The art of computer automation Press PageDown key or click to advance.
Automation using Selenium Authored & Presented by : Chinmay Sathe & Amit Prabhu Cybage Software Pvt. Ltd.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
Website Development & Management Introduction & Overview CIT Fall Instructor: John Seydel, Ph.D.
JavaScript Form Validation
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
_______________________________________________________________________________________________________________ E-Commerce: Fundamentals and Applications1.
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
JavaScript: Control Structures September 27, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Basics of Web Databases With the advent of Web database technology, Web pages are no longer static, but dynamic with connection to a back-end database.
Tutorial 10 Adding Spry Elements and Database Functionality Dreamweaver CS3 Tutorial 101.
© 2012 LogiGear Corporation. All Rights Reserved Robot framework.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
JSP Java Server Pages Softsmith Infotech.
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
Tutorial 10 Programming with JavaScript. XP Objectives Learn the history of JavaScript Create a script element Understand basic JavaScript syntax Write.
Tutorial 10 Programming with JavaScript
Done by: Hanadi Muhsen1 Tutorial 1.  Learn the history of JavaScript  Create a script element  Write text to a Web page with JavaScript  Understand.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Introduction to JavaScript 41 Introduction to Programming the WWW I CMSC Winter 2004 Lecture 17.
Active Server Pages  In this chapter, you will learn:  How browsers and servers interacted on the Internet when the Internet first became popular 
JavaScript - A Web Script Language Fred Durao
Web Automation Testing With Selenium By Rajesh Kanade.
Where does PHP code get executed?. Where does JavaScript get executed?
Pubman and Selenium tests. What is Selenium Selenium is a suite of Web application test automation tools for any browser on any operating system –Firefox,
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Introduction to JavaScript CS101 Introduction to Computing.
A tool for test-driven development
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Introduction Selenium IDE is a Firefox extension that allows you to record, edit, and debug tests for HTML Easy record and playback Intelligent field selection.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
Introduction to HTML. _______________________________________________________________________________________________________________ 2 Outline Key issues.
SPI NIGHTLIES Alex Hodgkins. SPI nightlies  Build and test various software projects each night  Provide a nightlies summary page that displays all.
Selenium server By, Kartikeya Rastogi Mayur Sapre Mosheca. R
Tutorial 10 Programming with JavaScript. 2New Perspectives on HTML, XHTML, and XML, Comprehensive, 3rd Edition Objectives Learn the history of JavaScript.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
JavaScript and AJAX 2nd Edition Tutorial 1 Programming with JavaScript.
Web Services Essentials. What is a web service? web service: software functionality that can be invoked through the internet using common protocols like.
Automated Testing with PHPUnit. How do you know your code works?
Arklio Studija 2007 File: / / Page 1 Automated web application testing using Selenium
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
JavaScript and Ajax (Ajax Tutorial)
Introduction to JUnit CS 4501 / 6501 Software Testing
Remote Method Invocation
Testing With Selenium - Open source Test Automation Tool
PHP / MySQL Introduction
Database Driven Websites
PHP.
Web Page Concept and Design :
Lecture 34: Testing II April 24, 2017 Selenium testing script 7/7/2019
Presentation transcript:

JsUnit An introduction to unit testing in-browser JavaScript with JsUnit Edward Hieatt February, 2005 JsUnit version 2.1 Note: this presentation assumes the reader/audience is familiar with basic unit testing concepts

JsUnit: Part 1 Writing and manually running JavaScript Tests with the XUnit framework for in-browser JavaScript

What is an XUnit framework? A standardized set of language-independent concepts and constructs for writing and running unit tests There is an XUnit framework for virtually every language Examples are JUnit for Java, CppUnit for C++, PHPUnit for PHP, UTPL/SQL for Oracle SQL Traditionally open-source All support certain constructs: –TestCase and TestSuite are well-defined entities –The assertion syntax follows rough standards, e.g. assertEquals(comment, expected, actual) All provide a TestRunner program to run tests Listed at

JsUnit: background The XUnit framework for JavaScript Open source (hosted on Sourceforge) Preserves the standards of a typical XUnit framework Written in 100% JavaScript Runs on “most” browser/platform combinations ~4 years old ~10,000 downloads and ~275 members of the news group

JsUnit: conforming to XUnit Unit tests in JsUnit are called Test Functions Test Functions live in an HTML page called a Test Page A Test Page is any HTML page that has a JavaScript “include” of jsUnitCore.js jsUnitCore.js provides the assertion functions of JsUnit, e.g. assertEquals(comment, arg1, arg2) JsUnit supports setUp() and tearDown() A Test Suite Page declares a suite() function that returns a JsUnitTestSuite for grouping Test Pages The JsUnit testRunner.html page runs Test Pages The TestRunner HTML page can be run from a file server or a web server

JUnit JsUnit JsUnit Test Functions Test Pages (HTML) Include jsunit/app/jsUnitCore.js Test Suite Pages (HTML) Pure HTML/JS TestRunner function setUp(), tearDown() Runs in the target browser(s) Pure JavaScript JUnit Test Methods Test Classes Extend junit.framework.TestCase junit.framework.TestSuite AWT/Swing/IDE TestRunners public void setUp(), tearDown() Runs in the target Java VM Pure Java

Test Page Example 1: Testing some simple math (code in Test Page) function multiplyAndAddFive(arg1, arg2) { return arg1*arg2+5; } function testWithValidArguments() { assertEquals("2 times 3 plus 5 is 11", 11, multiplyAndAddFive(2, 3)); assertEquals("Should work with negative numbers", -15, multiplyAndAddFive(-4, 5)); } function testWithInvalidArguments() { assertNull("null argument", multiplyAndAddFive(2, null)); assertNull("string argument", multiplyAndAddFive(2, "a string")); assertNull("undefined argument", multiplyAndAddFive(2, JSUNIT_UNDEFINED_VALUE)); } function testStrictReturnType() { assertNotEquals("Should return a number, not a string", "11", multiplyAndAddFive(2, 3)); } This is a Test Page for multiplyAndAddFive(arg1, arg2).

Test Page example 2: Testing form field/DOM interaction (code in.js file) //contains a “validate” function function setUp() { document.myForm.field1.value=“foo”; } function testInitialConditions() { assertEquals(“”, document.getElementById(“resultMessage”).innerHTML); } function testPushOK() { pushOKButton(); assertEquals(“No value in field 2”, document.getElementById(“resultMessage”).innerHTML); } function testFillingInField2() { document.myForm.field2.value=“bar”; pushOKButton(); assertEquals(“Good values”, document.getElementById(“resultMessage”).innerHTML); } function tearDown() { document.myForm.field1.value=“”; document.myForm.field2.value=“”; } function pushOKButton() { validate(); }

JsUnit: the TestRunner

JsUnit Tracing (debugging output) JsUnit includes certain functions that output “traces” to a debugging window Tracing is a way to view output without using JavaScript “alerts” Tracing is an alternative to a debugger if one is not available for a certain browser/platform There are 3 trace functions: –debug(message) (level 3) –warn(message) (level 2) –inform(message) (level 1) For a particular run of the tests, you can set the trace level to any of the 3 levels Traces cascade, i.e. trace message at lower levels show up when the level is set higher

Supported Browsers/OSs MS Internet Explorer 5.5+ on: –Windows NT/2000/XP/95 –Mac OS 9, Mac OS X Gecko-based browsers on all platforms, e.g.: –Mozilla –Firefox 0.9+ –Netscape Konqueror 5+ on Linux KDE Safari 1.2+ on Mac OS X

JsUnit: the most FA’d Q JsUnit is not usually appropriate for submitting forms/other navigation that interacts with a web server: –It is intended to test purely client-side functionality – it’s a unit testing framework of the in-browser JavaScript that usually lives in.js files included by generated/static HTML pages –Testing “walking through” a web application should be done in HTTPUnit/JWebUnit –In most cases, the desire to submit a form points at the need to instead mock out the submission in order to create a true unit test. However, there is currently work in progress to support the ability for a Test Page to submit a form in a separate page that lives in a Frame/IFrame in the Test Page

JsUnit: Part 2 The automation of runs of JsUnit Test Pages/Suites on multiple local or remote browsers/OSs from an Ant script or JUnit suite (and hence in a continuous build)

The goals of automated test runs We have seen how JsUnit allows you to run your tests in almost any particular browser on most OSs It is desirable to be able to easily run your tests in all of your target browsers: –while you are writing code and tests, without having to manually start up each browser and press “Run” –as part of a regression test suite in a continuous build It is also important that the tests be run on target browsers on all the target OSs, rather than just one machine We want to get instant feedback on which test failed on which browser on which OS, and also create logs of each test run That is, we don’t want to have to get out of our seat and go to a bank of test machines running various browsers – we want visual or programmatic feedback, as we get with the green bar.

Automation on a single machine: StandaloneTest JsUnit provides a Java JUnit TestCase class called net.jsunit.StandaloneTest StandaloneTest can be invoked from the JsUnit Ant build.xml You can configure StandaloneTest with respect to: –Which browsers to run tests in (a list of paths to browser executables) –Which Test Page to run (usually it is your highest-level Test Suite Page) When run, StandaloneTest starts each browser in turn and runs the specified Test Page without any user interaction If any test fails in any of the browsers, Standalone Test fails with a failure message indicating which test failed in which browser In this way, we can repeatedly run our Test Pages on all target browser for the machine we are using with the push of a button

StandaloneTest: how it works StandaloneTest starts a lightweight Java HTTP server called the JsUnitServer Each browser is launched as an external process Certain parameters are passed in the TestRunner URL that tell the test runner to collect results in a form field and submit the form to the JsUnitServer over HTTP The JsUnitServer waits for all browsers to finish, and inspects the submitted results If any browser fails, StandaloneTest fails

Automation on multiple machines: DistributedTest StandaloneTest allows for automated runs on a single machine, but we still need to be able to automatically launch all our tests on all target OSs This means we need to be able to programmatically kick off JsUnit tests on remote machines JsUnit provides a second Java JUnit TestCase class called net.jsunit.DistributedTest DistributedTest is configured to be aware of a list of multiple remote machines When run, DistributedTest asks each remote machine in turn to run specified Tests on the browsers relevant for the respective OS Each remote machine runs the specified tests on each browser relevant to its OS If any test fails on any browser on any remote machine, DistributedTest fails with an error message indicating the failure In this way, we can repeatedly run our Test pages on all target browser/OS combinations with the push of a button

DistributedTest: how it works We start a JsUnit server on each remote server Each JsUnit server is configured with target browsers for its OS The JsUnit servers listen for requests from DistributedTest to run tests DistributedTest tells each remote JsUnit server (over HTTP) to run tests Each JsUnitServer runs StandaloneTest For each JsUnitServer, if any test fails on any browser, a failure message is sent back to DistributedTest; otherwise a success message is returned (the messages are XML strings) If any failure message is received, DistributedTest fails

Running in a continuous build StandaloneTest can be repeatedly run by a developer as he/she is writing code and tests However, StandaloneTest is a JUnit test, so it can be added to a JUnit suite just like any other test, and thereby run in a continuous build Alternatively, because JsUnit can be launched from Ant, the Ant task can be added to a continuous build (such as Anthill or Cruise Control)

StandaloneTest: Logging For each run, StandaloneTest writes a log file to disk The filename is generated (and unique) or it can be specified by a parameter in the TestRunner The log file format matches JUnit’s; the details of each Test Function are given

Sample Log File