Browser Tests using Coded UI - watin

How do I write the following tests which is written in Watin using CodedUI
I am trying to learn CodedUI but really struggling
[Test]
public void SearchForWatiNOnGoogle()
{
using (var browser = new IE("http://www.google.com"))
{
browser.TextField(Find.ByName("q")).TypeText("WatiN");
browser.Button(Find.ByName("btnG")).Click();
Assert.IsTrue(browser.ContainsText("WatiN"));
}
}

You might find a bit of help from one of these links:
http://blogs.msdn.com/b/gautamg/archive/2009/12/18/why-is-coded-ui-test-generated-code-not-a-straight-line-code.aspx - this is a blog post on MSDN that talks about the code generated by recording a test. It has a sample project attached you might consider looking at.
As I said in your other question, the best way is to create recordings. There is a nice little walkthrough also on MSDN, here: http://msdn.microsoft.com/en-us/library/ff977233.aspx. This one shows you the process of recording a test and generating and maintaining the code for it.
I would strongly encourage you to thoroughly read those two articles.

WatiN and the Visual Studio Coded UI tests are different. 'Coded UI tests' refer to Visual Studio's own UI testing tools. WatiN is a different test framework.
Personally, I don't like WatiN because it takes a long time to write tests and it does not playback tests reliably (at least from my experience). The Visual Studio Coded UI tests I prefer more because they provide a test recorder which auto-generates the test code and a a graphical test editor, while still giving you the flexibility to edit the test code yourself.
I have found more success with Visual Studio coded UI tests. The best thing you can do to get started with creating tests using VS Coded UI tests is to read the MSDN documentation, particularly be sure to read the 'Anatomy of a Coded UI Test' to get a hang for the generated code. Read each article in the documentation as it goes through the whole process, from recording tests to editing, maintaining them and best practices.
Also, make sure you have downloaded Visual Studio Feature Pack 2 to be able to use the graphical test editor which is useful when creating and editing tests.
The really off-putting thing about WatiN (apart from its unreliability) is that it doesn't really have a proper documentation, so it can be difficult to know how to handle elements. It's my opinion that you should ditch WatiN, and go with the Visual Studio coded UI tests, if they are available to you. The great thing about the Visual Studio coded UI tests is that it can deal with minor changes to the UI and automatically adapts. WatiN can do no such thing.

Related

Can ReSharper run unit tests automatically

Is it possible to automatically run unit tests while you work without compiling or running them manually? I am aware that NDepend allows you to do so, but I would prefer to use the ReSharper suite.
This has been available since dotCover 10. See the dotCover documentation for details.
This adds a new panel "Continuous Testing Session" as well as a new status icon in the gutter.
Note that Visual Studio also has this feature, known as Live Unit Testing.
Not possible with Resharper at the moment, you will need something like NCrunch that runs your unit tests continuously in the background, highlighting code that breaks them as you write it and fails your tests.
Edit: At the time of my response it wasn't possible to do this with ReSharper but now in Version 10 it is, see Drew Noakes's answer. You could still give NCrunch a try as it continuously runs your tests in the background even without doing an explicit save.

Watin and Telerik Controls

I would like to ask if anybody knows an easy way to test Telerik controls with Watin.
We are about to start using it but before we do I wanted to see if there is anything I would need to know.
The problem that I can see we will be having is that if even smallest thing changes then all our tests will also break.
Any suggestion is greatly appreciated.
The problem that I can see we will be having is that if even smallest thing changes then all our tests will also break.
This could/will be true of all portions of your pages, including Telerik controls, depending on your test structure. Ideally, your elements will have IDs assigned and you'll use Page classes and custom control objects to remove all HTML references from your actual test code. Then if something changes on the webpage (or in a control), you verify the change is expected, then you change the WatiN page code (or control code) and re-run your tests.
The WatiN page class primer is here: http://watinandmore.blogspot.com/2009/06/introducing-page-class.html
Basically, you want to have your test code look like myPage.PickDate("3/29/2012") and not like ie.Tables[3].TableRows[2].TableCells[4].Textbox(Find.ByClass("datePicker")).TypeText("3/29/2012")
Changes can, do and should result in failing tests, however, I can attest that with a good page (or control) class setup that abstract away the HTML DOM and other specifics leaving non-HTML-filled test code, means that when changes do happen they are most often easy to get working again.
Note: Selenium also has a Page class concept, but I have not used it very much as of right now. Bottom line: If you write a lot of tests that reference the HTML DOM directly in test code, you're setting yourself up for a maintenance headache regardless of if you go with WatiN or Selenium or whatever.
Added: As to your original question: Can you work with Telerik controls in WatiN? Yes you can most likely, but depending on the control you may need to get a bit creative, possibly even calling javascript methods from within your test (page object ;) ). I've been stumped by a couple controls (non-Telerik) but most I've eventually figured out.
I realize you asked about WatiN, and I know I'll probably get downvoted into oblivion, but I might recommend Selenium instead. It seems to be more widely used and when we were evaluating the two we found Selenium easier to work with because of the Firefox plugin to record/generate the tests. This meant that our non-technical folks could set up the tests.
Since then we've successfully used Selenium to test ASP.NET sites that utilize Telerik controls. I only ran into one issue, with the RadNumericTextBoxes, which I've documented a fix for here: http://www.msigman.com/2012/02/entering-radnumerictextbox-selenium-webdriver-1-6-0/.
I'm currently in the process of writing a how-to guide for doing it: http://www.msigman.com/2012/03/automated-testing-asp-net-web-application/ (shameless plug).
You should also consider evaluating Telerik's Test Studio, our functional automation test tool. (Disclosure: I'm their evangelist for Test Studio.)
Test Studio really shines when you're working with Telerik controls. You'll get some great additional functionality around being able to dive deeper in to verifications and actions around the controls.
Even more importantly, Test Studio handles centralizing locators and pages by default, so you don't need any additional effort to best manage your UI changes.

Is there an API for running Visual Studio Unit Tests programmatically?

Is there an API for running Visual Studio Unit Tests programmatically?
Running MSTests.exe with Process.Start() does not work in the current scenario. What I'm looking for is something like the NUnit SimpleTestRunner.
Any ideas?
/Erik
You're correct in that there's no public API for the mstest framework. I wrote a manual replacement for mstest one day to see how hard it was, and it's not as simple as it looks (particularly if you want to take advantage of more than one CPU core), so beware of going down this path.
Personally I've always just run mstest.exe programatically and then parsed the resulting .trx XML file. Are there any particular reasons why you can't use Process.Start to run it?
P.S. Some of the strange behaviour of mstest.exe are solved if you pass the /noisolation command line parameter - give that a go if you feel so inclined :-)
Update: Erik mentions he wants to run the test API in the current thread so he can set the thread culture for globalization issues.
If you run a unit test under the debugger, you'll notice that mstest creates a bunch of threads, and runs all your tests in different threads, so this isn't likely to work even if you could access the API.
What I'd suggest doing is this:
From your test "runner" application, set an environment variable
Run mstest pointing it at the specific tests
Add a [ClassInitialize] (or [TestInitialize]) method which reads this environment variable and sets the culture
Profit!
After taking a deep dive with reflector into MSTest.exe and further down into the Visual Studio Unit Test stack, I found that the API used by MSTest is sealed up and made private so that i cannot be used from the outside.
Why not using Reflector and seeing how NUnit SimpleTestRunner is running the tests... And then use this technique...
You can make use of the Microsoft REST API's for TFS to run ms tests. Please refer to the documentation here.
I've linked to "Call a Rest API" so that you can see how you'd go about calling one of the REST API's for TFS.
Note that if your tests are linked to the build, they should run automatically every time a build is queued.
Here is the link to Run Functional Tests.
I've also discovered an article on using the TFS SDK API to run tests. Here is that link as well: Link to API Article

Using the VS Web Test Classes outside Visual Studio

I use HttpWebRequests pretty extensively - I've found that the Visual Studio Web Test classes (i.e. WebTestRequest and WebTestResponse etc) offer a lot of really nice methods that i'd love to start using.
But I can't seem to find a way to use these classes outside of the Visual Studio's testing framework. Has anyone done this?
Will,
WebTestRequest and WebTestResponse aren't designed to be used like HttpWebRequest and HttpWebResponse. They're mostly containers for passing various request and response data into and out of the web/load test engine in Visual Studio or MSTest.
I wrote both of the classes and even I find myself wishing I could use them for other purposes :) I've definitely had to reinvent similar querystring parameter parsing logic for other projects since then.
Josh

What are the best features of Visual Studio Team System?

Microsoft has a lot of stuff in there, but I'm wondering what features of Visual Studio Team System people really like and really use.
I'm specifically thinking about Team System as opposed to plain old Visual Studio.
What makes it worth the price?
I use the Development version of VSTS2005 and evaluating 2008. My top picks:
Profiler
Coding guidelines -- rules enforcement part
My favorite
Profiler
Integrated Testing Environment: I know a lot of people prefer other test frameworks but having the integration is just sweet.
FxCop
Some of the best features come from adding Team Foundation Server:
Continuous integration builds can be set up to run unit tests on every build
Code coverage figures can be gathered based on the unit test run
Reports of build success, unit test success, code coverage %, etc. can be produced daily
Code check-in can mark a work item (bug report) fixed, or can start the workflow to do so
It not only gives the developers a better idea what's going on with their code, and of how to fix it (unit tests, code coverage, code analysis), it also gives Management an overall picture of the same, without having to come around and bug the developers individually.
I like the line-by-line blame, profiler (as mentioned), but more importantly, I like the reports it produces, such as defect rates over time.
However, even though there are plenty of features that I like, I certainly don't think it provides good value for money.

Resources