WatiN won't work with MSpec - UnauthorizedAccessException
I'm trying to get WatiN working with MSpec in VS2010, using TestDriven.Net. My code is as follows:
[Subject("Whatever")]
public class when_on_home_page {
private static IE browser;
Establish context = () =>
browser = new IE();
Because of = () => {
browser.GoTo("http://localhost:1234/");
browser.WaitForComplete();
};
It should_show_add_details_link = () =>
browser.Link(Find.ByValue("Add Details")).Exists.ShouldBeTrue();
}
When I run this, the browser loads the page, but when it checks for the link, I get:
Whatever, when on home page
» should show add details link (FAIL)Test 'should show add details link' failed: System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at mshtml.IHTMLElementCollection.tags(Object tagName) at WatiN.Core.Native.InternetExplorer.IEElementCollection.GetElementsByTag(String tagName) at WatiN.Core.NativeElementFinder.FindElementsByTag(String tagName) at WatiN.Core.NativeElementFinder.d__2.MoveNext() at WatiN.Core.ElementFinder.FindFirst() at WatiN.Core.Element.FindNativeElementInternal() at WatiN.Core.Element.get_Exists() NavigationSpecs.cs(20,0): at RoboWeb.Specs.when_on_home_page.<.ctor>b__2() at Machine.Specifications.Model.Specification.InvokeSpecificationField() at Machine.Specifications.Model.Specification.Verify()
Having开发者_StackOverflow looked around, I checked the following:
- VS is running as administrator
- TestDriven.Net runs everything in STA mode, apparently
- IE has 'protected mode' turned off
- I can't add the site to trusted sites, presumably because it's running locally
- I call WaitForComplete(), and have also tried Thread.Sleep()
I also tried it with FireFox, but that just crashes.
Any idea what I'm doing wrong here?
These specifications work for me. Actually, I this is my first WatiN spec; I wrote while trying to reproduce the error you received. :-)
The only special thing I had to do was to turn off IE's Protected Mode. I ran the specs
- on the command line (see
WebSpecs-Watin.cmd
in the repo, please compile the specs project and run the web application from VS before running the cmd) - with the TD.Net runner
- with ReSharper
They succeed in all three cases. I'm not running as Administrator.
Can you please try to reproduce the error with the project linked above? If you don't have Git installed, there's a download button in the top section.
System.UnauthorizedAccessException can happen in a number of circumstances. If the page has not loaded fully and you try to access an object, as well as simply not being the right user. I would start by debugging through the script manually, and running the test runner as an administrator.
If that works, then try the combinations of admin & full speed, non-admin and full full speed as well as admin and debugging. Then you should have a good idea as to what the real problem. In some rare circumstances (with early early versions of WatiN,) I had to wrap calls in a try - catch then handle this exception, and then re-try after a short wait.
精彩评论