Skip to content

More Fixtures

fonlow edited this page Oct 10, 2016 · 4 revisions

There are fixtures for using other libraries more efficiently, and the code could be too trivial to be included in the FonlowTesting libraries. They are available through source code samples.

BrowserFixture

If you are usingOpenQA.Selenium for basic frontend testing with a browser, a BrowserFixture may be handy.

using System;
using Xunit;
using System.Diagnostics;
using Fonlow.Testing;
using OpenQA.Selenium.Remote;


namespace WebIntegrationTests
{
    public class BrowserFixture : IDisposable
    {
        public BrowserFixture()
        {
            BaseUri = (string)System.Configuration.ConfigurationManager.AppSettings["Testing_BaseUrl"];

            WebDriver = new OpenQA.Selenium.Firefox.FirefoxDriver();
        }

        public RemoteWebDriver WebDriver { get; private set; }

        public string BaseUri
        { get; private set; }



        public void Dispose()
        {
            if (WebDriver != null)
                WebDriver.Dispose();
        }
    }

    public class AdminWebTests : IClassFixture<IisExpressFixture>, IClassFixture<BrowserFixture>
    {

        public AdminWebTests(BrowserFixture fixture)
        {
            this.resource = fixture;
            this.webDriver = fixture.WebDriver;
        }

        BrowserFixture resource;
        RemoteWebDriver webDriver;

        const string adminLoginName = "Admin";
        const string adminPassword = "Oooooooo*7";

        void LoginAsAdmin()
        {
            webDriver.Navigate().GoToUrl(resource.BaseUri + "Account/Login");//IE security setting needs to be in protected mode; or the runner runs in admin account.       
            webDriver.FindElementById("UserName").Clear();
            webDriver.FindElementById("UserName").SendKeys(adminLoginName);
            webDriver.FindElementById("Password").Clear();
            webDriver.FindElementById("Password").SendKeys(adminPassword);
            webDriver.FindElementByCssSelector("input[type='submit']").Click();
        }
        [Fact]
        public void TestLoginAsAdmin()
        {
            LoginAsAdmin();


            Assert.Contains(adminLoginName, webDriver.FindElementByCssSelector("[title=\"Manage\"]").Text, StringComparison.CurrentCultureIgnoreCase);
        }

        [Fact]
        public void TestRegisterExistingUser()
        {
            webDriver.Navigate().GoToUrl(resource.BaseUri + "Account/Register");
            webDriver.FindElementById("UserName").SendKeys(adminLoginName);
            webDriver.FindElementById("Email").SendKeys(Guid.NewGuid().ToString("N")+"@fonlow.com");
            webDriver.FindElementById("Password").SendKeys("ttttTTTT1_");
            webDriver.FindElementById("ConfirmPassword").SendKeys("ttttTTTT1_");
            webDriver.FindElementByCssSelector("input[type='submit']").Click();
            Assert.True(webDriver.PageSource.Contains("already taken"));
        }

DriverServer setup

To use a Web browser, you need to copy respective browser driver to a local path defined in Windows Environment Variable Path, for example, c:\SeleniumDriverServers.

Google Chrome

For Google Chrome, download from https://sites.google.com/a/chromium.org/chromedriver/downloads

It is recommended to use Google Chrome:

  1. Comprehensive support for front-end debugging.
  2. Each instance is with a process.
  3. Over years, it seem that Chrome has less breaking changes than other browsers when dealing with Selenium.

Internet Explorer

For Internet Explorer, download the Internet Explorer Driver Server from from http://www.seleniumhq.org/download/

It is recommended to use the 32-bit one, since the 64-bit one is very slow in a Windows 64 bit machine.

Also read the instructions at https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#Required_Configuration

Firefox

Noted that Visual Studio will recognize the new path after restarting VS.

Clone this wiki locally