Does anyone have an examples or experience of writing UI automation in a functional language? I'm currently learning F# to get a better understanding of functional concepts and I'm having difficulty working out how an automated UI test would be structured in a functional language - seems easy to use the same page/screen object patterns I would in Java or C#, but given lack of experience I'm curious if there's a different approach I've missed.
-
1How would you automate UI testing in an imperative language? Automating UI testing has almost nothing to do with the paradigm you're using since you're likely to use a third party tool to do this sort of thing anyway. – Onorio Catenacci Aug 08 '12 at 12:54
-
You may look at [WebSharper](http://websharper.com/home) for Web UI and several [works](http://www.navision-blog.de/2012/03/22/wpf-designer-for-f/) for WPF, but in general, your question indicates no search effort. – Be Brave Be Like Ukraine Aug 08 '12 at 12:57
-
@Onorio - actually, I'm not likely to use a 3rd party tool, hence asking this question. In an imperative object oriented language, I'd structure my tests by creating objects representing each page/screen, create methods representing common interactions and use those objects for automation. Could do this in F# from what I can see, but was wondering if there was a better approach that functional languages allow – Dave Aug 08 '12 at 15:30
-
@bytebuster - can't see what those links have to do with UI automation. In general, your answer indicates no reading comprehension. – Dave Aug 08 '12 at 15:31
-
Ok--I was thinking--when you were talking about automated UI testing you were discussing Mercury WinRunner or a similar sort of tool. In my experience, using a tool like WinRunner for UI testing is worth every second of developer time it saves and then some. Building your own automated UI testing is error-prone and may hide problems--at least in my experience anyway. – Onorio Catenacci Aug 08 '12 at 15:40
-
I've found the advantages of working in a language that isn't VbScript, with a decent IDE and access to things outside the UI vastly outweigh the disadvantages - with Selenium, White, Telerik etc so easily available it's not like I'm having to completely reinvent the wheel each time. – Dave Aug 08 '12 at 15:42
2 Answers
Your biggest win with using a functional language will come from not having to use classes at all, but being able to when they are the right answer. Also, F# allows for a nice clean 'dsl' looking test suite because of type inference and the syntax. Common actions (example: logging in) are easily abstracted into a function and called within a test. Any function that is very specific to a page can be added to that page's module along with its defining features (css selectors etc).
Here is an example of a test written with canopy
test(fun _ -> //description of the test describe "registering a user" //go to root url "/" //ensure that you are on the login page on "/Account/LogOn" //click the registration link click "form a[href='/Account/Register']" //verify that you were redirected on "/Account/Register" //set the value of the input to email address specified "#Email" << "username@example.com" //set the value of the input to "Password" "#Password" << "Password" //set the value of the input to "PasswordConfirmation" "#PasswordConfirmation" << "Password" //click the register button click "input[value='register']" //verify that you were redirected on "/" //log off after test url "/account/logoff" )
More about canopy
I've written a Web automation framework/library in F# (also one in Ruby) and thus far, while I wouldn't consider its style to be functional, it doesn't have any classes. Almost everything is a function. Your test suite is a list of functions that are run.
With < 500 LoC there are only 3 modules, the main set of functions to interact with your page, a simple test runner, and some configuration variables. At this point this paradigm has worked really well for me. I don't use classes for page definitions because for me, a page definition is as simply the css selectors I use. A module with a bunch of values fulfills this need nicely.
Give it a shot, I think you will find it to be an excellent way to accomplish your goals.
Sorry first time post so it wont let me show more links. Look on github and you can see the source at /canopy/canopy/canopy.fs

- 9,091
- 5
- 34
- 46

- 498
- 3
- 10
-
Wow! 16 months on SO and this is your first answer. Sure you don't have any other hidden treasures? :) – Benjol Aug 09 '12 at 05:36
-
You seem to answer your own question, F# supports OOP, OOP is a good fit in this case, and the distinction between imperative vs functional is separate from structure in this case.
So use classes and methods just like you would in C#, but write the unit tests themselves in a functional manner.

- 18,775
- 1
- 33
- 64