c# - 使用 selenium 和 NUnit 在多个浏览器上测试框架

标签 c# selenium selenium-webdriver nunit

我需要帮助将多个浏览器测试装置组合到我的框架中。 我的目标是通过定义 testFixture 的类型在多个浏览器上依次运行测试:ChromeDriver、InternetExplorerDriver 等。

我已经按照 Pluralsight 上的一个教程构建了我的框架。现在看起来像这样:

测试类:登录测试

[TestFixture]
public class LoginTest : PortalTest
{
    [Test]
    public void LoginUser()
    {
        Assert.IsTrue(HomePage.IsAt, "Failed to login. ");
    }
}

接下来,PortalTest基类:

public class PortalTest
{
    [SetUp]
    public void Init()
    {
        Driver.Initialize();
        LoginPage.Goto();
        LoginPage.LoginAs("user").WithPassword("pass").Login();
    }

    [TearDown]
    public void CleanUp()
    {
        Driver.Close();
    }
}

使用 GoTo() 的登录页面:

 public class LoginPage
{
    public static void Goto()
    {
        //var wait = new WebDriverWait(Driver.Instance, TimeSpan.FromSeconds(10));
        //wait.Until(d => d.SwitchTo().ActiveElement().GetAttribute("id") == "UserName");
        Driver.Instance.Navigate().GoToUrl(Driver.BaseAddress + "Account/LogOn?ReturnUrl=%2FHome");
        if (Driver.Instance.Title != "Login")
        {
            throw new Exception("Not on Login page");
        }

    }

我的 Driver 类初始化 FirefoxDriver:

public class Driver : TestBase
{
    public static IWebDriver Instance { get; set; }

    public static void Initialize()
    {
        Instance = new FirefoxDriver();

        // wait 5 sec
        Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));

    }

如您所见,Driver 类扩展了 TestBase。这个定义了多个浏览器案例并返回适当的驱动程序。

我尝试了几次,但没有运气。 我基于的相关帖子: https://stackoverflow.com/a/7854838/2920121

http://makit.net/testing-aspdotnet-mvc-application-with-selenium-and-nunit

最佳答案

您需要有 WebDriver 工厂类来创建所有驱动程序实例,以便轻松处理驱动程序。

在其中实例化所有驱动程序的 WebDriver 工厂

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.PhantomJS;

namespace Test.Tests
{
    /// <summary>
    /// A static factory object for creating WebDriver instances
    /// </summary>
    public class WebDriverFactory
    {
        public IWebDriver Driver;

        protected WebDriverFactory(BrowserType type)
        {
            Driver = WebDriver(type);            
        }

        [TestFixtureTearDown]
        public void TestFixtureTearnDown()
        {
            Driver.Quit();
        }

        /// <summary>
        /// Types of browser available for proxy examples.
        /// </summary>
        public enum BrowserType
        {
            IE,
            Chrome,
            Firefox,
            PhantomJS
        }

        public static IWebDriver WebDriver(BrowserType type)
        {
            IWebDriver driver = null;

            switch (type)
            {
                case BrowserType.IE:
                    driver = IeDriver();
                    break;
                case BrowserType.Firefox:
                    driver = FirefoxDriver();
                    break;
                case BrowserType.Chrome:
                    driver = ChromeDriver();
                    break;
                default:
                    driver = PhanthomJsDriver();
                    break;
            }

            return driver;
        }

        /// <summary>
        /// Creates Internet Explorer Driver instance.
        /// </summary>
        /// <returns>A new instance of IEDriverServer</returns>
        private static IWebDriver IeDriver()
        {
            InternetExplorerOptions options = new InternetExplorerOptions();
            options.EnsureCleanSession = true;
            IWebDriver driver = new InternetExplorerDriver(options);
            return driver;
        }

        /// <summary>
        /// Creates Firefox Driver instance.
        /// </summary>
        /// <returns>A new instance of Firefox Driver</returns>
        private static IWebDriver FirefoxDriver()
        {
            FirefoxProfile profile = new FirefoxProfile();
            IWebDriver driver = new FirefoxDriver(profile);
            return driver;
        }


        /// <summary>
        /// Creates Chrome Driver instance.
        /// </summary>
        /// <returns>A new instance of Chrome Driver</returns>
        private static IWebDriver ChromeDriver()
        {
            ChromeOptions chromeOptions = new ChromeOptions();
            IWebDriver driver = new ChromeDriver(chromeOptions);
            return driver;
        }

        /// <summary>
        /// Creates PhantomJs Driver instance..
        /// </summary>
        /// <returns>A new instance of PhantomJs</returns>
        private static IWebDriver PhanthomJsDriver()
        {
            PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
            if (proxy != null)
            IWebDriver driver = new PhantomJSDriver(service);
            return driver;
        }
    }
}

用法

using System;
using NUnit.Framework;

namespace Test.TestUI
{
    [TestFixture(BaseTestFixture.BrowserType.Chrome)]
    [TestFixture(BaseTestFixture.BrowserType.Firefox)]
    [TestFixture(BaseTestFixture.BrowserType.InternetExplorer)]
    public class DemoTest : WebDriverFactory
    {
        public DemoTest(BaseTestFixture.BrowserType browser)
            : base(browser)
        {

        }

        [TestFixtureSetUp]
        public void SetUpEnvironment()
        {

        }
    }
}

我有点关注 this满足我的测试需求。

关于c# - 使用 selenium 和 NUnit 在多个浏览器上测试框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31136136/

相关文章:

c# - RTC授权总是失败

c# - ASP.NET Web UI ListView 不会刷新

windows - 如何将 Selenium Firefox (IDE) 测试移植到其他浏览器? ( Windows )

java - Selenium +Java : Parallel execution for test cases having login functionality

Python + Selenium WebDriver : open URL in new tab

python - 如何在 Ubuntu 上正确使用带有 geckodriver 的 selenium 和带有 python 的 firefox?

c# - 如何使用验证保持 ViewModel 和 View 同步

C#泛型方法选择

unit-testing - Chrome Webdriver 在 headless 模式下丢失用户凭据

python Selenium : Safari returns different text for span than Chrome and Firefox