Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Goal:
To use Microsofts IIS express when using Selenium. In othe words, execute unit testing in debug mode.

Problem:
i don't know how to make it working in order to use IIS express by using C# code. in other word, I can't initiate the IIS express with c# code. What am I missing in order to execute the IIS so I can enter the webpage "localhost:52844" and then enable to use the sourcecode of selenium?

Info:
*Im using ASP.net mvc.
*The sourcode is ASP.NET MVC + Selenium + IISExpress | Stephen Walther[^]
*I have seen some youtube clip that you an use Aspnet mvc in relation to Selenium.
*Im using the regular webpage that is created by Microsoft. Please take a look at the picture below.
*I have added the code "" in html page.
*Im using VS2013
*Im only interrested in Firefox

Thank you!



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcApplication11;
using MvcApplication11.Controllers;
using OpenQA.Selenium;

namespace MvcApplication11.Tests.Controllers
{
    [TestClass]
    public class HomeControllerTests : SeleniumTest
    {

        public HomeControllerTests() : base("MvcApplication11") { }


        [TestMethod]
        public void IndexFirefoxTest()
        {
            // Act
            this.FirefoxDriver.Navigate().GoToUrl(this.GetAbsoluteUrl("/home/index"));
            this.FirefoxDriver.FindElement(By.Id("btn")).Click();

            // Assert
            Assert.IsTrue(this.FirefoxDriver.FindElement(By.Id("testd")).Displayed);
        }


    }
}





C#
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Diagnostics;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using System.IO;

namespace MvcApplication11.Tests.Controllers
{
    [TestClass]
    public abstract class SeleniumTest {

        const int iisPort = 52844;
        private string _applicationName;
        private Process _iisProcess;

        protected SeleniumTest(string applicationName) {
            _applicationName = applicationName;
        }


        public FirefoxDriver FirefoxDriver { get; set; }


        [TestInitialize]
        public void TestInitialize() {
            // Start IISExpress
            StartIIS();

            // Start Selenium drivers
            this.FirefoxDriver = new FirefoxDriver();
        }


        [TestCleanup]
        public void TestCleanup() {
            // Ensure IISExpress is stopped
            if (_iisProcess.HasExited == false) {
                _iisProcess.Kill();
            }

            // Stop all Selenium drivers
            this.FirefoxDriver.Quit();
        }



        private void StartIIS() {
            var applicationPath = GetApplicationPath(_applicationName);
            var programFiles = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

            _iisProcess = new Process();
            _iisProcess.StartInfo.FileName = programFiles + "\\IIS Express\\iisexpress.exe";
            _iisProcess.StartInfo.Arguments = string.Format("//path:" + applicationPath + " //port:" + iisPort);
            _iisProcess.Start();
        }


        protected virtual string GetApplicationPath(string applicationName) {
            var solutionFolder = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)));
            return Path.Combine(solutionFolder, applicationName);
        }


        public string GetAbsoluteUrl(string relativeUrl) {
            if (!relativeUrl.StartsWith("/")) {
                relativeUrl = "/" + relativeUrl;
            }
            return String.Format("http://localhost:{0}{1}", iisPort, relativeUrl);
        }


    }
}


What I have tried:

Well i have tried a lot and made lots of test and google around but I still cannot find a solution in order to execute unit testing in debug mode.
Posted
Updated 26-Mar-16 20:25pm
v2
Comments
F-ES Sitecore 26-Mar-16 13:32pm    
I don't fully understand the question but the solution is probably selecting "Debug->Attach to process" from Visual Studio and attaching to the correct instance of the process that hosts your code. This can vary depending on the version of IIS you're using, it might be w3wp.exe or it might be an exe with asp in the name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900