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.
1 Answer
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();
Update:
If it is critical to test IisExpress shutdown than you should send "Q" key press to Iis Express window and wait till process termination.
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
public void StopIis()
{
if (_iisExpressProcess != null)
{
// wait Iis express show console window
for (int n = 0; n < 50; n++) // 50 * 100 msec
{
_iisExpressProcess.Refresh();
if (_iisExpressProcess.MainWindowHandle.ToInt32() != 0)
{
break;
}
Debug.WriteLine("Wait for process window");
Thread.Sleep(100);
}
uint WM_KEYDOWN = 0x0100;
int KEY_Q = 0x51;
PostMessage(_iisExpressProcess.MainWindowHandle, WM_KEYDOWN, KEY_Q, 0);
_iisExpressProcess.WaitForExit();
}
}