3

I have a solution with 2 projects.

  1. The static web page project
  2. The selenium tests project

Here's my Test File:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SeleniuumTestSuite
{
    [TestClass]
    public class HomePageTest
    {
        private string baseURL = "http://localhost:56403/";
        private static IWebDriver driver;

        [AssemblyInitialize]
        public static void SetUp(TestContext context)
        {
            driver = new ChromeDriver();
        }

        [TestMethod]
        public void RemoteSelenium()
        {
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl(this.baseURL);
        }

        [TestCleanup]
        public void Finally()
        {
            driver.Quit();

        }
    }
}

I need to start the localhost project before the test case runs so that navigating to the localhost doesn't result in a 404. I found this post that seems to answer that question but I have no idea what library the solution is using.

I tried using NuGet to download Microsoft.AspNet.WebApi.WebHost but if I try to do private Server _webServer = new Server(Port, VirtualPath, SourcePath.FullName);, VS doesn't recognize Server and has no idea what library to import. So I'm kind of stuck here.

Any idea how to get this to work?

Community
  • 1
  • 1
Richard
  • 5,840
  • 36
  • 123
  • 208
  • It sounds like you need to use some kind of build tool to start your project when triggering tests. Or you can just start your application manually before running the tests. Also, it looks like you're only starting the driver once in `AssemblyInitialize` but you're quitting it after every test in `TestCleanup` – mrfreester Jan 31 '17 at 18:23
  • @mrfreester I tried to do that, I started the project then tried to run the test case but it wouldn't let me. The option was just grayed out. – Richard Jan 31 '17 at 19:06
  • 1
    can you open two VS instances? One to run your project, one to run your test? – mrfreester Jan 31 '17 at 19:16
  • 1
    @mrfreester yup that worked but there's got to be a way to do it in a single instance of VS right? – Richard Jan 31 '17 at 19:36
  • 2
    I think if you right click the project in the `Solutions Explorer`, go to `Debug > start new instance` that will do what you want. I'm not sure if there is a way to automatically start the application when you try to run a test inside VS without some other tools, but that would be a nice feature. – mrfreester Jan 31 '17 at 19:52
  • It's been a while but... I have also solved the problem by running two Vs, one to run the app normally, another one to run the tests. It works. Yet when you start a new instance from Debug option in solution explorer, the same original problem (tests run button appears disabled) happens. – Alvaro Rodriguez Scelza Jun 28 '20 at 01:26

1 Answers1

3

In order to solve the problem, it is necessary to run the two projects at the same time, as mrfreester pointed out in comments:

  • The main project in order to be able to run the main application.
  • The test project which will access the running main application to test it.

As mrfreester suggested, you might use two visual studio instances and it will work. Yet, to enhance this solution and manage everything in just one visual studio instance, you can run the main project using Debug.StartWithoutDebugging (default keyboard shortcut is ctrl + f5). This will effectively run the server for the application without starting VS debug mode, allowing you (and your test project) to normally use the application. The application will run even if you close your browser.

Be noted: if you start your application debugging normally, when you stop the execution, the server will stop, and you will have to start again without debugging to be able to pass your tests again.

Alvaro Rodriguez Scelza
  • 3,643
  • 2
  • 32
  • 47