简体   繁体   中英

How to run a web application project (ASP.NET MVC) within C#, NUnit project in Visual Studio 2022

I'm building a NUnit test project in Visual Studio (VS) 2022 in order to test an older web site (MVC 4, ASP.NET). I would like to run a Selenium WebDriver to test the older, MVC 4, web application in VS 2022. The issue I've run across is that I need to run the site and run the tests at the same time. In the Selenium WebDriver documentation, I need to give a URL for it to test, but the web site is running in debug in VS 2022. The code I need to test doesn't have a URL. How can I run the website from the C# code in the test project, and then run the Selenium WebDriver to test the site? Thank you for your help.

If you are using IIS Express to debug your web app, you can use following code in test module init (SetUp if i'm not wrong):

        Process _iisExpressProcess;

        const string webAppPath = @"..."; // path to web app folder with web.config 
        const string webAppPort = "..."; // port of your web app

        ProcessStartInfo startInfo = new ProcessStartInfo
        {
            WindowStyle = ProcessWindowStyle.Normal,
            ErrorDialog = true,
            LoadUserProfile = true,
            CreateNoWindow = false,
            UseShellExecute = true,
            Arguments = string.Format("/path:\"{0}\" /port:{1}", webAppPath, webAppPort)
        };
        
        startInfo.FileName = "..."; // absolute path to your iisexpress.exe f.e. C:\Program Files\IIS Express\iisexpress.exe

        _iisExpressProcess = new Process { StartInfo = startInfo };
        _iisExpressProcess.Start();

than your selenium tests methods is going to be be executed.

On test module uninit (TearDown if i'm not wrong) you should terminate iisexpress execution by calling

_iisExpressProcess.CloseMainWindow();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM