尝试使用Nunit运行多个Selenium Webdriver测试失败

时间:2015-02-16 10:45:04

标签: c# firefox selenium nunit

我想知道是否有人可以帮助我。我在C#和Nunit上使用selenium Webdriver,当我尝试运行多个测试时,我收到以下错误:

  

OpenQA.Selenium.WebDriverException:意外错误。 System.Net.WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:无法建立连接,因为目标计算机主动拒绝它127.0.0.1:7055      在System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)

当我以个人身份运行这些测试时,他们正在运行并且传递正常。

任何帮助或指针都会很棒。

以下是我的代码和我的驱动程序类的示例

  

驱动程序类

using System;
using System.Configuration;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;

namespace SMT.Web.FunctionalTests.Drivers
{
   public class Driver
{
    private static IWebDriver _driver;

    protected static IWebDriver GetWebDriver
    {
        get
        {
            if (_driver == null)
            {
                string driverConfig = ConfigurationManager.AppSettings["browser"];
                if (!String.IsNullOrEmpty(driverConfig))
                {
                    switch (ConfigurationManager.AppSettings["browser"])
                    {
                        case "Chrome":
                            _driver = new ChromeDriver();
                            ConfigureDriver();
                            break;
                        case "Firefox":
                            _driver = new FirefoxDriver();
                            ConfigureDriver();
                            break;
                        case "IE":
                            _driver = new InternetExplorerDriver();
                            ConfigureDriver();
                            break;
                        default:
                            Console.WriteLine("App.config key error");
                            Console.WriteLine("Defaulting to FireFox");
                            _driver = new FirefoxDriver();
                            ConfigureDriver();
                            break;
                    }
                }
                else
                {
                    Console.WriteLine("* * * * DEFAULT MODE * * * *");
                    Console.WriteLine("Defaulting to Firefox");
                    _driver = new FirefoxDriver();
                    ConfigureDriver();

                }
            }
            return _driver;
        }
    }

    internal static void ConfigureDriver(string page = "")
    {
        string url = ConfigurationManager.AppSettings["URL"];
        _driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
        _driver.Manage().Cookies.DeleteAllCookies();
        _driver.Manage().Window.Maximize();
        _driver.Navigate().GoToUrl(string.Concat(url, page));
    }

    public static void Close()
    {
        _driver.Close();
    }
}

}
  

测试类

public class LoginRegressionTests : Driver
{
    public static IWebDriver WebDriver { get; set; }
    public AjaxHelpers AjaxHelpers;
    public DatabaseHelpers DatabaseHelpers;

    [SetUp]
    public void Setup()
    {
        DatabaseHelpers = new DatabaseHelpers();
        DatabaseHelpers.DatabaseSetUpClearDown();
        WebDriver = GetWebDriver;
        AjaxHelpers = new AjaxHelpers(WebDriver);
    }

    [TearDown]
    public void Teardown()
    {
        WebDriver.Close();
    }

    [Test]
    public void LoginWithIncorrectUserNamePasswordTest()
    {
        // Create an Instance of Header Banner Page Object
        var headerBannerPage = new HeaderBannerPage(WebDriver);
        headerBannerPage.ClickSigninLink();


        // Create an Instance of Login Page Object
        var loginPage = new LoginPage(WebDriver);
        loginPage.EnterCustomerUsername("someoneemail@email.com");
        loginPage.EnterCustomerPassword("*&^%£$£");
        loginPage.ClickLoginCustomerButton();

        AjaxHelpers.WaitForAjax();

        String usernameRequiredBorderBottom = WebDriver.FindElement(By.CssSelector("#login-username")).GetCssValue("border-bottom-color");
        String usernameRequiredBorderLeft = WebDriver.FindElement(By.CssSelector("#login-username")).GetCssValue("border-left-color");
        String usernameRequiredBorderRight = WebDriver.FindElement(By.CssSelector("#login-username")).GetCssValue("border-right-color");
        String usernameRequiredBorderTop = WebDriver.FindElement(By.CssSelector("#login-username")).GetCssValue("border-top-color");
        Assert.AreEqual(usernameRequiredBorderBottom, "rgba(181, 79, 80, 1)");
        Assert.AreEqual(usernameRequiredBorderLeft, "rgba(181, 79, 80, 1)");
        Assert.AreEqual(usernameRequiredBorderRight, "rgba(181, 79, 80, 1)");
        Assert.AreEqual(usernameRequiredBorderTop, "rgba(181, 79, 80, 1)");


        String passwordrequiredBorderBottom = WebDriver.FindElement(By.CssSelector("#login-password")).GetCssValue("border-bottom-color");
        String passwordrequiredBorderLeft = WebDriver.FindElement(By.CssSelector("#login-password")).GetCssValue("border-left-color");
        String passwordrequiredBorderRight = WebDriver.FindElement(By.CssSelector("#login-password")).GetCssValue("border-right-color");
        String passwordrequiredBorderTop = WebDriver.FindElement(By.CssSelector("#login-password")).GetCssValue("border-top-color");
        Assert.AreEqual(passwordrequiredBorderBottom, "rgba(181, 79, 80, 1)");
        Assert.AreEqual(passwordrequiredBorderLeft, "rgba(181, 79, 80, 1)");
        Assert.AreEqual(passwordrequiredBorderRight, "rgba(181, 79, 80, 1)");
        Assert.AreEqual(passwordrequiredBorderTop, "rgba(181, 79, 80, 1)");

    }
}

0 个答案:

没有答案