3

I need the help with combining multiple browser test fixtures into my framework. My goal is to run test on multiple browsers one after another by defining type of testFixture: ChromeDriver, InternetExplorerDriver etc.

I have followed one of tutorials on Pluralsight to build my framework. Now this look like this:

TestClass: LoginTest

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

Next, PortalTest base class:

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

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

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

    }

And my Driver class which initializes 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));

    }

As you can see Driver class extends TestBase. This one defined multiple browser cases and returns the appropriate driver.

I had few attempts but with no luck. Related posts I'm basing on: https://stackoverflow.com/a/7854838/2920121

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

Community
  • 1
  • 1
Jakubee
  • 634
  • 1
  • 9
  • 30

1 Answers1

1

You need to have WebDriver factory class to create all driver instance for handling the driver easily.

WebDriver Factory where you instantiate all the drivers

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;
        }
    }
}

Usage

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()
        {

        }
    }
}

I kind of follow this for my testing needs.

Saifur
  • 16,081
  • 6
  • 49
  • 73
  • 1
    Hi. I managed to look at your example just now. I have a question: What BaseTestFixture is? – Jakubee Jul 02 '15 at 09:03
  • I found also useful using NUnit ValueSourceAttribute, to specify list of browser in an external file. I've blogged about this http://www.codewrecks.com/blog/index.php/2016/02/19/parametrize-nunit-selenium-test-for-run-with-different-browsers/ – Alkampfer Feb 24 '16 at 23:58