Automated testing of Magento with Watir
Few months ago I was attending this web conference where among various topic one covered the “Automated testing”. To be honest I never looked into any “automated testing” tools until then. This was the first time I heard about this tool called Watir. Shortly put, Watir (pronounced water), is an open-source (BSD) family of Ruby libraries for automating web browsers.
First thing some of you might notice is that Watir is written in Ruby, and your Magento is written in PHP so how does this relate? Actually it does not. Meaning Watir is not one of those include it in your script then use it tools. Basically it does not interact with your code whatsoever. Consider it as fully independent/separated app that runs desired browser and simulates real user sitting in front of the browser doing all sort of input text filling and form submitting, link clicking etc. Watir also checks results, such as whether expected text appears on the page.
All you need to do is to install Ruby language, then Watir, then… you are done. Watir website has great installation instructions so no need for me to write one here.
The guy whose lecture I listened on that conference is making his living as code tester. So, listening him giving praises to Watir got me thinking. My first impressions are extremely positive.
I myself am not a Ruby developer, as I code PHP most of the time and some Java on a side. However it took me less than hour to write this little Watir test class that when run starts the browser, goes to Magento demo site, goes to cellphones category, adds a cellphone to cart, then does a checkout as a “guest”.
class Magento_GuestAddToCart < Test::Unit::TestCase def test_GuestAddToCart browser = Watir::Browser.new browser.goto("http://demo.magentocommerce.com/electronics") browser.goto("http://demo.magentocommerce.com/electronics/cellphones") browser.link(:href, "http://demo.magentocommerce.com/samsung-mm-a900m-ace.html").click browser.text_field(:id, "qty").set("3") browser.button(:title, "Add to Cart").click browser.button(:title, "Proceed to Checkout").click browser.radio(:id, "login:guest").set browser.div(:id, "checkout-step-login").button(:text, "Continue").click browser.text_field(:id, "billing:firstname").set("John") browser.text_field(:id, "billing:lastname").set("Doe") browser.text_field(:id, "billing:company").set("My company name here") browser.text_field(:id, "billing:email").set("email@example.com") browser.text_field(:id, "billing:street1").set("Sample street name1") browser.text_field(:id, "billing:street2").set("Please ignore this order, developer test") browser.select_list(:id, "billing:region_id").set("California") browser.text_field(:id, "billing:city").set("Alamo") browser.text_field(:id, "billing:postcode").set("94507") browser.select_list(:id, "billing:country_id").set("United States") browser.text_field(:id, "billing:telephone").set("0038531399222") browser.div(:id, "checkout-step-billing").radio(:id, "billing:use_for_shipping_yes").set browser.button(:title, "Continue").click sleep 4 browser.radio(:id, "s_method_flatrate_flatrate").set browser.div(:id, "checkout-step-shipping_method").button(:text, "Continue").click sleep 4 browser.radio(:id, "p_method_checkmo").set browser.div(:id, "checkout-step-payment").button(:text, "Continue").click sleep 4 browser.div(:id, "checkout-step-review").button(:text, "Place Order").click #browser.close end end [/sourcecode] Pretty nice, right?! So my question is, if this is something you can do in 15 min or less (when you get the hang of it), what else can you do? After tonight, I see Watir something as I will most likely be using in my projects from now on. Great thing about Magento is that it has this "specific functionalities" like "Cart page", "Register page", "My account page", "Checkout page" that you can write tests for and then simply run them on your new theme to see if anything got broken, etc. Especially the One Page Checkout with various payment and shipping gateways, etc. Not to mention entire Admin section of Magento when you are building your own custom admin modules. I would strongly encourage everyone interested who hasn't checked it out yet to spare some time and write a test or two.
I used Watir in 2006 to automate all test cases (register, login, sell, review, my account, etc) in a site like ebay and it was very robust.
I assume that seven years later it will be more usefull than in that time.
Magento just realeased the Magento Test Automation Framework:
Getting it working can be confusing, especially on Linux, but luckily I’ve written a tutorial covering the topic! 🙂
Does waitr support test recording as well? and can I use the same testscript on different browsers ?
Hi there Branko. Thanks for the tip!
Please, I´m starting the study with Watir+Ruby on Magento after reading this article.
I would like to know if the Inchoo team really adopted this software to test Magento features, and if you found any difficult to write automated test with it.
Thank you so much.
Seems like a good autotesting system. Will give it a try.
for those of you who had the same problem as i did, and who are working on mac, have a look at this.
sorry i just read the above comment. im unable to resolve the same issue.
getting a jssh runtime error with firefox.
Im really so keen to use Watir but im immediately having issues. ive been through the docs quiet thoroughly but im struggling to get it working.
a pity as i would really like to use it…
Nice topic, anyways i tried it but getting error unable to start JSSH
nice topic~ thx for sharing~
Seems like a very useful tool to supplement selenium (see Matthias Zeisses post further up). Thanks for the post!
Wow… Now, that’s what they have been talking about in Mallorca? 😉
Anyway, this sounds really great thing to know for the development of larger modules. I’m sure that especially you and your team will find it useful on your type of projects.
The procedures are clear. You have a very nice post. keep posting!!
Did you give Selenium (http://seleniumhq.org/) a try? I think you may like it too. There is a Firefox Plugin to record the tests, PHP code can be created automatically and you can run the tests across multiple browsers and platforms.