使用Selenium和NUnit在多个浏览器上进行测试框架

3

我需要帮助将多个浏览器测试夹具合并到我的框架中。 我的目标是通过定义testFixture的类型(ChromeDriver,InternetExplorerDriver等)依次在多个浏览器上运行测试。

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

TestClass:LoginTest

[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()的LoginPage:

 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://dev59.com/52445IYBdhLWcg3wH2rH#7854838

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

1个回答

1
你需要一个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


1
嗨,我刚刚看了你的例子。我有一个问题:什么是BaseTestFixture? - Jakubee
我发现使用NUnit ValueSourceAttribute也很有用,可以在外部文件中指定浏览器列表。我在博客中写了这个 http://www.codewrecks.com/blog/index.php/2016/02/19/parametrize-nunit-selenium-test-for-run-with-different-browsers/ - Alkampfer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接