【问题标题】:Testing framework on multiple browsers using selenium and NUnit使用 selenium 和 NUnit 在多个浏览器上测试框架
【发布时间】:2015-06-30 10:44:11
【问题描述】:

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

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

TestClass:登录测试

[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");
        }

    }

还有初始化 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

【问题讨论】:

    标签: c# selenium selenium-webdriver nunit


    【解决方案1】:

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

    WebDriver Factory,您可以在其中实例化所有驱动程序

    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

    【讨论】:

    • 嗨。我刚才设法看了你的例子。我有一个问题:什么是 BaseTestFixture?
    • 我发现使用 NUnit ValueSourceAttribute 来指定外部文件中的浏览器列表也很有用。我在博客上写过这个codewrecks.com/blog/index.php/2016/02/19/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多