• Subscribe to the RSS feed! RSS icon
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Eight years of PHP
    • Learning English
    • Saturday night hack - coords
    • When a package update goes wrong
    • Frontend testing with phantomjs and casperjs
    • Gnucash 4.2 with SQLite3 on GNU/Linux
    • A monkey with a banana
    • 2012 - 'twas a fine year!
    • Let's learn Astronomy!
    • Unit testing Zend Framework 2 modules
  • Tags

    php, about, random, framework, zend, example, ubuntu, zend framework, blog, site, python, book, conference, linux, me, wordpress, apache, hack, hex, introduction, lamp, longboard, open source, review, script, setup, signals, zend framework 2, ape, community, contributing, dbus, dojo, events, life, mysql, netbeans, pidgin, plugin, pyqt
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
    • Uncategorized
  • Archives

    • April, 2013
    • March, 2013
    • February, 2013
    • January, 2013
    • September, 2012
    • August, 2012
    • July, 2012
    • June, 2012
    • February, 2012
    • January, 2012
    • December, 2011
    • November, 2011
    • October, 2011
    • September, 2011
    • August, 2011
    • July, 2011
    • May, 2011
    • April, 2011
    • March, 2011
    • January, 2011
    • December, 2010
    • November, 2010
    • October, 2010
    • July, 2010
    • June, 2010
    • April, 2010
    • February, 2010
    • January, 2010
    • December, 2009
    • November, 2009
    • October, 2009
    • August, 2009
    • May, 2009
    • March, 2009
    • February, 2009
    • January, 2009
    • December, 2008
    • November, 2008
    • October, 2008
    • September, 2008

Eight years of PHP

by Robert Basic on April 4th, 2013

This time around eight years ago I was introduced to this thing called PHP: Hypertext Preprocessor. I studied it in college as a part of classes on "Internet Technologies". It consisted of HTML, CSS, some Javascript and XML, and PHP.

Maybe a half a year earlier my parents finally gave in and agreed to hook up to the Internet via our local ISP. At that time I was mostly hanging out on IRC channels, bulletin boards and the like. Soon enough I got curious about how all that stuff works and me being a fresher picked a class that sounded cool, the aforementioned "Internet Technologies".

First I wanted to be a designer, but luckily for everyone I soon realised I am not quite talented for that sort of work. I still don't understand what is so bad about having a green background with red fonts on it.

Then the PHP classes started. I was lucky enough to have a great professor teaching it and I quite enjoyed the lessons. I owe a great deal of gratitude to him and am lucky to call him a friend today. In the following years of college he pushed me to learn more than what was planned by the classes, took me to conferences and made me present my work there and so instilled in me the love for programming.

Thanks Zlatko!

One of the first things we did in class was printing out numbers from one to ten and have the even numbers coloured blue and the odd ones red. Or maybe it was the other way round. And that was fun! Some text that looks like you mashed your hands over the keyboard can do such a thing? It's magic!

There's another thing I remember. We had a "home project" that we were supposed to make. It was a simple address book, basic CRUD operations, nothing fancy. Well, nothing fancy today, but then... Oh man. I was stuck for at least a day trying to connect to the MySQL database and run a simple query against it. Once I did it though, I was jumping from joy like a little kid and ran three circles around the house. Magic!

Somehow at that point I was certain that doing this as "work" would be extremely fun and could clearly imagine myself doing it in my life. Hell, for the first time I knew what I want to do when I grow up.

And here I am eight years later, doing exactly what I wanted to do happy with how things turned out. Here's to eighty more years.

Tags: about, php, life, random.
Categories: Blablabla.

Learning English

by Robert Basic on March 29th, 2013

Most of my knowledge of the English language is self-taught. I had English classes in elementary school, but that was more or less singing "London bridge is falling down" and reciting a list of irregular verbs. I also had a semester of English in college, but that again consisted of reciting a list of irregular verbs (true, this time the list was longer) and reading and translating engineering texts. Good for learning how to read a technical manual, but not so much when it comes to having a conversation with other people. Other than that, it's all from computers, music, films, books.

My mom has an old dictionary that I used to read when I was a kid. I remember spending hours upon hours flipping through the pages, learning words, trying to figure out how to pronounce them. Guess I was a nerd already when I was only seven years old.

In the past few months Swizec was quite persistent with his nagging of how terrible my English is, especially when it comes down to the grammar, so I decided to do something about it.

I started taking private English classes.

I had my first hour and a half long lesson yesterday evening. I enjoyed it very much, had a lot of fun and already learned a thing or two (at least I think I did). I am taking the classes from an English teacher who also happens to be my friend. But, don't let the friendship fool you! She's not afraid off bashing me when I make a mistake: "Hold on Robert! That sentence doesn't make any sense!" (or something along those lines). And I think it is actually a good thing that we know each other from before as it is much easier to just start a random discussion and not just go through the textbook examples. All in all, very enjoyable classes and I know I will learn a lot from them.

And to show that I am really serious about this, I started another blog dedicated to writing called Magic of Writing. And yes, I am aware that that sounds something like a five year old would come up with, but then again writing is magical. There's not much there yet, but I already have a few ideas in my head that will hopefully find their way onto paper.

Tags: learning, writing, english, language, reading.
Categories: Blablabla, Free time.

Saturday night hack - coords

by Robert Basic on March 24th, 2013

When I was just starting out learning programming, everything was so simple. I did not care about design patterns and best practices and unit tests and how will users use that piece of code. Hell, I did not even know those things exist. I was having fun, I was learning, I was free to do whatever I wanted to do, I was playing, I was like a child. Not that there is something wrong caring about those things now, but then I was able to put out a piece of code that was fixing a core of one problem I had and that was it. Once I was done with that, I would move on to the next problem. For a long time now I was missing that feeling of not caring, just fix the damn problem and move on. Just to slap together some crappy piece of code, use it once or twice and then forget about it.

And that was exactly what I did last night. I sat down and in some five or six hours I put together coords. It is an ugly as hell little pygtk application, void of any good practices, no tests, just a few comments here and there and that's it. And I had fun writing it! I completely lost track of time while hacking, got into the zone and today, after some six hours of sleep I woke up feeling like I was on a vacation for a week.

The application itself doesn't do much, it helps determine coordinates on your desktop. Start the application, click "track", drag the mouse from the top-left corner you're interested in to the bottom-left one and that's it. The entire functionality is shown in this ten second long gif that runs somewhere here on the page. The best part is that it actually solves a problem I had, it helps me determine coordinates on my desktop and then I can use those coordinates for byzanz-record. I loved every second I spent hacking on this.

Best part is that even this little application had a quite an interesting challenge to solve, namely, to determine the position of the mouse anywhere on the screen. It's no big deal to determine the position of the mouse inside your application, but once you want to break out of it, well, it gets bit tricky.

With pygtk one can only subscribe to events that happen inside the application itself. To go lower than that one needs to use a different library, something like xlib (python-xlib from python). After much poking around I found a way to do it from pygtk itself. It is possible to get hold of the root window instance, which is created by the X server itself (you can't create a root window from an application, or make an application be a root window, afaik). Once you have the root window, grab the pointer, and then filter events you are interested in on the root window before they get sent from the X server to gtk. Or at least that is how I understood this whole process. While having control over the pointer, get the mouse coordinates from the time left button is pressed till the time it is released. Don't forget to ungrab/release the pointer once your done. And that's all there is to it, more or less.

The interesting parts are:

def start_tracking(self, widget, data=None):
    mask = gtk.gdk.POINTER_MOTION_MASK | gtk.gdk.BUTTON_PRESS_MASK | gtk.gdk.BUTTON_RELEASE_MASK
    self.root_window = gtk.gdk.get_default_root_window()
    gtk.gdk.pointer_grab(self.root_window, False, mask)
    self.root_window.add_filter(self.track_region, self.region)
def track_region(self, event, region):
    x, y, flags = event.window.get_pointer()
    if 'GDK_BUTTON1_MASK' in flags.value_names \
            and region.track_started == False:
        region.start_x = x
        region.start_y = y
        region.track_started = True
        region.track_ended = False
    if 'GDK_BUTTON1_MASK' not in flags.value_names \
            and region.track_started == True:
        region.end_x = x
        region.end_y = y
        region.track_ended = True
        region.track_started = False
        # ungrab the pointer so we get control back
        gtk.gdk.pointer_ungrab()
        self.show_region_values(region)
    return gtk.gdk.FILTER_CONTINUE

Isn't it ugly? Very. But it works and it solves the problem I had. Btw you can check out the code on github to have a bit more context for all this.

Happy hackin'!

Tags: hack, python, pygtk, byzanz.
Categories: Development, Programming, Software.

When a package update goes wrong

by Robert Basic on February 6th, 2013

I am running Fedora 17 on my laptop, and yesterday there were some packages to update. Nothing unusual, updates on Fedora are quite frequent and, up until yesterday, there was not a single problem I remember with any update. And it was a small update, four packages in total. What could possibly go wrong, right?

After a reboot an odd thing happened. My laptop did not automatically connect to the wifi. Huh. So I clicked on the little network manager icon to pick the wifi. The list of scanned wifis was empty. Not a single network on the list. Then I did all the usual things one could do in a situation like this - turn on/off the wifi via the network manager, turn on/off the wifi via the keyboard shortcut, reboot the laptop a couple of times. Still nothing. At this point I started panicking.

I started suspecting the update. But I have no idea what packages were actually updated, what those packages do, or how I could see what packages were updated. After a bit of a googling from my phone, the answer was yum history. Apparently, this command will list recent changes to the system done by yum. Each change has a Transaction ID. More information about each change can be get with yum history info TID, where TID would be the transaction ID of the change you're after. That will nicely list when did the change occur, which user made the changes, and what packages were affected in what way by that change.

One package caught my eye, called crda. It was updated from version 1.1.2 to 1.1.3. Google told me that crda has something to do with wireless. So, that was probably the culprit of my broken wireless. I started searching for possible bugs for this, or maybe even a workaround or a fix, but to no avail. I am not really good at debugging things like this, so I started looking for a way to somehow revert the update to this package and hopefully fix my wireless problems.

Yet again Google was involved and yet again yum history came to rescue. Apparently, besides tracking changes to the packages, it can also undo these changes: yum history undo TID, where TID is the ID of the transaction you want undone. It will try to undo changes to all packages, and in my case, it failed to undo some (hi Java!), but the crda package was reverted back to version 1.1.2 and after another reboot, the wireless was up and running once again, like nothing happened.

Yey for yum!

I have submitted a bug to Fedora's bugzilla to hopefully figure out what went wrong and help the developers find a fix for this.

Tags: linux, fedora, packages, yum, history, undo.
Categories: Development, Software.

Frontend testing with phantomjs and casperjs

by Robert Basic on January 29th, 2013

I am not usually fond of doing much frontend stuff, but I do like to dable in some javascript from time to time. Nothing fancy, no node.js, coffeescript and the likes for me. I still feel like making applications on the server side, and have the client just show things to the user. If needed some 3rd party javascript library or framework to make my life easier, and that's about it.

For a while I was reluctant on writing any frontend tests, integration tests, or whatever you want to call them, because, y'know, *refresh-click-click-click* and the testing is done. Easy. Except when it's not. From time to time some piece of user interface gets wild on javascript and it turns out after a couple of weeks, half a dozen of bugs reported, and hundreds of refreshes and thousands of clicks, that whole thing becomes tiresome. Thus, I decided to dabble my toes deeper in the waters of the javascript world and try writing some tests for all this.

What I found first is that there's a lot of testing libraries out there for javascript. Won't even try listing them all. When I set out for the hunt I knew what I wanted and needed. I wanted a tool that will help me automate all my refreshclicks, or at least to some extent. Tell the tool through my tests: "go to that page, check what and how is rendered, do some things with the UI, test again". The less dependencies it has on other things, the better. I asked around among people who are bit more fluent in javascript than I am, what are they using and what would they recommend. Not too much to my surprise, everyone recommended a different thing, whatever fits their problem. So I ended up picking the tools that fit my problem.

The tools chosen

The first tool I picked was phantomjs. It's a "headless WebKit with JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG." (shamelessly copy-pasted from their website). When installing it either download a binary from their website, or compile the source on your own. If you're on Ubuntu, do not install it via apt-get. It installs some very old version, 1.4 I think, and phantomjs will just scream at you something about some X servers. The binaries are pre-built for any and all systems, so just use them (not that I tried all of them, but I trust they all work).

To cut the story short, writing tests with just phantomjs is difficult. If at all possible. Because hey! It's not a testing library. I think. This part is a bit blurry for me.

Enter casperjs! These two together look like a perfect match for doing automated, frontend tests full of javascript.

Even though last week on Friday I was being a bit frustrated and a bit more of a dick and said bad things about casperjs over twitter which I shouldn't have. Sorry. Broken things on a Friday at 5PM can do that to a man.

Examples!

That's why you're here, to see some examples. And this example, which, btw, is on Github here, will have a simple login page with a form and a logged in page with some UI elements that you can click around and do stuff! And we're going to test all that with phatnomjs and casperjs. Just like real life. Cookies included.

When writing tests with phantomjs and casperjs, all one need to do is think in steps. Same simple steps like in the refreshclick procedure. First, open page. Make sure we are on the correct page. Make sure all the elements are there. Click something. Make sure that thing is clicked. Fill in a field. Make sure the field is filled. And so on.

Let's have a look at the first part of the tests.js file:

casper.start('http://localhost/frontend-testing', function () {
    this.test.assertUrlMatch(/login.php$/, 'Redirected to login page');
    this.test.assertExist("#login_form", 'Login form exists');
    this.fill('#login_form', {
        'email': 'email@example.com'
    }, false); // false means don't autosubmit the form
    this.test.assertField('email', 'email@example.com');
});
casper.thenClick('#login', function () {
    this.test.assertUrlMatch(/index.php$/, 'Redirected to index page after login');
});

The tests are surprisingly self-explanatory: start by opening up the home page. Assert that we are (redirected to) on the login page and that the login form exists. Then fill the email field of the login form with a given value. Assert that the field was indeed filled with that value. Once that's done, then click on the login button, and assert that we end up on the index page.

Easy!

And practically that goes on in the entire test. Start, assert, then do this, assert, then do that, assert, done.

Testing ajax calls isn't difficult either:

casper.thenClick('#do_ajax', function () {
    this.waitForResource('http://localhost/frontend-testing/ajax.php');
});
casper.then(function () {
    // Sometimes we need to wait a bit more for ajax requests ...
    this.wait(50);
});
casper.then(function () {
    this.test.assertTextExist('Just some ajax response.', 'Ajax request was made');
});

Do some action that triggers an ajax request, wait for that ajax request to finish and assert that something was done with the response from that ajax call. Of course, in real life examples you will have a bit more complicated setup, but hey... As for faking ajax requests I hear that can be done with this cool sinonjs library, but I haven't managed to get that working, yet. Mostly because I didn't need to fake any ajax calls.

The most voodoo-like thing in these tests is probably the evaluate() part. I don't think I really know what that it is, but what I think it is, that, whenever you want to do something in the actual webpage, but from within the tests, you use evaluate().

For example, I had to use evaluate() to determine is the checkbox checked or not:

casper.thenClick('#enable_ajax', function () {
    // I could swear I had this one working
    // this.test.assertEquals(this.getElementAttribute('#enable_ajax', 'checked'), 'checked', 'Checkbox is checked');
    this.test.assertTrue(this.evaluate(function () { 
        return document.getElementById("enable_ajax").checked;
    }), 'Checkbox is checked');
});

Not sure how, but the this.getElementAttribute() way does actually work in my other tests. Honest. Not sure why it didn't work in this example. Maybe some other factors not present here, affected my other tests? I don't know.

Helpful bits

What I found extremely helpful while writing the tests is the this.debugHtml() and this.debugPage() casper functions. The former will dump the entire HTML to the terminal, and the latter will dump just the text of the entire page. Can be useful to figure out what's going on.

The other helpful debugging function I used a lot is this.getElementAttribute( ). It retrieves a bunch of helpful information on an element, which you can then dump to the terminal, to further figure out things:

casper.then(function () {
    require('utils').dump(this.getElementAttribute('#enable_ajax'));
});

Will result in an output like this:

{
    "attributes": {
        "id": "enable_ajax",
        "name": "enable_ajax",
        "type": "checkbox"
    },
    "height": 13,
    "html": "",
    "nodeName": "input",
    "tag": "<input type=\"checkbox\" name=\"enable_ajax\" id=\"enable_ajax\">",
    "text": "",
    "visible": true,
    "width": 13,
    "x": 12,
    "y": 58
}

I mean, it even gives the positions of the checkbox on the page! Super helpful.

casperjs supports CSS3 selectors and, which gives me much joy, XPath. Makes getting those pesky little elements covered in layers of divs and tables a walk in the park.

One thing though: you can't do things like selecting multiple elements at once and then .each()'em or something like that. You have to select elements one by one and do assertions on each of them. Even if the selector matches multiple elements, it will just return the first element, so you'll probably end up using lots of :nth-child(n) selectors. But that's what copy-paste was invented for.

That's about it, I guess. Of course, there's much more to both phantomjs and casperjs, but I found the documentations well written, so I believe it's fairly easy to tackle even the testing of more complicated web pages with these libraries.

Happy testing!

Tags: testing, frontend, javascript, phantomjs, casperjs, automating.
Categories: Development, Programming.
1 2 3 4 5 › »
Robert Basic © 2008 — 2013
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on November 30th, 2009.
Frameworks used: Zend Framework, Dojo, 960 Grid System