使用Selenium Web驱动程序在Visual Studio中测试本地项目 [英] Testing local project in Visual Studio using selenium web driver

查看:209
本文介绍了使用Selenium Web驱动程序在Visual Studio中测试本地项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NUnit和Selenium Web Driver建立一个带有验收测试的Visual Studio项目,我希望能够运行测试",并且这可以启动我的网站,使用Selenium来运行测试并退出

I am trying to set up a visual studio project with acceptance tests using NUnit and Selenium Web Driver, I would like to be able to "run tests" and this to start my web site, use selenium to run the tests and quit.

到目前为止,我已经完成了以下基本设置:

I have this basic setup so far:

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在硒中返回elementnotfoundexception.

If I set the URL to 'www.google.com' the tests pass fine (with the correct class set) but localhost returns elementnotfoundexception in selenium.

我如何使其在本地工作?

How do I get it to work locally?

谢谢

推荐答案

好,您需要在所有测试之前启动站点,或者可以在SetUp中启动一次并在TearDown中杀死它(或者如果要运行您的在某些CI上进行测试,然后在所有测试之前运行一次,并最终终止.)要启动它,可以在使用WebDev.WebHost.dll的示例下面选择webdev或iisexpress(根据您的选择)

Well, you need to start you site before all tests or you can start it once in SetUp and kill it in TearDown (or if you are going to run your tests on some CI then run once before all tests and kill after all). To start it you can choose either webdev or iisexpress (on your choice), below sample of using 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;
            }
        }
    }

这篇关于使用Selenium Web驱动程序在Visual Studio中测试本地项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆