Internal DSLs For Automated Functional Testing


Published on

Automated functional testing is one of the hardest things to get right on a project. Many people with high hopes set out to develop fully automated regression tests, only to be caught up in the tangle of fragile tests which are always broken. In this session I will present a clean way to build a framework using free tools to develop an internal Domain Specific Language custom to the application being testing. I will show the benefits of this approach versus the record and modify methods of most automated testing tools. In addition, I will talk about a real working practical example I have developed for the Dept of Health in Welfare in testing their new case management system.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Internal DSLs For Automated Functional Testing

  1. 1. Boise Code Camp 2010 John Sonmez
  2. 2. <ul><li>Automated regression testing is hard </li></ul><ul><ul><li>Fragile tests </li></ul></ul><ul><ul><li>Difficult to write tests </li></ul></ul><ul><ul><li>Tools are expensive </li></ul></ul><ul><ul><li>Recording is not flexible enough </li></ul></ul><ul><ul><li>Tests failures don’t indicate code problem </li></ul></ul>
  3. 4. <ul><li>#1 Problem with automated tests is the tests break easily. </li></ul><ul><li>Screen changes cause multiple tests to break. </li></ul><ul><li>Functionality or text changes cause tests to break. </li></ul><ul><li>Why? Multiple tests are dependant on parts of the user interface. </li></ul>
  4. 5. <ul><li>Recording is easy at first, until you need variables or reuse. </li></ul><ul><li>Scripting the tests requires knowledge of HTML, CSS, Javascript and a high level programming language such as C# or Java. </li></ul><ul><li>QA or business person thinks in terms of domain knowledge, not button clicks and HTML divs. </li></ul>
  5. 6. <ul><li>Most of the record tools have license costs from several hundred to several thousand dollars. </li></ul><ul><li>Cost prohibitive, so only certain people in organization get license. </li></ul><ul><li>Support contracts. </li></ul><ul><li>Upgrade costs. </li></ul><ul><li>“ Automation Experts” trained in the vendor specific tool are expensive. </li></ul>
  6. 7. <ul><li>Recording seems to work at first, but then the scenarios get more complicated. </li></ul><ul><li>When the UI changes scripts have to be re-recorded. </li></ul><ul><li>Recording limits the kinds of testing you can do. </li></ul><ul><ul><li>Test result interactions </li></ul></ul><ul><ul><li>Check against external data source </li></ul></ul>
  7. 8. <ul><li>False failures reduce the confidence in the tests. </li></ul>
  8. 9. <ul><li>Start treating test code like real code. </li></ul><ul><li>Instead of building “test scripts”, build a “testing application.” </li></ul><ul><li>Apply the same tools of good software craftmanship to test code craftmanship. </li></ul><ul><li>Bridge the gap between domain knowledge and test writing. </li></ul>
  9. 10. <ul><li>User: Add a person to the case. </li></ul><ul><li>Tester: </li></ul><ul><ul><li>1. Click button with id=“loginPage123”. </li></ul></ul><ul><ul><li>2. Set focus userName field. Add username to field. </li></ul></ul><ul><ul><li>3. Set focus passWord field. Add password to field. </li></ul></ul><ul><ul><li>3. Click button with id=“login”. </li></ul></ul><ul><ul><li>4. Check HTML title for text containing “successful”. </li></ul></ul><ul><ul><li>5. Click div in top navigation with id of addPerson. </li></ul></ul><ul><ul><li>6. blah blah blah… blah blah blah </li></ul></ul>
  10. 11. <ul><li>User: Add a person to the case. </li></ul><ul><li>Tester: </li></ul><ul><ul><li>1. Case.AddPerson(“Joe Smith”); </li></ul></ul><ul><ul><li>2. AssertTrue(Case.isPersonOnCase(“Joe Smith”)); </li></ul></ul><ul><li>Abstraction… Business language becomes testing language. </li></ul>
  11. 12. <ul><li>Internal Domain Specific Language (DSL) </li></ul><ul><li>A language to write tests which uses the same or similar vocabulary as the business or user. </li></ul><ul><li>Framework built specific to the application, not a general purpose testing framework. </li></ul><ul><li>Framework developed by developers and testers collaboratively. User of framework is test automation resources. </li></ul>
  12. 13. <ul><li>Domain Specific Language </li></ul><ul><li>According to Wikipedia: A programming language or specification language dedicated to a particular problem domain, a particular problem representation technique, and/or a particular solution technique. </li></ul><ul><li>Basically a custom programming language designed for a specific problem or set of problems. </li></ul>
  13. 14. <ul><li>A DSL that is a set of APIs still in the native language used to approximate a full DSL. </li></ul><ul><li>Writing a language is hard. </li></ul><ul><li>Writing an API is easier. </li></ul><ul><li>Objected oriented programming techniques already lead us in this direction. </li></ul><ul><li>We use internal DSLs in English all the time. </li></ul><ul><ul><li>Company acronyms and language. </li></ul></ul><ul><ul><li>Programmer speak </li></ul></ul><ul><ul><li>Slang </li></ul></ul>
  14. 15. <ul><li>Remember the gap? </li></ul><ul><li>Bridge the gap by building an application specific layer that exposes a DSL for writing tests. </li></ul><ul><li>Hide the details about web pages, HTML, CSS, Javascript from the tests. </li></ul><ul><li>Easy to use, fluent design. </li></ul>
  15. 17. <ul><li>The web browser provides us a layer we can use to abstract away the notion of parsing HTML, CSS and Javascript. </li></ul><ul><li>Without the browser, we would have to write code to open HTTP connections and GET web pages. </li></ul><ul><li>We would have to parse CSS, and Javascript, to verify functionality. </li></ul>
  16. 18. <ul><li>Web testing framework interacts with the browser for us. </li></ul><ul><li>Operates on the DOM the browser provides. </li></ul><ul><li>Without the Web Testing Framework, we would have to write code to manipulate the browser, and change it’s state through COM or some other interface. </li></ul>
  17. 19. <ul><li>We build this on top of the Web Testing Framework. </li></ul><ul><li>Abstracts away all concepts having to do with browsers and HTML. </li></ul><ul><li>Uses the Web Testing Framework to translate business language into web functionality. </li></ul><ul><li>Contains all our changing things about the application. </li></ul><ul><li>Without this layer our code would contain hard coded ids of elements and would have to deal with page layouts. </li></ul>
  18. 20. <ul><li>Uses only the application framework. </li></ul><ul><li>Uses the internal DSL. </li></ul><ul><li>Simple </li></ul><ul><li>Written as close to business language as possible. </li></ul>
  19. 21. <ul><li>A good starting place is to start writing the test. </li></ul><ul><li>Pretend like you don’t know anything about the web, but you do know about the application. </li></ul><ul><li>Think “fluent.” </li></ul><ul><li>Think easy to use. </li></ul>
  20. 22. <ul><li>Web based store. </li></ul><ul><li>Write a test to create a new account and buy bananas. </li></ul><ul><ul><li>Account.Create(“Joe Smith”, “123 Main Street, Boise ID 83713”, “ ”); </li></ul></ul><ul><ul><li>Category.Fruits.GoTo(); </li></ul></ul><ul><ul><li>Cart.AddItem(Items.BANANAS, 1); </li></ul></ul><ul><ul><li>Cart.CheckOut(PaymentType.CreditCard); </li></ul></ul><ul><li>Simple, business logic based syntax. </li></ul>
  21. 23. <ul><li>Similar to Test Driven Development when unit testing. </li></ul><ul><li>Build the business objects and methods needed to make the test syntax possible. </li></ul><ul><li>Don’t worry about all the variations, only add what is needed for the test you are working on. </li></ul><ul><li>Here the identifiers are hard coded in the business objects, NOT in the tests. </li></ul>
  22. 24. <ul><li>Account.Create </li></ul><ul><ul><li>Make an account class with a static method called create, that takes in a name, address, and email. </li></ul></ul><ul><ul><li>In the method use the web testing framework to navigate to the account creation page and enter the information into the fields. </li></ul></ul><ul><ul><li>After the information is entered create the new account. </li></ul></ul><ul><ul><li>Default non-important selections. (You can make them specific later if needed.) </li></ul></ul>
  23. 25. <ul><li>Actual test layer. </li></ul><ul><li>Think about how to achieve the same amount with fewer lines of code. </li></ul><ul><li>The simpler and fewer lines of code to write the tests, the easier to write the tests. </li></ul><ul><li>Eliminate variables that are not important to the test. </li></ul><ul><li>Eliminate any steps that cannot vary. </li></ul>
  24. 26. <ul><ul><li>Do we are about the account name, address, etc? </li></ul></ul><ul><ul><ul><li>Account.Create(“Joe Smith”, “123 Main Street, Boise ID 83713”, “ ”); </li></ul></ul></ul><ul><ul><ul><li>becomes </li></ul></ul></ul><ul><ul><ul><li>Account.Create(People.Base_Case); </li></ul></ul></ul><ul><ul><li>Do we need to explicitly go to the fruits page to add a banana? </li></ul></ul><ul><ul><ul><li>Category.Fruits.GoTo(); </li></ul></ul></ul><ul><ul><ul><li>Cart.AddItem(Items.BANANAS, 1); </li></ul></ul></ul><ul><ul><ul><li>becomes </li></ul></ul></ul><ul><ul><ul><li>Cart.AddItem(Items.BANANAS, 1); </li></ul></ul></ul>
  25. 27. <ul><li>Created for Dept of Health and Welfare IBES System to automate web testing. </li></ul><ul><li>Creates test cases in very few lines of code. </li></ul><ul><li>Able to write a test cases which goes through 30+ screens in a few minutes. </li></ul>
  26. 28. <ul><li>Test frameworks </li></ul><ul><ul><li>Watij ( ) </li></ul></ul><ul><ul><li>Watin ( ) </li></ul></ul><ul><ul><li>Watir ( ) </li></ul></ul><ul><li>My Blog, posts on automation and Watij. </li></ul><ul><ul><li> </li></ul></ul><ul><li>Contact me </li></ul><ul><ul><li>I can help you build your framework </li></ul></ul><ul><ul><li>[email_address] </li></ul></ul><ul><ul><li>Twitter jsonmez </li></ul></ul>