49

I've never used Selenium but I guess it's for simulating user interaction in all browsers.

That's like integration tests.

But how do you test your js libraries/frameworks (unit testing) on all the browsers in an automated way?

ajsie
  • 77,632
  • 106
  • 276
  • 381
  • 1
    Similar to http://stackoverflow.com/questions/5028926/run-selenium-tests-in-multiple-browsers-one-after-another-from-c-nunit – Edgar Jun 02 '11 at 16:11

14 Answers14

29

For unit-testing you can try http://code.google.com/p/js-test-driver/

JsTestDriver consist of a single JAR file which contains everything you need to get started. For in depth discussion of command line option see GettingStarted.

Here is an overview of how JsTestDriver works at runtime...

gnat
  • 6,213
  • 108
  • 53
  • 73
Olegas
  • 10,349
  • 8
  • 51
  • 72
  • 1
    You can automate testing on all browsers installed on your machine with js-test-driver. – JoshNaro Apr 26 '11 at 20:54
  • 2
    Not even on your machine, but even on any other machine in your local network. You can connect different browsers from different OS'es to js-test-driver server and run tests in all of them. – Olegas Apr 27 '11 at 06:48
13

You can have a look at TestSwarm:

project that I’m working on: TestSwarm...

Its construction is very simple. It’s a dumb JavaScript client that continually pings a central server looking for more tests to run. The server collects test suites and sends them out to the respective clients.

All the test suites are collected. For example, 1 “commit” can have 10 test suites associated with it (and be distributed to a selection of browsers)...

The nice thing about this construction is that it’s able to work in a fault-tolerant manner. Clients can come-and-go. At any given time there might be no Firefox 2s connected, at another time there could be thirty. The jobs are queued and divvied out as the load requires it. Additionally, the client is simple enough to be able to run on mobile devices (while being completely test framework agnostic)...

gnat
  • 6,213
  • 108
  • 53
  • 73
9ikhan
  • 1,177
  • 3
  • 11
  • 22
  • 2
    Above link is broken. Here is the correct link: http://swarm.jquery.org/ However this is still an Alpha product, as per their Wiki: **NOTE: TestSwarm is currently in an Alpha state. Critical problems may arise during the alpha tests that result in lost data, disconnected clients, and other un-desirable effects. Please keep this in mind while participating in the alpha test.** – anubhava May 31 '11 at 13:00
  • TestSwarm is currently non-functional – JD. Sep 11 '15 at 22:07
7

As the referenced post in a previous article suggests, you could use js-test-driver. Its specifically for JavaScript unit testing across multiple browsers, exactly what you want. I have messed around with it and it is pretty good. I haven't done any serious commercial testing in it though.

mark-cs
  • 4,677
  • 24
  • 32
  • It uses its own testing framework I believe. So I assume I can't use YUI Test on it right? – ajsie Apr 23 '11 at 12:22
  • @weng I believe they are compatible, I haven't tried it myself but if you google js-test-driver and YUI you should find some tutorials on how to run them together, sorry I can't be of more help on the YUI side. – mark-cs Apr 23 '11 at 13:48
  • @weng This may be of help http://gorn.ch/archive/2009/12/08/yui3-test-and-jstestdriver.html – mark-cs Apr 24 '11 at 09:24
7

The best one imo is the one from YUI : http://developer.yahoo.com/yui/3/test/ But doing unit-testing in every browser is kind of hard... Most people just test with it during development and just use node.js to test later on in case they broke something.

xavierm02
  • 8,457
  • 1
  • 20
  • 24
  • 1
    For those wondering what yeti is: [ http://www.yuiblog.com/blog/2010/08/25/introducing-yeti-the-yui-easy-testing-interface/ ] – jamiebarrow Aug 25 '11 at 12:13
6

For unit testing you should look at solutions that do not load up a browser to do the tests.

You can look at something like RhinoUnit for that - http://code.google.com/p/rhinounit

Also have a look at Dojo Object Harness (DOH) unit test framework http://dojotoolkit.org/reference-guide/util/doh.html

Look at a similar question here which can give you an idea on how to TDD js - JavaScript unit test tools for TDD

Community
  • 1
  • 1
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • But I have to know that the js library im writing will work on all major browsers! It seems to be very important to test the js code directly on the browsers. – ajsie Apr 21 '11 at 05:34
  • That's why I gave two different frameworks and also a link to a related question. Decide how you want to test. – manojlds Apr 21 '11 at 05:37
6

It's no longer actively maintained, but I've still been happy with JSUnit for Javascript unit testing: https://github.com/pivotal/jsunit

It includes both an HTML/Javascript framework you can run in the browser, and a java-based test runner that you can invoke from ant.

Rob W
  • 341,306
  • 83
  • 791
  • 678
nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
  • Agreed, in general - but it is quite stable, so active maintenance may be less important than in other contexts. – nrabinowitz Apr 21 '11 at 16:10
5

To test multiple browsers in parallel you would Selenium Grid. Please take a look here: http://selenium-grid.seleniumhq.org/step_by_step_installation_instructions_for_windows.html for step by step instructions on how to use it.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I dont know if I have misunderstood the usage of Selenium or if you misunderstood my question. Is Selenium capable of running unit tests (not click and see if text is on the page)? – ajsie Apr 21 '11 at 05:20
  • I read your unit testing clause but also read `on all the browsers` clause as well. Selenium is good for UI testing so if you are not into UI testing then please ignore my answer. – anubhava Apr 21 '11 at 05:43
5

I haven't used it so far, and it is still in beta, but FuncUnit declares itself as "A functional test suite based of qUnit, Selenium and jQuery".

There is also an infographic explaining how it works

Maybe it is something what you want? The github repo seems to be quite active.

knb
  • 9,138
  • 4
  • 58
  • 85
4

You should check http://saucelabs.com its a cloud base selenium environment that allow you to build your test and then upload them, run them in as many browsers as you want.

Siedrix
  • 641
  • 4
  • 10
  • But that is still not testing my javascript libraries/frameworks. Just end user web applications. – ajsie May 27 '11 at 16:23
4

You should consider the capabilities offered by crossbrowsertesting.com (It is not for free). For js testing try JsUnit (http://www.jsunit.net/).

Quoted from its homepage

JsUnit is a Unit Testing framework for client-side (in-browser) JavaScript. It is essentially a port of JUnit to JavaScript. Also included is a platform for automating the execution of tests on multiple browsers and mutiple machines running different OSs.

Luixv
  • 8,590
  • 21
  • 84
  • 121
3

As people have already said so, you should use JsTestDriver. It has it's own test system, but you can use other test libraries with it, for example Jasmine ( http://pivotal.github.com/jasmine/ ). You can find a list of adaptors for JsTestDriver here: http://code.google.com/p/js-test-driver/wiki/XUnitCompatibility

Mutahhir
  • 3,812
  • 3
  • 21
  • 26
3

I'd go for QUnit, which is what jQuery uses. I've ran it on lots of desktop browsers as well as iPhones and Android phones.

There's some great tutorials and it can be integrated easily with things like js-test-driver. QUnit is modeled after JUnit and all the other xUnit testing frameworks (like PHPUnit) so it's easy to pick up the API.

The basic syntax is as follows:

test("my first test", function() {
  var str = "hello";
  equals( "hello", str, "Should be hello" );
});

It also looks quite nice:

QUnit screenshot

dave1010
  • 15,135
  • 7
  • 67
  • 64
2

There are few companies that specialize in cross browser testing.

http://browserling.com/

http://www.browserstack.com/

http://saucelabs.com/ (already mentioned here)

http://browsershots.org/

http://www.browsercam.com/

http://browserseal.com/

Use whatever testing lib you want.

esamatti
  • 18,293
  • 11
  • 75
  • 82
1

Selenium or SauceLabs etc are not unit testing. They are functional/integration testing solutions.

You need to abstract your external usages like DOM to unit test javascript.

Write your tests so that they can use any external library like jquery by configuration. So that, you can unit test your logic without touching any externality and you can also both test cross-browser testing.

Inanc Gumus
  • 25,195
  • 9
  • 85
  • 101