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.
CodePudding user response:
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();