Coder Social home page Coder Social logo

funcunit's Introduction

FuncUnit

Build Status npm version Join our Slack

Write better tests, faster.

Note: FuncUnit Roadmap

The FuncUnit Getting Started guide is a quick walkthrough of creating and running a test.

Set up a test

QUnit provides the basic structure needed to write unit or functional tests.

Module

Modules are groups of tests with setup and teardown methods that run for each test.

module("Contacts", {
  // runs before each test
  setup: function(){
    // setup code
  },
  // runs after each test
  teardown: function(){
    // cleanup code
  }
});

Test

test("findOne", function(){
  // define a test
});

Assertions

test("counter", function() {
  ok(Conctacts.first().name, "there is a name property");
  equal(Contacts.counter(), 5, "there are 5 contacts");
});

Open a page

The following uses F.open( URL ) to open autocomplete.html before every test.

module("autosuggest", {
  setup: function() {
    F.open('autosuggest.html');
  }
});

Calling open on window will cause FuncUnit commands to operate on the current window. This is also the default if you don't open any page.

Query for elements

FuncUnit tests are written just like jQuery. F is a copy of the $ method. It is used to find elements in the page you're testing. Like $, FuncUnit methods are chainable on the results of F.

// grab the #description element, wait for it to be visible, type in it
F("#description").visible().type("Test Framework");

Simulate user actions

When you're testing a widget, you need to simulate the actions that user takes. FuncUnit uses the syn library to accurately simulate the correct low level events like mouseup and keypress for high level actions like click() and type(). The following shows how to simulate common user actions.

Click

// click a button
F('#submit_button').click();

Type

// type in an input
F('#task_name').type("Learn FuncUnit");

Drag

// drag a task item to the trash area
F('.task').drag(".trash");

Wait for page conditions

After a user action, your test page's event handlers run and the page is changed. Wait commands are used to wait for some page condition before continuing.

Waits are overloaded jQuery getter methods. F.fn.text( textVal, callback ) waits for an element's $.fn.text to match the textVal.

// wait for result to show "task complete"
F("#result").text("task complete");

Visible

// wait for first result to be visible
F('#autocomplete_results:first-child').visible();

Width

// after clicking a menu item, wait for its width to be 200px
F('#horizontal_menu_item').width(200);

Val

// wait for the input value
F('#language_input').val("JavaScript");

Size

// wait for number of matched elements
F('.contact').size(5);

There are many more waits possible.

Get information and run assertions

After simulating an action and waiting for the page to change, you often want to get information about an element and run assertions. You can use jQuery getter methods in combination with QUnit assertions.

These methods (which return synchronous results) are used in callbacks that run after a wait method completes.

// wait until we have some results, then call the callback
F('.autocomplete_item').visible(function(){
  equal( F('.autocomplete_item').size(), 5, "there are 5 results");
});

Running in browser

These tests can be loaded in any browser. The app page opens in a separate window and results show up in the QUnit page.

test("JavaScript results", function(){
  F('input').click().type("JavaScript");

  // wait until we have some results
  F('.autocomplete_item').visible(function(){
    equal( F('.autocomplete_item').size(), 5, "there are 5 results");
  });
});

funcunit's People

Contributors

akagomez avatar alexisabril avatar andrejewski avatar binodpanta avatar ccummings avatar chasenlehara avatar cherifgsoul avatar daffl avatar gitter-badger avatar imaustink avatar justinbmeyer avatar kdillon avatar lazd avatar m-mujica avatar marshallswain avatar matthewp avatar mckenna avatar michalbe avatar mickmcgrath13 avatar mjstahl avatar moschel avatar phillipskevin avatar rlin415 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

funcunit's Issues

Add S.if

S.if(checker1, fn1, checker2, fn2, errorFn)

Useful for running conditional code in funcunit.

Trick to verify that a <select> is empty?

I need to verify that a <select> element is empty.

When I call F("select.mine").val(); I get null.

When I call F("select.mine").val(null);, I get an exception: "TypeError: Cannot read property 'timeout' of null".

I can do F("select.mine option:selected").missing(); as an alternative, but the the first one looks like a bug. Should .val([expected]) be updated to allow null?

Funcunit finding within a context

According to the documentation on the website you can give a number or string as context and funcunit will look up the frame according to the context supplied.
However in core.js in getContext you create a selector iframe:eq(+context+) or iframe[name='+context+'] this will never work for getting a frame making it frame instead of iframe does the trick.
So you might consider changing your documentation or change the code to effectively select frames.

Need a way to wait for animations to finish

When dealing with applications with animations, it would be useful to have a wait that waits for all current animations to be finished before proceeding with the rest of the test.

Right now, the instance of jQuery that is running the test (S) and the version running in the app ($) have separate animation queues. This means S(':animated') will always be empty and so S(':animated').missing() will return immediately even though the application still has elements in the animation queue.

safari with selenium broken

This fixes it in open.js

           if(FuncUnit.win && FuncUnit.win.document == null){
                    setTimeout(arguments.callee, 500)
                    return;
                }
         }catch(e){

FuncIt IDE no longer working

Hi Everybody,

I am comparatively new to FuncUnit. In my organization we have been using FuncUnit for over a year now. We have been using Funcit IDE to generate test scripts for us. But it no longer seems to be working.Is the Funcit IDE no longer supported? Any help would be appreciated.

Syn removes href of clicked link, even if it's just an id reference.

Syn.click() removes the href attribute of "a" elements and replaces it with "javascript://" (see http://github.com/pinhook/funcunit/blob/master/synthetic/mouse.js#L162). This causes problems for any event handler that looks at the href attribute of a click event's target element. For instance, I have a tab view that links tabs with the corresponding content based on the tab's href, and Syn is unable to switch from one tab to the next because it interferes with this attribute.

I'm not sure I understand the purpose of this code, but should the replacement occur unconditionally, or should it perhaps leave id reference hrefs alone?

Restore async methods within callbacks

Make it possible to add an async method (action or wait) inside a callback.

Async methods inside a callback should be added to the start of the queue.

If the callback ends and there are no actions or waits, it should wipe out anything added to the queue.

Clicking multiple select boxes crashes Chrome

If you use .click() to change the option of a select box in Chrome, the options will remain visible until you manually trigger a blur. The options remain visible through S.open calls.

If you use .click() to change the option of multiple select boxes, Chrome will crash. I've created a test in my own fork of FuncUnit that consistently shows this behavior.

I created a similar test in syn, but clicking multiple select boxes worked in all browsers. It's possible this issue manifests itself in FuncUnit because we are operating on a window other than our own?

FuncUnit Test: https://github.com/ccummings/funcunit/commit/6885d07101c7a05c812b4580527f512bf85ed0fa
Syn Test: https://github.com/ccummings/syn/commit/8a09ef267696465250c57a2c3ee12ca79e32c9a4

Prompt does not trigger

I run funcunit in FF 9.0.1 and Chrome 16. It opens a new window for running the tests just fine.
In that window, if I type "prompt('Question')" in the javascript console it does nothing.

(I ended up there because I was trying to test a script with a prompt within)

Possible problems with PhantomJS and F.open()?

I've got funcunit almost working with grunt-contrib-jasmine (which runs jasmine tests in PhantomJS and reports back the results via the console), without Selenium, etc.

I'm pretty sure that the problem I'm running into is with how FuncUnit and PhantomJS handle popup windows. I'm not getting much info back from Phantom, but I am getting this error:

>> TypeError: 'undefined' is not an object (evaluating 'FuncUnit.win.document.documentElement') at
>> http:/127.0.0.1:8000/static/bower_components/funcunit/dist/funcunit.js:2648 
>> http:/127.0.0.1:8000/static/bower_components/funcunit/dist/funcunit.js:2657 
>> http:/127.0.0.1:8000/static/bower_components/funcunit/dist/funcunit.js:4285 

There's some discussion about frames vs. popups on this page. Is there something I need to do to switch funcunit into 'modern Node/PhantomJS mode', so it can take advantage of Phantom's window.open(), instead of opening windows in an iframe?

Is there any other information about trying to run FuncUnit tests via PhantomJS without using the 'official process'?

This discussion may also be of interest to anyone following #32.

simulated click on anchor tag - wrong behavior

First I'd like to say thanks for releasing the Syn.js library. Actually I have been working on something similar myself but have only achieved a fraction of the Syn.js.

I have noticed a bug when simulating a click on an anchor tag tag that has both an href attribute and an event handler listening to the onclick attribute, which prevents the default behaviour (e.g. follwing the link) and does some JavaScript magic instead.

[a id="buggy" href="http://www.example.com/images/001.png"]Open Image in Lightbox[/a]

document.getElementById("buggy").addEvent("onclick", function() {
//open image in lightbox
event.preventDefault();
});

For a real world example, see:
http://www.digitalia.be/software/slimbox2

A synthetic Syn.js-Click will load the href in the current window, which is wrong. Unfortunately, there seems to be no way of knowing, that there is an event handler registered for the onclick event, unless it has been assigned using the oldschool onclick attribute.

What are your thoughts on this?

Best,
Chris

offset/position can't execute callback after success

hi,
i'm using javascriptmvc-3.2.4 to create test for my app.

this is my code:

S("#main li:nth-child(2)").offset({top:0,left:-755},function(){
alert('!');
ok(S("#main li:nth-child(2)").offset().left<0);
ok(S("#main li:nth-child(3)").offset().left<755);
equal(S("#myTab_btns li:nth-child(2)").hasClass('hot'),false);
equal(S("#myTab_btns li:nth-child(3)").hasClass('hot'),true);
});

the callback func doesn't work. i test other waits func, they work fine.

does my writing is wrong, or it's a bug?

Don't overwrite confirm, etc

Detect if the app page already overwrite confirm, prompt, or alert. Only overwrite those methods in FuncUnit if that isn't the case.

Add text selection to funcunit drag

We would like funcunit's "drag" event to select text and nodes in the browser, just like when you click and drag with your mouse. Ideally it should use/populate each browser's native selection framework.

Question: how to access jQuery for new opened window

Issue by rinatio from Tuesday Oct 01, 2013 at 18:44 GMT
Originally opened as https://github.com/bitovi/funcunit.js/issues/21


Sometimes I'd like to use jQuery instead of F to execute some code in the "test app window" after F.open(). How can I access it? I guess F.win.$ refers to the app's jQuery, not F.jQuery, but F.jQuery executes inside qUnit window. I know I can use

F.jQuery('selector', F.win.document)

but is there something simpler? A shortcut?

funcunit not producing valid junit xml files

When running your funcunit tests with following command
./js funcunit/open/phantomjs funcunit.html -out "results.xml" the resulting xml file isn't correct. I has class in it's testcases which has to be classname.

By changing
<testcase class="' + xmlEncode(classPrefix FuncUnit.browserName
to
<testcase classname="' + xmlEncode(classPrefix + FuncUnit.browserName

in output.js it is fixed and you can use the valid junit xml files with xunit.

Clipboard emulation

Possibility for FuncUnit/Syn to emulate clipboard functionality (as mentioned in this thread).
So when typing ([ctrl]c[ctrl-up] or ([ctrl]x[ctrl-up] get the current selection (when cutting remove it) and store it in a dummy clipboard. When ([ctrl]c[ctrl-up] is being typed insert is as the value in the element that currently has the focus.

No output message from envjs when installing JMVC from Git

Hi,

I've just installed JavascriptMVC from Git, following the tutorial : http://javascriptmvc.com/docs.html#!developwithgit

I created a new cookbook app : ./js jquery/generate/app cookbook
Then I ran the following command :

$ ./js funcunit/run envjs cookbook/qunit.html
Opening file:///tmp/tmp_project/static/cookbook/qunit.html
starting steal.browser.envjs
$

There is no output message from envjs.

I tried with the Zip file - http://javascriptmvc.com/builder.html - the output is not the same :
$ ./js funcunit/run envjs cookbook/qunit.html
Using Default Settings
starting steal.browser.envjs

cookbook
cookbook testing works
[x] an assert is run

Time: 20 seconds, Memory: 55.13 MB

OK (1 tests, 1 assertions)
$

Thank you for your support

Jon

noConflict

Set $.noConflict in extreme mode so any other jQuery in the page won't be affected.

Fix traverser -> waits

S(":contains('Holler')")
.closest("#foo")
.find(".combo")
.hasClass("combo", false)

This doesn't work as expected. Currently, waits are re-running their queries with this.selector every time the check is made.

S(this.selector). But, traversers aren't modifying the selector.

What needs to happen is we store the last traverser method AND the previous collection, and rerun the traverser on the previous collection.

OR

we can simply not re-run queries for anything except .size waits (exists, missing, etc) and not support the case where you call a traverser followed by a .size wait

Funcunit sometimes generate invalid XML files

Using the ouput option to generate an xml file with test result sometimes create broken xml files. The command I use to generate the results xml is:

./js funcunit/open/phantomjs myapp/myapp_test.html -out myapp/docs/funcunit.xml

The problems is related to an invalid tag "testcase" inside the "testsuite"(s) record during the process. All the tests are executed but somtimes the report process just add only a closing "" tag, this breaks my build when it runs with our CI server (jenkings + xunit plugin). The test script contents is something like:

steal('funcunit')
    // test modules
    .then('myapp/modules/expression/builder/builder_test.js')
    .then('myapp/modules/expression/factory/factory_test.js')
    .then('myapp/modules/expression/variable_selector/variable_selector_test.js')
    // etc ...

All tests pass, no errors inside them, no java exceptions, nothing wrong except for broken xml files.

My Environment:

  • Ubuntu 12.04 64bit (tested even with 13.04 64bit)
  • PhantomJS 1.9.1 (for 64bit platform)
  • Jenkins 1.505
  • XUnit 1.6.1

Design tweaks

Issue by daffl from Tuesday Sep 03, 2013 at 16:21 GMT
Originally opened as https://github.com/bitovi/funcunit.js/issues/9


I really like the FuncUnit page design but I think that the flat style from the front page can also be brought over to the API and guides by removing some of the borders and just using background colors. Attached an example screenshot:

screen shot 2013-09-03 at 10 15 16 am

What do you think @tomgreever?

change handler on controller sub-element not being triggered in tests

It appears that if you have a change handler defined for an element in your controller, that handler will be called when run directly, but will not be called when run through Funcunit.

See gist (git://gist.github.com/2938064.git) with altered "recipe" example that manifests the bug.

Button click on non-existent button causes selenium to freeze test

This is a show stopper at the moment, because it causes hudson build scripts to freeze and never finish

Issue reported here:
http://forum.javascriptmvc.com/topic/timeout-differences-for-funcunit-browser-and-funcunit-selenium#32525000000520013

Basically, if I have a test where I attempt to click on a button that doesn't exist the test simply freezes
Using Default Settings
BROWSER *firefox =====

MODULE setup

--Selenium negative test for button that does not exist--
steal.js INFO: Opening //
steal.js INFO: Clicking .button-doesnot-exist

// Just stops here and doesn't timeout

test("Selenium negative test for button that does not exist",function(){
S('.button-doesnot-exist').click(function(){
//never gets here
})
})

Can't get phantomjs testing to fail

There seems to be a problem with the -e flag when trying to do funcunit tests on phantomjs.

Steps to reproduce:

  • generate an app
  • change the APPNAME/test/funcunit/APPNAME_test.js to test equals(1, 0).
  • run ./js -e funcunit/open/phantomjs APPNAME/funcunit.html
  • watch the test fail
  • create this small bash-script to test the exit status
#!/bin/bash
./js -e funcunit/open/phantomjs rtorrent/funcunit.html
echo "Exit status" $?

Expected output

...
FAILURES!
...
Exit status SOMETHING_NOT_ZERO

Actual output

...
FAILURES!
...
Exit status 0

API improvements

Issue by justinbmeyer from Wednesday Sep 04, 2013 at 04:27 GMT
Originally opened as https://github.com/bitovi/funcunit.js/issues/14


[ ] The menu is useless.

[ ] Documentation presentation issues

The format of the docs should match CanJS's. Lets take: http://funcunit.com/docs/FuncUnit.prototype.click.html for example.

  • its signature click(options [,success]) should be either .click(options [,success]) or funcunit.click(options [,success]).
  • signatures are for terse, explanations. There are way too many examples.
  • Links are broken.
  • Sentences should start with a capital letter.

assertion counters do not show any pass/fails for func unit tests

Issue by shcarrico from Monday Sep 09, 2013 at 19:55 GMT
Originally opened as https://github.com/bitovi/funcunit.js/issues/17


I propose we add a "FuncUnit" assertion and pass/fail it for each test / adapter, so we do not have 0 assertions passing/failing for funcunit tests. Any test w/ a funcunit operation would then have 1 matching assertion of "FuncUnit Tests" or something similar, so you can see clearly whether they have passed/failed for that test.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.