无法使用带有CodedUi的Spec Flow启动浏览器

时间:2015-03-18 08:04:47

标签: coded-ui-tests specflow

尝试使用启动浏览器 BrowserWindow browser = BrowserWindow.Launch(); 在Visual Studio 2012 Ultimate中获得异常

我已经包含以下dll:

TechTalk.SpecFlow nunit.util nunit.core Microsoft.VisualStudio.QualityTools.UnitTestFramework NUnit.VisualStudio.TestAdapter nunit.core.interfaces nunit.framework Microsoft.VisualStudio.TestTools.UITesting

和使用具有编码UI的规范流获取运行时异常的创建测试方法:

测试方法specflow_CodedUI.SpecFlowFeature1Feature.AddTwoNumbers抛出异常: System.IO.FileNotFoundException:未能加载文件或组件' Microsoft.VisualStudio.TestTools.UITest.Playback,版本= 11.0.0.0,文化=中性公钥= b03f5f7f11d50a3a'或其中一个依赖项。系统找不到指定的文件.WRN:程序集绑定日志记录已关闭。 要启用程序集绑定失败日志记录,请将注册表值[HKLM \ Software \ Microsoft \ Fusion!EnableLog](DWORD)设置为1.

3 个答案:

答案 0 :(得分:0)

你可能想尝试清理你的解决方案,删除你的包文件夹,然后重建 - 我前一段时间遇到了类似的问题,这样就解决了这个问题。

显然,如果您需要Microsoft.VisualStudio.TestTools.UITesting引用的程序集绑定重定向,请确保它是正确的。

答案 1 :(得分:0)

它不能直接工作,因为specflow不会在单元测试中生成[CodedUITest]属性。请遵循:https://github.com/techtalk/SpecFlow/wiki/Using-SpecFlow-with-CodedUI-API

答案 2 :(得分:0)

我一直在使用Process.Start来启动浏览器:

public class Browser : IDisposable
{
    private System.Diagnostics.Process currentBrowserProcess;

    private BrowserWindow window;

    public BrowserWindow Window
    {
        get
        {
            if (window == null)
            {
                window = BrowserWindow.FromProcess(currentBrowserProcess);
                window.CloseOnPlaybackCleanup = false;
            }

            return window;
        }
    }

    public Browser(string url)
    {
        Open(url);
    }

    public void Dispose()
    {
        Window.Close();
        window = null;
        currentBrowserProcess = null;
    }

    public void Open(string url)
    {
        if (currentBrowserProcess == null)
        {
            currentBrowserProcess = System.Diagnostics.Process.Start("iexplore.exe", string.Format("\"{0}\"", url));
        }
        else
        {
            Window.NavigateToUrl(new Uri(url));
        }
    }
}

使用它:

Browser browser = new Browser("http://localhost/foo");

您可以将其放在ScenarioContext中以便安全保管。您可以使用browser.Window访问浏览器的“窗口”对象。将自己的辅助方法添加到此类中,以便于查找元素。

public HtmlButton Button(string buttonText, UITestControl container = null)
{
    HtmlButton element = new HtmlButton(container ?? Body);
    element.FilterProperties[HtmlButton.PropertyNames.InnerText] = buttonText;
    return element;
}

使用:

HtmlButton button = browser.Button("Click Me!");

Mouse.Click(button);