C# 使用selenium web驱动程序在Visual Studio中测试本地项目
我正在尝试使用NUnit和Selenium Web驱动程序建立一个带有验收测试的visual studio项目,我希望能够“运行测试”,这将启动我的网站,使用Selenium运行测试并退出 到目前为止,我有以下基本设置:C# 使用selenium web驱动程序在Visual Studio中测试本地项目,c#,visual-studio-2010,selenium,nunit,phantomjs,C#,Visual Studio 2010,Selenium,Nunit,Phantomjs,我正在尝试使用NUnit和Selenium Web驱动程序建立一个带有验收测试的visual studio项目,我希望能够“运行测试”,这将启动我的网站,使用Selenium运行测试并退出 到目前为止,我有以下基本设置: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
namespace FrontEndTests.AcceptanceTests
{
[TestFixture]
class Phantom
{
private PhantomJSDriver _driver;
[SetUp]
public void WhenOpeningANewWebPage()
{
_driver = new PhantomJSDriver();
_driver.Navigate().GoToUrl(@"localhost");
}
[Test]
public void ThenICanFindAClass()
{
Assert.NotNull(_driver.FindElement(By.ClassName("featured")));
}
[TearDown]
public void Finally()
{
_driver.Quit();
}
}
}
如果我将URL设置为“www.google.com”,测试可以顺利通过(设置了正确的类),但localhost在selenium中返回elementnotfoundexception
我如何让它在本地工作
谢谢基于此:
“当我在visual studio中运行该项目时,它指向localhost:31106,我曾尝试将其用作URL,但这会产生相同的错误-Gregg_1987” IIS必须正在运行您的应用程序。单击“运行”时,它将在应用程序运行时在IIS express中启动应用程序。VisualStudio然后附加到该文件以执行 如果您试图在此服务器上执行Selenium,则必须安装常规IIS并通过IIS注册应用程序,以便可以访问它。然后,您的测试可以通过在IIS中注册的URL实现这一点。否则,您必须尝试使用IIS express以编程方式执行应用程序,此处有一些指导:
一旦站点可以通过IIS访问,您就可以使用Selenium测试点击它。好的,您需要在所有测试之前启动站点,或者您可以在安装时启动站点一次,然后在拆卸时将其杀死(或者如果您要在某个CI上运行测试,那么在所有测试之前运行一次,最后将其杀死)。要启动它,您可以选择webdev或iisexpress(根据您的选择),下面是使用webdev.WebHost.dll的示例
public class Phantom
{
private PhantomJSDriver _driver;
//Move this field to base class if you need to start site before each test
//e.g. you can move setup and teardown to base class, it's all up to you
public DevServer WebDevServer { get; private set; }
[SetUp]
public void WhenOpeningANewWebPage()
{
WebDevServer = new DevServer();
WebDevServer.Start();
_driver = new PhantomJSDriver();
_driver.Navigate().GoToUrl(@"localhost");
}
[Test]
public void ThenICanFindAClass()
{
Assert.NotNull(_driver.FindElement(By.ClassName("featured")));
}
[TearDown]
public void Finally()
{
_driver.Quit();
WebDevServer.Stop();
}
}
public class DevServer
{
private Server _webServer;
public DirectoryInfo SourcePath { get; set; }
public string VirtualPath { get; set; }
public int Port { get; set; }
public DevServer()
{
//Port
Port = Settings.WebDevPort;
//Path to your site folde
SourcePath = Settings.WebDevSourcePath;
//Virt path can be ~
VirtualPath = Settings.WebDevVirtualPath;
}
public void Start()
{
Stop();
try
{
_webServer = new Server(Port, VirtualPath, SourcePath.FullName);
_webServer.Start();
}
catch (Exception e)
{
Trace.TraceError("Process cannot be started." + Environment.NewLine + e);
throw;
}
}
public void Stop()
{
if (_webServer != null)
{
_webServer.Stop();
_webServer = null;
}
}
}
您确定使用
localhost
,您可以浏览您的应用程序吗?任何端口?当我在visual studio中运行该项目时,它指向localhost:31106。我曾尝试将其用作URL,但这会产生相同的错误。应用程序如何运行?您是否必须按F5/运行才能访问该网站,或者该网站是否在IIS下正确托管?使用ChromeDriver,这样您就有了一个实际的GUI来进行交互,并且可以了解到localhost
的实际用途。我怀疑这只是一个错误页面。当然,这会因每个人的需要而有所不同,但在[TestFixtureSetUp]
和[testfixturedeardown]
中启动/停止WebDev不是更合适吗?这样,您就不必等待服务器为每个测试启动。正如我在回答中所写的,它可以在用户想要的任何地方使用(在安装程序、TestFixtureSetUp中或在所有测试之前)。这取决于人的需要(和测试)。VisualStudio会自动启动并建议“使用System”和“System.Diagnostics”,但您是否愿意包含服务器和设置类的名称空间?