gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.08k stars 574 forks source link

Testing the DOM that templates output #908

Open arschles opened 6 years ago

arschles commented 6 years ago

Storytime

A few weeks back, I realized that my templates were a mess, so I went to rewrite a bunch of them. When I did so, I broke a bunch of links and the next release was a big mess. The old Google Analytics chart was down and to the right. Sad face.

I didn't really want to build a whole ui testing thing (like in #888) but I figured something that got me kinda-sorta-almost there would do just fine for now.

My Interim Solution

I reached for goquery and hooked it up to my actions tests. Any time I run a test on an action that outputs HTML, I load that HTML into a goquery document and test some basic things about the structure of the HTML. Mostly, I just wanna make sure that links go to the right place. I'm gonna add some tests to make sure stuff doesn't show up when the user is logged out.

What to Do Now

I have just a little bit of code in a private project right now and it's decently generalized. I figure there's a ton more use cases that folks could come up with.

Any interest in putting the code here, or maybe in suite?

See Also

888 is about selenium testing. This goquery-based thing would only work on statically output HTML, would not be able to simulate events, and probably a bunch more limitations. It's somewhere between nothing to see here, these are just templates and selenium all the things!

stanislas-m commented 6 years ago

I'd love to see some examples for that. 👍 For the buffalo integration part, I think you're right, that might be better to have it in the suite project. In terms of HTML (or even XML) render testing, a complementary solution can be https://github.com/antchfx/xpath (or maybe I'm too old-fashioned). Thoughts?

arschles commented 6 years ago

@stanislas-m I'll submit a small PR to the suite library when I get a chance, and link it to here. I'm sad (happy?) to say I never learned much about XPath, so I don't know if it'd be complementary?

stanislas-m commented 6 years ago

Xpath is a query tool for XML, it's a common solution when you want to extract data from an XML tree. See it as a more general solution, with advanced queries capabilities.

In my opinion, goqueries can be a good solution for simple tests, though.

arschles commented 6 years ago

I haven't forgotten about this... I've gotten to the point where the code is fairly generalized and I can extract it fairly easily. Hoping to get this done soon, and thanks so much for being patient!

😄