2013-03-11 9 views
5

Sto provando per diverse ore a effettuare un test dell'unità con il driver Selenium per un sito Web personalizzato. Il problema è che il mio primo test case è sempre in sospeso. Non ho scritto nessun altro. Ho provato diversi tutorial, ma nessuno funziona. Cosa ho fatto:Visual Studio 2010 (win 7 x64) Il test delle unità di selenio rimane in sospeso

  • Installare Selenium WebDriver tramite NuGet Console (avere un riferimento);
  • Installare Selenium WebDriver.Support tramite NuGet (avere un riferimento);
  • Ho persino spento il firewall;

Infine, sto seguendo il tutorial in questa pagina dotnet-developer.de! Anche il tutorial non funziona. È dato in vb.net, ma l'ho tradotto in C# come segue:

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

    } 
} 

risposta

2

Ho identificato il problema da solo. Prima di tutto, devo essere più specifico riguardo al software sulla mia macchina. Ho installato sia Visual Studio 2010 e Visual Studio 2012 sul mio PC. Stavo cercando di eseguire i test in Visual Studio 2010, ma erano in sospeso. Quando ho aperto i test in VS 2012, tutti corrono bene e non ho avuto problemi. Sembra che entrambi abbiano rovinato qualcosa (di cui non sono a conoscenza) e ho bisogno di usare la versione successiva.

+1

Pensavo che tu avessi torto e qualcosa che non riguardava i problemi di compatibilità doveva essere la vera causa, fino a quando non mi sono imbattuto in [questo blog] (http://blogs.msdn.com/b/aseemb/archive/2012/09/ 08/not-able-to-run-unit-test-when-visual-studio-2010-and-visual-studio-2012-are-installed-side-by-side.aspx) che dimostra che sei corretto. – Arman

+0

Sto riscontrando lo stesso problema con VS2017. Ho VS2015 su questa macchina ma l'ho usato raramente. Qualche idea sul perché sto ancora ricevendo lo stesso problema? –

+1

@DanCsharpster Non posso dirti il ​​motivo corrente, forse se potresti fornire tutto ciò che vedi nella finestra di output. Tuttavia, ReSharper potrebbe essere in grado di eseguirli. Ad esempio, è possibile eseguire test nUnit con ReShaper, senza la necessità di installare l'estensione nUnit in Visual Studio. Al momento ho sia VS2015 e VS2017 e i test sono in esecuzione sulla mia macchina in entrambe le versioni. Si potrebbe anche provare a eseguirli nella console con MSTest o dotnet, a seconda che siano .net o netcore; Nel peggiore dei casi, produrrà alcuni messaggi sul perché non possono essere eseguiti. – Nikola

Problemi correlati