2013-03-11 25 views
5

Tôi đang thử vài giờ để thực hiện Kiểm tra đơn vị với trình điều khiển Selenium cho một trang web tùy chỉnh. Vấn đề là, trường hợp thử nghiệm đầu tiên của tôi luôn được giải quyết. Tôi chưa viết gì khác. Tôi đã thử một số hướng dẫn khác nhau, nhưng không có công trình nào. Những gì tôi đã làm:Visual Studio 2010 (giành chiến thắng 7 x64) Kiểm tra đơn vị Selenium Keeps Pending

  • Cài đặt Selenium WebDriver qua NuGet Console (có tham chiếu);
  • Cài đặt Selenium WebDriver.Support thông qua NuGet (có tham chiếu);
  • Tôi thậm chí đã tắt tường lửa;

Cuối cùng, tôi đang làm theo hướng dẫn trên trang này dotnet-developer.de! Hướng dẫn cũng không hoạt động. Nó được đưa ra trong vb.net, nhưng tôi đã dịch nó trong C# như sau:

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); 
     } 

    } 
} 

Trả lời

2

Tôi đã tự xác định được vấn đề. Trước hết, tôi phải cụ thể hơn về phần mềm trên máy của tôi. Tôi đã cài đặt cả Visual Studio 2010 và Visual Studio 2012 trên PC của mình. Tôi đã cố gắng để làm cho các bài kiểm tra trong Visual Studio 2010, nhưng họ đang chờ giải quyết. Khi tôi mở các bài kiểm tra trong VS 2012, tất cả chạy tốt và tôi không có vấn đề gì. Dường như cả hai VS lộn xộn một cái gì đó (mà tôi không biết) và tôi cần phải sử dụng phiên bản sau.

+1

Nghĩ rằng bạn sai và không có vấn đề tương thích nào phải là nguyên nhân thực sự, cho đến khi tôi gặp [blog này] (http://blogs.msdn.com/b/aseemb/archive/2012/09/ 08/không thể chạy-đơn vị-kiểm tra-khi-visual-studio-2010-và-visual-studio-2012-được-cài đặt-side-by-side.aspx) mà chứng minh rằng bạn là chính xác. – Arman

+0

Tôi gặp vấn đề tương tự với VS2017. Tôi có VS2015 trên máy này nhưng hiếm khi sử dụng nó. Bất kỳ suy nghĩ về lý do tại sao tôi vẫn nhận được cùng một vấn đề? –

+1

@DanCsharpster Tôi không thể cho bạn biết lý do hiện tại, có thể nếu bạn có thể cung cấp bất cứ điều gì bạn thấy trong cửa sổ đầu ra. Tuy nhiên, ReSharper có thể chạy chúng. Ví dụ, bạn có thể chạy thử nghiệm nUnit với ReShaper, mà không cần cài đặt phần mở rộng nUnit trong Visual Studio. Tôi hiện đang có cả hai VS2015 và VS2017 và các thử nghiệm đang chạy trên máy của tôi trong cả hai phiên bản. Bạn cũng có thể thử chạy chúng trong giao diện điều khiển với MSTest hoặc dotnet, tùy thuộc vào chúng là .net hoặc netcore; trường hợp xấu nhất, nó sẽ xuất ra một số thông báo tại sao chúng không thể chạy. – Nikola

Các vấn đề liên quan