PHP in Action

Beautiful code

dagfinn | 03 November, 2008 04:36

Max Horwath has published his slides on Making Selenium Test Writing easier using a DSL onlinefrom IPC 2008. Let me quote the whole short description:

Implementing automated tests by using Seleniums API methods has several drawbacks. Selenium is great for what it does, providing a generic framework for testing a generic application. Using the Testing_SeleniumDSL framework, I will show you how to create your own Domain Specific Language (DSL), which would allow you to write tests in the language of your business rather than in Seleniums language.

I'm quite impressed by the examples he presents, such as:

$this->assertThat($form->hasSelect(withName('statusConfirm'))->hasValues(),
    array('Yes','No'));
 

This is truly expressive, readable code. I've refactored web test code in this direction many times, but I admit I never got quite this far.

The DSL is planned as an open source release. It would be interesting to try something similar for the SimpleTest web tester, which is my favorite for testing web interfaces without too much JavaScript. (Based on the paparrazzi principle.)

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
    blogmarks del.icio.us digg NewsVine Reddit

Comments

Re: Beautiful code

nico | 03/11/2008, 05:41

I'm sorry, probably I'm completely missing the point of this, so can someone give me a real life example of when such code would be used?

I mean... if I created the page I DO know if a certain form has a certain select in it (I wrote it!!)

And if I'm not sure I'll just open the page in the browser, or even in a text editor...

No need to code anything...

Re: Beautiful code

dagfinn | 03/11/2008, 06:00

dagfinn

I can't conjure up a complete example on the fly, but I can explain what the point is. The intent is automated testing. As you say, you created the page, but then maybe later you broke it, maybe even while changing something code you thought was unrelated. Instead of the page with the select menu, there may be a page that says "fatal error...",for instance. To avoid those situations, I want a test script I can run that will check that everything is still in place without my having to test all of it manually.

Re: Beautiful code

Nigel | 03/11/2008, 06:11

Yeah, I think that code looks gross.

Not eas to read if you ask me

Re: Beautiful code

David | 03/11/2008, 06:53

What is 'withName' in that example... I don't understand how that code would work unless withName is a function within local scope

Re: Beautiful code

dagfinn | 03/11/2008, 09:42

dagfinn

@Nigel: You don't have like it as far as I'm concerned. But let me just say that the reason I find it readable is that it reads almost like an sentence in English.

Re: Beautiful code

nico | 03/11/2008, 09:46

dagfinn, I get your point, but you'll have to agree that probably that is not the best example ever!

I have to say, though, I'm a bit skeptical about automatic testing of pages. The way I prefer to debug code is to actually try it as an user would do, and try to break it, as users do.
The automatic testing is probably good for "internal" pages (for instance hidden to the user) but it takes time to write, and it's not guaranteed to give you "bug-free" code.

What if, for instance, the user changes or exploits some javascript of your page? That you can only check by actually trying to do those things and see what happens. :)

Re: Beautiful code

dagfinn | 03/11/2008, 10:34

dagfinn

You're absolutely right, there are some things that can only be tested manually. The most obvious may be the visual design of the page.

Re: Beautiful code

Steve | 03/11/2008, 12:17

Crickey

That is a painful way to write selenium. I do a lot of selenium i found that far the easiest way is just to write some simple functions to write html that selenium ide understands and put id tags in all the html elements that you'll wanna access, i.e.

$a[] = array('assertText,'myelem','abc');
test('mytest',$a);

making your own DSL ou've got an inner platform effect where you've created a new language (DSL) just to write another language (selenium). very pointless IMO.

Re: Beautiful code

Open | 25/11/2008, 00:14

The code looks terrible.

Re: Beautiful code

Josh | 27/11/2008, 01:39

If you think this is "expressive", you need to look at Ruby. I build my web applications entirely in PHP, but build utilities around them for the sysadmins, and build them purely in Python and Ruby. PHP code itself is terrible.

Re: Beautiful code

dagfinn | 27/11/2008, 02:41

dagfinn

I know what you're talking about. I'm have Ruby project going myself. I agree with you, except I find the word "terrible" hugely exaggerated. Instead I would say that Ruby is even better for this kind of API. All programming languages have their pros and cons.

 
Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LifeType - Design by BalearWeb