Tuesday, February 24, 2009

cross browser key events in javascript

this is a pure JS solution that lets you get the key code from an event, it works for all browser:

Monday, February 23, 2009

Functional Testing with Tellurium

I have recently discovered a new toolset for functional testing. Its called Tellurium and it runs on top of Selenium. Tellurium brings some cutting edge technologies to the table:
  • It's built fully in Groovy and takes advantage of DSLs, closures and other syntax sugar, while letting you code in java if you prefer.
  • It supports JUnit and TestNG unit testing frameworks
  • Instead of relying on xpath to describe the UI, it uses a UI mapping DSL
  • Comes with a firefox plugin for generating UI modules called TrUMP
Even though Tellurium is a fairly young project, it has very good support on the project forums. I have requested a few bug fixes and patches, and had the fixes committed within days. Anyway, the biggest advantage in using Tellurium vs Selenium is the UI mapping. Let's look at what a simple Selenium script would look like versus that same script written for Tellurium.

In this simple script we will click around the Tellurium project page. This is the Selenium version:



And now, the same test in Tellurium. Tellurium uses UI modules to describe the UI in a class that is separate from the test. Here is the UI module DSL:



And this is the Tellurium TestNG test case that uses the above module:




As you can see, the code looks very different. You will notice that the Selenium test is much shorter, but does not re-use UI elements like the Tellurium test does. Tellurium's UI mapping maps UI elements to very short element ids, which support hierarchical nesting, which can greatly simplify large tests.
Another big and obvious advantage to UI mapping is that if your underlying html changes, you would only need to modify the UI mapping DSL, and not need to worry about the actual tests. This is where Tellurium really shines for me.
Modifing the UI maps is really simple using the TrUMP Firefox plugin. TrUMP lets you point and click on the page, and builds out the nested UI module DSL for you.

What you will need in order to use Tellurium for your project
Useful resources for working with Tellurium and TrUMP firefox plugin
The best place to go to find support is the tellurium user group. Questions are answered very fast, and it is the main reason I was able to integrate Tellurium into my functional testing strategy at work.

Also, the Tellurium team has created two videos to help you get started with the project:
Tellurium Demo Video Part I: Tellurium project and TrUMP IDE
Tellurium Demo Video Part II: Create Tellurium Test cases

Tuesday, February 17, 2009

Building fast jQuery plugins

There are 3 million articles out there about making jQuery plugins, but most of them seem to focus on the basics. That is all well and good, but none of those articles will help you if you want to write a fast, responsive plugin that wont peg the CPU.

I will outline some techniques that have worked for me in building good jQuery plugins.

  • Cache your selectors. Not caching can be a real performance killer, once you select an element, cache it, if you plan to use it again. Even ID selectors should be cached. it will always be faster to reuse a cached selector than to ask the DOM for it. When it comes to plugins, you can hide the cached selectors in the main closure, and reuse them in the methods of the plugin.
  • Cache your state. It will always be faster to check if a boolean is true, than to check if a DOM element is visible. Forget about the DOM. Cache everything you will need access to, cache your modifications. DOM access is slow. I will exchange a few bytes of memory for a few ms of execution any day.
  • Use closures. If you dont know how to use closures, you must learn now. You wont be able to write a good plugin if you dont know when and how to use these. You should use the closure to hide all your config data, your cached selectors etc.
  • Dont use $.fn. if it doesnt make sense. Sometimes it makes sense to return something other then the jQuery object from your plugin. You can always pass in the 'this' the old school way as an argument. Sometimes it may be better to return an object that makes some methods from the closure 'public' and exposes some members. This kind of an object can be useful when writing unit tests, or if you want to provide a rich API without bastardizing $.fn.
  • Use functional inheritance. Use this and you will be get private/public members. This is important if you would like to have a clean API. I am talking about the functional inheritance from 'javascript: the good parts' book. See example of what i mean here.
  • Break things up into responsibilties. Let each method inside your plugin handle one thing. Do we need to recalucate something on window resize? Do we need to reload some event handlers after AJAX calls? Dont make one giant 'reload' method when you can have more specific methods that will save time.
  • Time and profile. I wish i had started doing this earlier. Use firebug's timing and profiling to see which portions of your code are slow, and optimize them.
  • Unit test. QUnit is jQuery's testrunner. Use it.
  • Test on a Pentium 3, in IE 6. This is my golden standard. If its fast here, you have nothing to worry about.

Sunday, February 08, 2009

Using jQuery with NYT JSON API

New York Times recently made public a great API that lets you search all of their articles from 1981 to present, and get the data back in JSON format.
I decided to use this API to add some relevant news stories to a site i have been working on. I was going to use jQuery to pull the data using $.getJSON() and shove it into some divs on my site.

Problem:
I quickly ran into a problem. NYT is retuning plain JSON, not JSONP. JSONP is needed to do cross-domain JSON calls. jQuery will make a jsonp call if you add the following to the request url:
jsoncallback=?
In the background, what happens is that jQuery replaces the ? with a function is creates, for example jsonp1229009. jQuery is expecting a response of:

but new york times is returning:

When jQuery pulls that response into a script tag, and executes in, i get the following error in firefox: "Invalid Label".

Solution:
What i needed was a JSONP wrapper. Luckly, i have a friend who knows perl =) I asked him to write a service that will take 2 params: url and callback. Where URL is an encoded URL i am requesting, and callback is the param to wrap the response in. It worked out nicely, i can now use ANY JSON API and get JSONP back. This is the perl source:



Here is my crude and incomplete jQuery plugin that gets me a couple of articles with predefined fields and displays them on my site:



i call it like this:

Tuesday, February 03, 2009

Architect - Belgian Connection Lyrics

Lyrics of Belgian Connection from the 2004 album 'I Went Out Shopping To Get Some Noise' by Architect:

You wait and see if I'm not right.
Forty-eight hours and we'll have them aloft.
Then whoosh, up, over and whammo.
Forty-eight hours and we'll have them aloft.
Then whoosh.
There goes the enemy.
Obliterated, finished.
But what are they doing in the meantime?
What do you mean what are they doing?
Probably retaliating the best way they can.
It's a big waste of time, let me tell you.
We get the first licks so they can't do much.

You wait and see if I'm not right.
Forty-eight hours and we'll have them aloft.
Then whoosh whoooosh whooooosh
There goes the enemy.
??
Forty-eight hours and we'll have them aloft.
Then whoosh
There goes the enemy.
Obliterated, finished.
But what are they doing in the meantime?
What do you mean what are they doing?
Probably retaliating the best way they can.
It's a big waste of time.

Thursday, January 22, 2009

Realtek HD audio fails install

Problem:
I am using SP3. When i try to intall realtek HD audio drivers the installer program tells me "install failed" and exits. Looking in device manager shows that i have no drivers installed. Its really annoying. SoundBlaster 16 was easier to get working!

Solution:
The problem is that the MS Universal Audio Architecture (UAA) High Definition Audio also known as HD Bus drivers are either not isntalled or corrupt. Since i am running SP3, they are suppose to be there, so there is something wrong with them.

Step 1: Go into installed programs and uninstall the HD audio drivers. (if they are not there go to step 2) It will complain that a bunch of stuff will probably stop working if you do this. Ignore this warning, and all like it. I mean, who cares if some stuff breaks a little, you just want a 'multimedia PC in 2009'!

Step 2: Reboot your computer, You will need to reinstall the HD bus, the installer for that is called KB888111 and can be gotten here. Now, since you have service pack 3, you wont be able to install these, because windows thinks that you already have them. You need to trick windows into thinking you have SP2:

Step 3: Get into regedit (google it) and edit the following key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows\CSDVERSION
Set it to 00000200 from 00000300. Restart windows.

Step 4: Install KB888111, install your audio drivers. Reboot. Change the above key back to 00000300 (or dont like me, i mean, who cares?).

If this does not work for you, you should just reinstall windows, or, get a real sound card. Ive never had more problems getting sound to work then with this HD audio crap.
Also, i hear this will also work for other sound cards that use this UUA crap.

Thursday, January 08, 2009

Intellij port in use when starting tomcat

Problem:
Recently, i started to get "Error running Unnamed: Address localhost:8080 is already in use" errors when starting tomcat through intellij. I checked netstat and nothing was using port 8080, restarting the computer didnt help. Interestingly, i could start tomcat from the command line on port 8080.

Solution:
The same day, ESET NOD32 3.0.684.0 Antivirus was pushed on my box. It was the evil problem. I needed to disable the following things in it in order to get tomcat to launch again:
Setup -> Advanced -> Web Access Protection -> Uncheck "enable web access protection"
Setup -> Advanced -> Web Access Protection -> HTTP -> Web Browsers -> put [x] on java.exe and idea.exe

apply and reboot.

Friday, December 26, 2008

LG VX8300 USB driver download

LG VX8300 usb drivers are hard to find on the internets for some reason...

download them here: LGUSBModemDriver_4.6_WHQL.zip

also, the unofficial user guide is a good thing to have

edit: wow this post is popular, please click on my ads to help me pay for the bandwidth that hosting these files for you is costing me. Thanks.

edit: fixed links

Wednesday, December 17, 2008

jQuery: responsiveness plugin for fast UI

for demo go here

Problem:
I was working on a dropdown widget where the user could scroll down using the arrow keys, and while he was scrolling, two things needed to happen:
  1. UI need to be updated, showing his new selection, and removing his old selection.
  2. Some costly DOM operations had to happen in the background to set the selected item in the widget.
I noticed that the scrolling was slow in IE6 on a P3-1000. Mind you, scrolling 1 item was fast, but holding down the arrow key and scrolling 50 items was painfully slow, because the DOM operations had to happen for every item scrolled.
I also knew that i only really needed to do the DOM operations for the last item selected, that is the one the user really wanted.


Solution:
Javascript is single threaded, but there is a timer function that makes faking multi-threaded execution possible.
What i needed to do was to split up the scroll function into two components, first call the UI update every time the arrow was pressed, and second, queue up the expensive DOM operations to happen in the future, but dont execute them until no new DOM operations have been queued for some interval of time. This would give the user the visual feedback of "something is happening" while not executing the costly operation that would make everything feel slow and unresponsive.

The plugin i came up with lets you specify the interval that you will wait for further requests until you execute the last request, and the number of times you will wait that interval.


One thing to be aware of is that this plugin does not implement any queing internally, so if you should only use one instance of it per function. make a new instance if you want to improve the performance of a different function.

For more info such as:
  • A simple typing demo
  • A crappy implementation of hoverIntent plugin
  • API
go here

Friday, December 12, 2008

jQuery: Conflict free events

Problem:
I had to validate an input, both onBlur and when the user pressed enter. If there was an error, in both cases, an alert would pop up. The problem was that when the user clicked 'ok' on the alert, the field would lose focus and cause the blur event to fire, and thus cause another alert to pop up. I only wanted one alert per error, so i made this little plugin to solve this problem.

Solution:
To use this plugin, you can pass in a hash with 2 keys: {blur: function(){}, enter: function(){}}, or a single function that you want run both onBlur and onEnter.



Example usage:

Saturday, November 15, 2008

How to make a pie crust from scratch


Making a pie crust is harder than programming, i have decided. It is one of the hardest things a man can undertake. Anyway, i am a pie crust making beginner. I just made a great pie crust and i need to remember how to do it.

You will need:
a food processor with a metal blade
1 1/4 cups white flour
2 teaspoons sugar
1/8 teaspoon salt
1 stick (1/2 cup) of cold butter, diced
1 egg, beaten to a pulp
1 table spoon of water

First, the butter needs to be pretty cold, think, freezer cold. The colder, the better.

  • combine flour, surgar and salt in the food processor and turn it on, let it do its thing while you do the next step
  • dice your stick of butter, and put it in the food processor, leave it on until the mixture looks like corn meal, make sure that all butter gets mixed in well.
  • beat the egg and add that into the food processor. at this point the dough should start forming into a ball.
  • add 1 table spoon of water and let it get mixed in.
  • remove the dough from the food processor, and form it into a ball. refrigerate the ball for about 15 mins
  • get some clear plastic wrap, lay it on your counter. put the ball on the wrap and start forming into a circle, about 1/8 inch thick. (it has to be bigger then your pie pan by about 1 inch)
  • after you make the circle, cover it with some wrap, and put it on a pizza pan, and freeze it for 5 mins
  • take dough out of the freezer, it should be hard, and easily broken. DO NOT BREAK IT. put it on top of your pie pan and remove the plastic wrap from under it. let it rest there for 3-4 minutes to warm up
  • when dough warms up it will be more pliable. form the dough into the pie pan. if it breaks, uses peices to patch up cracks. you might have extra dough on the sides. use food scissors to cut off excess. you can make something cool with the extra on top of the pie.
  • freeze the dough in the pie pan for 5 minutes, covered.
  • put a piece of foil in the pie plate, and form it around the dough, add pie weights (i dont have these, so i use a pan top)
  • bake at 400 deg for 20 minutes
  • remove foil and bake at 350 deg for 5 minutes or until crust is golden.

Wednesday, November 12, 2008

How to debug UI display issues

i spent far too much time tracking down stupid UI issues in IE. This is an attempt to write down some techniques that yielded results.

  • If the issue is on a screen with a javascript UI widget:
    • Does the issue occur oh pages without the widget? no:
    • Does returning early from the widget rendering function fix it? yes:
    • Move the return statement down the render function, until the UI breaks.

Tuesday, October 14, 2008

Use Selenium RC in Firefox 3

How to use Selenium RC in Firefox 3

For a while now i have been told that selenium RC does not work in firefox 3. This is a huge bummer for anyone who wants to run functional tests and has to downgrade to firefox 2.

Edit 1/12/2009:
A new version (1.0 beta 2) of selenium RC was released today that fixes this issue once and for all!
get it here. You shouldnt need to do this hack now, unless you want to use the old non-beta version. If you do need to use the old version of selenium, here is how to get it to run firefox 3:

Edit 2/8/2009:
Today i was told that there is a firefox extension that allows you to turn of extension version checking. Also, you can do it by editing about:config.

Today i found a workaround to this problem, which works, and is really simple.

Here is what you need to do:
1. Open selenium-server.jar using winrar
2. locate 2 dirs: customProfileDirCUSTFFCHROME and customProfileDirCUSTFF
3. recursively explore each of those dirs, and when you find a file called install.rdf drag it to some temp location, and edit the following line:
<em:maxVersion>2.0.0.*</em:maxVersion>
change it to:
<em:maxVersion>4.0.0.*</em:maxVersion>
4. drag the install.rdf back into the archive and overwrite the old one.
5. do this for all the install.rdf files in those 2 dirs.


you can now use "*firefox" in selenium RC. "*chrome" was giving me problems though, complaining about a malformed rdf file, i might have made a mistake in one of them. Either way, i use *firefox, so it does not matter to me.

if you want, you can get my modified selenium jar here - modified to run in firefox3.
the version of selenium that this jar contains are:
selenium.core.version=0.8.3
selenium.rc.version=0.9.2
selenium.rc.revision=2006
selenium.core.revision=1879

also, there is a 1.0-beta1 compatible modified jar here

Edit 12/10/2008:
Some people have been asking for help with selenium errors in the comments. I cannot help you with those, they are probably specific to what you are doing. If you have question about selenium RC errors, you should try asking them on the Selenium RC User Forums.

Monday, October 13, 2008

IE beeps on enter in an input

Problem:
I have an input box where the onKeyDown event will check if the key pressed was enter, and submit the form on enter. This works fine, but in IE 7 the browser beeps when you press enter, and submits the form.

Solution:
This happens because 'enter' is not valid within the context of an input box, according to IE. Thus, pressing it will cause the beep. What you need to do to remove the beep, is to return false from the function which does the submit. It looks something like this:

<input onkeydown="return postOnEnter(event)"/>



obviously, i am calling some custom functions in the snippet above, but the location of the return statements, and their values is all that matters.

Wednesday, October 01, 2008

jQuery: constrain input chars plugin

Problem:
you need to only allow the user to input certain characters into the input box, and you want a jquery plugin to do it!

Solution:




the following lets you specify which chars (regexes or'ed together) are allowed in the input.

i added a small demo for the plugin, check it out here

get the plugin here

Saturday, September 13, 2008

beer mushroom chicken with potatos

another thing i made up, and it was good.

you will need:
  • 2 chicken breasts
  • 1/2 cup dried mushrooms
  • 1/2 package of mushrooms
  • 4 large potatos
  • sam adams seasonal beer, 2 bottles
  • 2 cloves of garlic
  • 1 medium onion
  1. slice the potatos into round thin slices, and boil them in some water until soft-ish
  2. put the dried mushrooms into a food processor and chop into small peices
  3. meat hammer the chicken breasts to flatten and dredge the chicken breasts in the mushroom powder
  4. slice the non-dried mushrooms
  5. put some olive oil into a pan with the chopped up garlic and cook until garlic gives off some flavor
  6. dice the onions and add that to the pan, cook until translucent
  7. add the mushrooms and let cook for about 3 mins
  8. turn the heat to high and add the chicken letting it brown on the outside
  9. add the potatos which are now softish to the pan and cook with the chicken and the mushrooms, turn the heat down to low, cover the pan while the stuff cooks.
  10. when the stuff starts looking pretty cooked, add the beer, but not all of it right away, do it in stages. When the beer evaporates your stuff should be cooked
  11. drink the 2nd bottle of beer while you cook.
make sure the chicken is cooked through. serve with more beer. yum.

Thursday, September 11, 2008

find table contstraint in oracle database

problem:
free toad does not provide a constraint view.

solution:
select * from user_constraints where constraint_name = 'THE_CONSTRAINT_NAME';

TABLE_NAME is the name of the table that has the constraint, and probably is the table you need to delete stuff from to clear the constraint

Sunday, September 07, 2008

cheese stuffed chicken

I made some food last night that was good, using a recipe which i mostly made up. I need to write this down.

feeds 4:
  • 2 chicken breasts
  • 1/2 of a brick of frozen spinach 
  • 1 medium onion
  • some phylo dough
  • mozz cheese, atleast 3 cups
  • some melted butter
  • salt and pepper
Hammer the shit out of the chicken breasts so that they are flat. put some salt and pepper on them. Dice the onion and cook with olive oil on a pan, add the spinach. Cook until onion is cooked.
On each chicken breast, put 1/2 of the onion and spinach, and a ton of cheese, and roll the chicken up into tubes filled with cheese, onion and spinach. Get out the phylo dough, place 2 sheets on top of eachother and brush with melted butted until soaked. wrap each chicken roll in 2 peices of phylo dough.  place on baking sheet and bake for about 20-25 mins on 450.

yum