views:

350

answers:

2

I'm using Selenium to test my application. A nice test would be that after every DOM manipulation I would validate the DOM. Is there a nice way to do this?

Obvious ways are:

  • use some builtin Selenium function. Is there one?
  • get the HTML of the DOM after the manipulation and use a local validator. However, I can't see how to get the HTML for the current DOM from Selenium.
  • I could go through javascript somehow, either by adding a JS file while testing or through Selenium. But I'm not sure how to go about this. Thoughts?
+4  A: 

There is no built in process for doing this using Selenium. There is however a Open Source project that is built on WebDriver, WebDriver is becoming Selenium 2, that allows you to do layout testing.

The project is at http://code.google.com/p/fighting-layout-bugs/ and was first shown at GTAC 2009 in Zurich. The code uses W3C and jQuery to check the validity of the DOM. Have a look at the video of the talk and the code that is there.

EDIT:

Thanks to sampablokuper the W3C validation is at code.google.com/p/w3c-markup-validation-filter/

AutomatedTester
That's an extremely cool looking project, but layout testing is quite different to testing HTML for validity.
sampablokuper
the frame work does do w3c validaion. Watch the video on the site to see the exAmple. It creates a box on the page that is either red or green depending on how it validates so you just check its colour
AutomatedTester
True, the vid does mention it, but the code for doing it is actually in Michael Tamm's other project: http://code.google.com/p/w3c-markup-validation-filter/ . I haven't managed to get that code running on my system yet (I don't touch Java often, so I'm a bit slow with it) but I'm looking forward to doing so. Cheers :)
sampablokuper
ahh right, i didnt realise it was 2 separate projects so have updated my answer accordingly. Email Michael if you can't get it working, he is a really nice guy so am sure that he would help you get it going.
AutomatedTester
My impression is that this wouldnt help validate after DOM manipulation?
Paul Biggar
From speaking to Michael it should work because you are getting selenium/webdriver to inject a bit of html and then assert if its the correct colour. When you inject the code or tell it to run is up to you really.
AutomatedTester
+1  A: 

I've been pondering on the same front. Here's my progress so far.

The JavaScript attribute document.documentElement.outerHTML can be accessed in some browsers, e.g. Safari and Chrome, but not in Firefox. This will show you the result of DOM manipulations but won't give you quite the full source code for the page. If you inject some JavaScript that will store the value of document.documentElement.outerHTML after your DOM manipulation has been carried out, you should be able to get Selenium to type that value into the "direct input" textarea of the W3C Validator and submit it. You'd need to prepend and append the value appropriately in order to make it a valid (X)HTML document. I haven't managed to get all these steps working yet from Selenium.

prepending "view-source:" to the URL (as done here) works in some browsers, e.g. Firefox and Chrome, but not in Safari. It won't show you the result of DOM manipulations. I can't seem to get Selenium IDE to open an address beginning with "view-source:" however; it always prepends a forward slash, and I'm not sure how to avoid that.

I'd be interested to know which other approaches people have tried, and whether they've had any success!

sampablokuper