【问题标题】:Visual Studio 2010 (win 7 x64) Selenium Unit Test Keeps PendingVisual Studio 2010 (win 7 x64) Selenium 单元测试保持挂起
【发布时间】:2013-03-11 12:25:53
【问题描述】:

我尝试了几个小时为自定义网站使用 Selenium 驱动程序进行单元测试。问题是,我的第一个测试用例始终处于未决状态。其他的我没写。我尝试了几个不同的教程,但没有一个有效。我做了什么:

  • 通过 NuGet 控制台安装 Selenium WebDriver(有参考);
  • 通过 NuGet 安装 Selenium WebDriver.Support(有参考);
  • 我什至关闭了防火墙;

最后,我正在关注此页面上的教程dotnet-developer.de!本教程也不起作用。它是在 vb.net 中给出的,但我已经用 C# 翻译如下:

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;

namespace FastMenuSeleniumTests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class UnitTest1
    {

        public IWebDriver driver;
        const string BaseURL = "http://www.bing.com";

        public UnitTest1()
        {
            // do nothing
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void TestInitialization() 
        {
            driver = new FirefoxDriver();
            //System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en");
            //System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
        }
        //
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void TestCleanup() 
        {
            driver.Quit();
        }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            IWebElement SearchBox = default(IWebElement);
            IWebElement FirstResult = default(IWebElement);

            //Go to Bing Homepage
            driver.Navigate().GoToUrl(BaseURL);

            //Get Handle for Searchbox
            SearchBox = GetWebElement(driver, By.Id("sb_form_q"), 10);

            //Enter Search Text
            SearchBox.SendKeys("dotnet-developer.de");

            //Different ways to start the search
            //Method 1: Press ENTER
            SearchBox.SendKeys(Keys.Enter);

            //Method 2: Grab Search-Button and click it
            //Dim SearchButton As IWebElement
            //SearchButton = GetWebElement(driver, By.Id("sb_form_go"), 10)
            //SearchButton.Click()

            //Now get the first result returned by Bing search
            FirstResult = GetWebElement(driver, By.XPath("//ul[@class='sb_results']/li/div/div/div/h3/a"), 10);

            //Method 1: Compare the subject
            string ExpectedText = "dotnet-developer.de | Tips for vb.net,…";
            Assert.AreEqual(ExpectedText, FirstResult.Text, "Subject is not correct");

            //Method 2: Compare the link
            string ExpectedURL = "http://www.dotnet-developer.de/";
            Assert.AreEqual(ExpectedURL, FirstResult.GetAttribute("href"), "URL is not correct!");
        }

        /// <summary>
        /// Retrieve Web Element using default driver and default timeout
        /// </summary>
        /// <param name="definition">Definition of the WebElement to grab</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private IWebElement GetWebElement(OpenQA.Selenium.By definition)
        {
            const int DefaultTimeout = 10;
            return GetWebElement(definition, DefaultTimeout);
        }

        /// <summary>
        /// Retrieve Web Element using default driver
        /// </summary>
        /// <param name="definition">Definition of the WebElement to grab</param>
        /// <param name="timeoutSeconds">Seconds to wait until a timeout is thrown</param>
        /// <returns></returns>
        /// <remarks></remarks>
        private IWebElement GetWebElement(OpenQA.Selenium.By definition, int timeoutSeconds)
        {
            return GetWebElement(driver, definition, timeoutSeconds);
        }

        /// <summary>
        /// Waits until the given element is enabled and visible
        /// </summary>
        /// <param name="webDriver"></param>
        /// <param name="definition"></param>
        /// <param name="seconds"></param>
        /// <returns></returns>
        /// <remarks>Needs to wait for .displayed because for e.g. in a collapsed Treeview all nodes are available but not visible 
        /// if the parent node is collapsed and therefore the following error would appear:
        /// OpenQA.Selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
        /// </remarks>
        private IWebElement GetWebElement(IWebDriver webDriver, OpenQA.Selenium.By definition, int seconds)
        {
            WebDriverWait wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(seconds));

            wait.Until(d => { return d.FindElement(definition).Enabled & d.FindElement(definition).Displayed; });

            return webDriver.FindElement(definition);
        }

    }
}

【问题讨论】:

    标签: c# visual-studio-2010 unit-testing selenium nuget


    【解决方案1】:

    我自己发现了问题。首先,我必须更具体地了解我机器上的软件。我已经在我的 PC 上安装了 Visual Studio 2010 和 Visual Studio 2012。我试图在 Visual Studio 2010 中进行测试,但它们正在等待中。当我在 VS 2012 中打开测试时,一切运行良好,没有问题。看来VS都搞砸了(我不知道),我需要使用更高版本。

    【讨论】:

    • 以为你错了,不是兼容性问题一定是真正的原因,直到我遇到to this blog证明你是正确的。
    • 我在 VS2017 中遇到了同样的问题。我在这台机器上有 VS2015,但很少使用它。关于为什么我仍然遇到同样的问题有什么想法吗?
    • @DanCsharpster 我无法告诉您当前的原因,也许您可​​以提供您在输出窗口中看到的任何内容。但是,ReSharper 可以运行它们。例如,您可以使用 ReShaper 运行 nUnit 测试,而无需在 Visual Studio 中安装 nUnit 扩展。我目前同时拥有 VS2015 和 VS2017,并且两个版本的测试都在我的机器上运行。您也可以尝试使用 MSTest 或 dotnet 在控制台中运行它们,具体取决于它们是 .net 还是 netcore;最坏的情况,它会输出一些为什么它们无法运行的消息。
    • @Nikola,感谢您的建议!我能够修复它。我更新了 NUnit 和 NUnit3Adapter 包,然后将 Resharper 升级到了最新版本。然后一切都很顺利!
    猜你喜欢
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-21
    • 2012-03-28
    • 2012-02-25
    • 2012-03-20
    相关资源
    最近更新 更多